import { definePlugin } from "@prismicio/plugin-kit"; import { deleteCustomTypeDirectory, deleteSliceDirectory, readCustomTypeLibrary, readCustomTypeModel, readSliceLibrary, readSliceModel, renameCustomType, renameSlice, upsertGlobalTypeScriptTypes, writeCustomTypeModel, writeSliceModel, } from "@prismicio/plugin-kit/fs"; import { name as pkgName } from "../package.json"; import { projectInit } from "./hooks/project-init"; import { sliceCreate } from "./hooks/slice-create"; import { rejectIfNecessary } from "./lib/rejectIfNecessary"; import { upsertSliceLibraryIndexFile } from "./lib/upsertSliceLibraryIndexFile"; import { PluginOptions } from "./types"; export const plugin = definePlugin({ meta: { name: pkgName, }, defaultOptions: { format: true, lazyLoadSlices: true, }, setup({ hook }) { //////////////////////////////////////////////////////////////// // project:* //////////////////////////////////////////////////////////////// hook("project:init", projectInit); //////////////////////////////////////////////////////////////// // slice:* //////////////////////////////////////////////////////////////// hook("slice:create", sliceCreate); hook("slice:update", async (data, context) => { await writeSliceModel({ libraryID: data.libraryID, model: data.model, ...context, }); await upsertGlobalTypeScriptTypes({ filename: context.options.generatedTypesFilePath, format: context.options.format, ...context, }); }); hook("slice:rename", async (data, context) => { await renameSlice({ libraryID: data.libraryID, model: data.model, format: context.options.format, ...context, }); rejectIfNecessary( await Promise.allSettled([ upsertSliceLibraryIndexFile({ libraryID: data.libraryID, ...context, }), upsertGlobalTypeScriptTypes({ filename: context.options.generatedTypesFilePath, format: context.options.format, ...context, }), ]), ); }); hook("slice:delete", async (data, context) => { await deleteSliceDirectory({ libraryID: data.libraryID, model: data.model, ...context, }); rejectIfNecessary( await Promise.allSettled([ upsertSliceLibraryIndexFile({ libraryID: data.libraryID, ...context, }), upsertGlobalTypeScriptTypes({ filename: context.options.generatedTypesFilePath, format: context.options.format, ...context, }), ]), ); }); hook("slice:read", async (data, context) => { return await readSliceModel({ libraryID: data.libraryID, sliceID: data.sliceID, ...context, }); }); //////////////////////////////////////////////////////////////// // slice-library:* //////////////////////////////////////////////////////////////// hook("slice-library:read", async (data, context) => { return await readSliceLibrary({ libraryID: data.libraryID, ...context, }); }); //////////////////////////////////////////////////////////////// // custom-type:* //////////////////////////////////////////////////////////////// hook("custom-type:create", async (data, context) => { await writeCustomTypeModel({ model: data.model, format: context.options.format, ...context, }); await upsertGlobalTypeScriptTypes({ filename: context.options.generatedTypesFilePath, format: context.options.format, ...context, }); }); hook("custom-type:update", async (data, context) => { await writeCustomTypeModel({ model: data.model, format: context.options.format, ...context, }); await upsertGlobalTypeScriptTypes({ filename: context.options.generatedTypesFilePath, format: context.options.format, ...context, }); }); hook("custom-type:rename", async (data, context) => { await renameCustomType({ model: data.model, format: context.options.format, ...context, }); await upsertGlobalTypeScriptTypes({ filename: context.options.generatedTypesFilePath, format: context.options.format, ...context, }); }); hook("custom-type:delete", async (data, context) => { await deleteCustomTypeDirectory({ customTypeID: data.model.id, ...context, }); await upsertGlobalTypeScriptTypes({ filename: context.options.generatedTypesFilePath, format: context.options.format, ...context, }); }); hook("custom-type:read", async (data, context) => { return await readCustomTypeModel({ customTypeID: data.id, ...context, }); }); //////////////////////////////////////////////////////////////// // custom-type-library:* //////////////////////////////////////////////////////////////// hook("custom-type-library:read", async (_data, context) => { return await readCustomTypeLibrary({ helpers: context.helpers, }); }); }, });