{"version":3,"file":"loadModels.cjs","names":["prismicCT"],"sources":["../../src/cli/loadModels.ts"],"sourcesContent":["import { readFileSync } from \"fs\";\n\nimport type { CustomTypeModel, SharedSliceModel } from \"@prismicio/client\";\nimport * as prismicCT from \"@prismicio/custom-types-client\";\nimport fg from \"fast-glob\";\nimport fetch from \"node-fetch\";\n\nconst isCustomTypeModel = (input: unknown): input is CustomTypeModel => {\n\treturn typeof input === \"object\" && input !== null && \"json\" in input;\n};\n\nconst isSharedSliceModel = (input: unknown): input is SharedSliceModel => {\n\treturn typeof input === \"object\" && input !== null && \"variations\" in input;\n};\n\nconst readJSONFromGlob = async <T>(globs: string): Promise<T[]> => {\n\tconst paths = await fg(globs.split(\",\").map((path) => path.trim()));\n\n\treturn paths.map((path) => {\n\t\tconst raw = readFileSync(path, \"utf8\");\n\n\t\treturn JSON.parse(raw);\n\t});\n};\n\ntype LoadModelsConfig =\n\t| {\n\t\t\tlocalPaths?: string[];\n\t  }\n\t| {\n\t\t\tlocalPaths?: string[];\n\t\t\trepositoryName: string;\n\t\t\tcustomTypesAPIToken: string;\n\t\t\tfetchFromRepository?: boolean;\n\t  };\n\ntype LoadModelsReturnType = {\n\tcustomTypeModels: CustomTypeModel[];\n\tsharedSliceModels: SharedSliceModel[];\n};\n\nexport const loadModels = async (config: LoadModelsConfig): Promise<LoadModelsReturnType> => {\n\tconst customTypeModels: Record<string, CustomTypeModel> = {};\n\tconst sharedSliceModels: Record<string, SharedSliceModel> = {};\n\n\tif (\"customTypesAPIToken\" in config) {\n\t\tconst customTypesClient = prismicCT.createClient({\n\t\t\trepositoryName: config.repositoryName,\n\t\t\ttoken: config.customTypesAPIToken,\n\t\t\tfetch,\n\t\t});\n\n\t\tif (config.fetchFromRepository) {\n\t\t\tconst [remoteCustomTypeModels, remoteSharedSliceModels] = await Promise.all([\n\t\t\t\tcustomTypesClient.getAllCustomTypes(),\n\t\t\t\tcustomTypesClient.getAllSharedSlices(),\n\t\t\t]);\n\n\t\t\tfor (const customTypeModel of remoteCustomTypeModels) {\n\t\t\t\tcustomTypeModels[customTypeModel.id] = customTypeModel;\n\t\t\t}\n\t\t\tfor (const sharedSliceModel of remoteSharedSliceModels) {\n\t\t\t\tsharedSliceModels[sharedSliceModel.id] = sharedSliceModel;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (config.localPaths) {\n\t\tconst models = (\n\t\t\tawait Promise.all(config.localPaths.map((glob) => readJSONFromGlob(glob)))\n\t\t).flat();\n\n\t\tfor (const model of models) {\n\t\t\tif (isCustomTypeModel(model)) {\n\t\t\t\tcustomTypeModels[model.id] = model;\n\t\t\t} else if (isSharedSliceModel(model)) {\n\t\t\t\tsharedSliceModels[model.id] = model;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn {\n\t\tcustomTypeModels: Object.values(customTypeModels).sort((a, b) => {\n\t\t\treturn a.id.localeCompare(b.id);\n\t\t}),\n\t\tsharedSliceModels: Object.values(sharedSliceModels).sort((a, b) => {\n\t\t\treturn a.id.localeCompare(b.id);\n\t\t}),\n\t};\n};\n"],"mappings":";;;;;;;;;AAOA,MAAM,qBAAqB,UAA6C;AACvE,QAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,UAAU;;AAGjE,MAAM,sBAAsB,UAA8C;AACzE,QAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,gBAAgB;;AAGvE,MAAM,mBAAmB,OAAU,UAAgC;AAGlE,SAFc,OAAA,GAAA,UAAA,SAAS,MAAM,MAAM,IAAI,CAAC,KAAK,SAAS,KAAK,MAAM,CAAC,CAAC,EAEtD,KAAK,SAAS;EAC1B,MAAM,OAAA,GAAA,GAAA,cAAmB,MAAM,OAAO;AAEtC,SAAO,KAAK,MAAM,IAAI;GACrB;;AAmBH,MAAa,aAAa,OAAO,WAA4D;CAC5F,MAAM,mBAAoD,EAAE;CAC5D,MAAM,oBAAsD,EAAE;AAE9D,KAAI,yBAAyB,QAAQ;EACpC,MAAM,oBAAoBA,+BAAU,aAAa;GAChD,gBAAgB,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