UNPKG

@baseplate-dev/core-generators

Version:

Core generators for Baseplate

54 lines 2.34 kB
import { createGenerator, createGeneratorTask, createProviderType, } from '@baseplate-dev/sync'; import { stringifyPrettyCompact } from '@baseplate-dev/utils'; import { z } from 'zod'; import { packageScope } from '#src/providers/scopes.js'; import { TEMPLATE_PATHS_METADATA_FILE } from '#src/renderers/extractor/plugins/template-paths/template-paths.plugin.js'; const descriptorSchema = z.object({}); export const pathRootsProvider = createProviderType('path-roots'); /** * Metadata generator for path roots that writes the metadata * for path roots to the base of the project. */ export const pathRootsGenerator = createGenerator({ name: 'metadata/path-roots', generatorFileUrl: import.meta.url, descriptorSchema, buildTasks: () => ({ main: createGeneratorTask({ exports: { pathRoots: pathRootsProvider.export(packageScope), }, dependencies: {}, run() { const pathRoots = []; return { providers: { pathRoots: { registerPathRoot(pathRootName, outputRelativePath) { if (pathRoots.some((p) => p.canonicalPath === outputRelativePath)) { throw new Error(`Path root ${outputRelativePath} already registered`); } pathRoots.push({ canonicalPath: outputRelativePath, pathRootName, }); }, }, }, build: (builder) => { if (!builder.metadataOptions.includeTemplateMetadata) return; if (Object.keys(pathRoots).length === 0) return; builder.writeFile({ id: 'path-roots', destination: TEMPLATE_PATHS_METADATA_FILE, contents: stringifyPrettyCompact(pathRoots), }); }, }; }, }), }), }); //# sourceMappingURL=path-roots.generator.js.map