{"version":3,"file":"onPostBootstrap.cjs","sources":["../../../src/gatsby-node/onPostBootstrap.ts"],"sourcesContent":["import * as path from \"path\";\nimport type {\n\tCustomTypeModel,\n\tPrismicDocument,\n\tSharedSliceModel,\n} from \"@prismicio/client\";\nimport type { Node, ParentSpanPluginArgs } from \"gatsby\";\nimport { createMutex } from \"gatsby-core-utils/mutex\";\n\nimport { getPublicModelsFileName } from \"../lib/getPublicModelsFileName\";\nimport { pascalCase } from \"../lib/pascalCase\";\n\nimport { PluginOptions } from \"../types\";\n\nimport * as fs from \"fs/promises\";\n\nconst isCustomTypeModelIDs = (input: unknown): input is string[] => {\n\treturn (\n\t\tArray.isArray(input) &&\n\t\tinput.every((element) => typeof element === \"string\")\n\t);\n};\n\ntype PrismicDocumentNodeLike = Node & {\n\tprismicId: string;\n\traw: PrismicDocument;\n};\n\nexport const onPostBootstrap = async (\n\targs: ParentSpanPluginArgs,\n\toptions: PluginOptions,\n): Promise<void> => {\n\tconst gatsbySourcePrismicCache = args.getCache(\"gatsby-source-prismic\");\n\n\tconst customTypeModelIDs = await gatsbySourcePrismicCache.get(\n\t\t`${options.repositoryName}:customTypeModelIDs`,\n\t);\n\tif (!isCustomTypeModelIDs(customTypeModelIDs)) {\n\t\tthrow new Error(\n\t\t\t\"Did not find Custom Type model IDs from gatsby-source-prismic.\",\n\t\t);\n\t}\n\n\tconst nodes: PrismicDocumentNodeLike[] = [];\n\n\tfor (const customTypeModelID of customTypeModelIDs) {\n\t\tconst nodesForType = args.getNodesByType(\n\t\t\tpascalCase(\"Prismic\", options.typePrefix, customTypeModelID),\n\t\t) as PrismicDocumentNodeLike[];\n\n\t\tnodes.push(...nodesForType);\n\t}\n\n\tconst mutex = createMutex(\n\t\t`gatsby-plugin-prismic-previews:write-nodes:${options.repositoryName}`,\n\t);\n\n\tawait mutex.acquire();\n\n\t// Write models\n\t{\n\t\tconst fileName = await getPublicModelsFileName(args.cache);\n\t\tconst filePath = path.join(\"public\", \"static\", fileName);\n\n\t\tlet contents: Record<\n\t\t\tstring,\n\t\t\t{\n\t\t\t\tcustomTypeModels: CustomTypeModel[];\n\t\t\t\tsharedSliceModels: SharedSliceModel[];\n\t\t\t}\n\t\t> = {};\n\t\ttry {\n\t\t\tconst rawExistingContents = await fs.readFile(filePath, \"utf8\");\n\t\t\tcontents = JSON.parse(rawExistingContents);\n\t\t} catch {\n\t\t\t// noop\n\t\t}\n\n\t\tconst models: {\n\t\t\tcustomTypeModels: CustomTypeModel[];\n\t\t\tsharedSliceModels: SharedSliceModel[];\n\t\t} = await gatsbySourcePrismicCache.get(`${options.repositoryName}:models`);\n\n\t\tcontents[options.repositoryName] = models;\n\n\t\tawait fs.writeFile(filePath, JSON.stringify(contents));\n\t}\n\n\tawait mutex.release();\n};\n"],"names":["pascalCase","mutex","createMutex","getPublicModelsFileName","path","fs"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAgBA,MAAM,uBAAuB,CAAC,UAAqC;AAEjE,SAAA,MAAM,QAAQ,KAAK,KACnB,MAAM,MAAM,CAAC,YAAY,OAAO,YAAY,QAAQ;AAEtD;AAOa,MAAA,kBAAkB,OAC9B,MACA,YACkB;AACZ,QAAA,2BAA2B,KAAK,SAAS,uBAAuB;AAEtE,QAAM,qBAAqB,MAAM,yBAAyB,IACzD,GAAG,QAAQ,mCAAmC;AAE3C,MAAA,CAAC,qBAAqB,kBAAkB,GAAG;AACxC,UAAA,IAAI,MACT,gEAAgE;AAAA,EAEjE;AAED,QAAM,QAAmC,CAAA;AAEzC,aAAW,qBAAqB,oBAAoB;AAC7C,UAAA,eAAe,KAAK,eACzBA,WAAA,WAAW,WAAW,QAAQ,YAAY,iBAAiB,CAAC;AAGvD,UAAA,KAAK,GAAG,YAAY;AAAA,EAC1B;AAED,QAAMC,UAAQC,MAAAA,YACb,8CAA8C,QAAQ,gBAAgB;AAGvE,QAAMD,QAAM;AAGZ;AACC,UAAM,WAAW,MAAME,wBAAAA,wBAAwB,KAAK,KAAK;AACzD,UAAM,WAAWC,gBAAK,KAAK,UAAU,UAAU,QAAQ;AAEvD,QAAI,WAMA,CAAA;AACA,QAAA;AACH,YAAM,sBAAsB,MAAMC,cAAG,SAAS,UAAU,MAAM;AACnD,iBAAA,KAAK,MAAM,mBAAmB;AAAA,IAAA,QACxC;AAAA,IAED;AAED,UAAM,SAGF,MAAM,yBAAyB,IAAI,GAAG,QAAQ,uBAAuB;AAEhE,aAAA,QAAQ,cAAc,IAAI;AAEnC,UAAMA,cAAG,UAAU,UAAU,KAAK,UAAU,QAAQ,CAAC;AAAA,EACrD;AAED,QAAMJ,QAAM;AACb;;"}