{"version":3,"file":"components.mjs","sources":["../../src/loaders/components.ts"],"sourcesContent":["import { join } from 'path';\nimport _ from 'lodash';\nimport { pathExists } from 'fs-extra';\nimport type { Core, Struct, UID } from '@strapi/types';\nimport { loadFiles } from '../utils/load-files';\n\ntype LoadedComponent = {\n  collectionName: string;\n  __filename__: string;\n  __schema__: LoadedComponent;\n  uid: string;\n  category: string;\n  modelName: string;\n  globalId: string;\n  info: any;\n  attributes: any;\n};\n\ntype LoadedComponents = {\n  [category: string]: {\n    [key: string]: LoadedComponent;\n  };\n};\n\ntype ComponentMap = {\n  [uid in UID.Component]: Struct.ComponentSchema;\n};\n\nexport default async function loadComponents(strapi: Core.Strapi) {\n  if (!(await pathExists(strapi.dirs.dist.components))) {\n    return {};\n  }\n\n  const map = await loadFiles<LoadedComponents>(strapi.dirs.dist.components, '*/*.*(js|json)');\n\n  const components = Object.keys(map).reduce((acc, category) => {\n    Object.keys(map[category]).forEach((key) => {\n      const schema = map[category][key];\n\n      if (!schema.collectionName) {\n        // NOTE: We're using the filepath from the app directory instead of the dist for information purpose\n        const filePath = join(strapi.dirs.app.components, category, schema.__filename__);\n\n        return strapi.stopWithError(\n          `Component ${key} is missing a \"collectionName\" property.\\nVerify file ${filePath}.`\n        );\n      }\n\n      const uid: UID.Component = `${category}.${key}`;\n\n      acc[uid] = Object.assign(schema, {\n        __schema__: _.cloneDeep(schema),\n        uid,\n        category,\n        modelType: 'component' as const,\n        modelName: key,\n        globalId: schema.globalId || _.upperFirst(_.camelCase(`component_${uid}`)),\n      });\n    });\n\n    return acc;\n  }, {} as ComponentMap);\n\n  strapi.get('components').add(components);\n}\n"],"names":["loadComponents","strapi","pathExists","dirs","dist","components","map","loadFiles","Object","keys","reduce","acc","category","forEach","key","schema","collectionName","filePath","join","app","__filename__","stopWithError","uid","assign","__schema__","_","cloneDeep","modelType","modelName","globalId","upperFirst","camelCase","get","add"],"mappings":";;;;;AA4Be,eAAeA,eAAeC,MAAmB,EAAA;IAC9D,IAAI,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