import type { SliceCreateHook, SliceCreateHookData, PluginSystemContext, } from "@prismicio/plugin-kit"; import { upsertGlobalTypeScriptTypes, writeSliceFile, writeSliceModel, } from "@prismicio/plugin-kit/fs"; import { stripIndent } from "common-tags"; import { rejectIfNecessary } from "../lib/rejectIfNecessary"; import { upsertSliceLibraryIndexFile } from "../lib/upsertSliceLibraryIndexFile"; import type { PluginOptions } from "../types"; type CreateComponentFileArgs = { data: SliceCreateHookData; } & PluginSystemContext; const createComponentFile = async ({ data, helpers, actions, options, }: CreateComponentFileArgs) => { const contents = data.componentContents ?? stripIndent` `; await writeSliceFile({ libraryID: data.libraryID, model: data.model, filename: "index.vue", contents, format: options.format, actions, helpers, }); }; export const sliceCreate: SliceCreateHook = async ( data, context, ) => { rejectIfNecessary( await Promise.allSettled([ writeSliceModel({ libraryID: data.libraryID, model: data.model, format: context.options.format, helpers: context.helpers, }), createComponentFile({ data, ...context }), ]), ); rejectIfNecessary( await Promise.allSettled([ upsertSliceLibraryIndexFile({ libraryID: data.libraryID, ...context, }), upsertGlobalTypeScriptTypes({ filename: context.options.generatedTypesFilePath, format: context.options.format, helpers: context.helpers, actions: context.actions, }), ]), ); };