UNPKG

@baseplate-dev/core-generators

Version:

Core generators for Baseplate

96 lines 3.36 kB
import { createGenerator, createGeneratorTask } from '@baseplate-dev/sync'; import { stringifyPrettyStable } from '@baseplate-dev/utils'; import { z } from 'zod'; import { CORE_PACKAGES } from '#src/constants/core-packages.js'; import { packageScope } from '#src/providers/scopes.js'; import { nodeProvider } from '../node/node.generator.js'; const taskSchema = z.object({ /** * Name of the task (e.g., 'build', 'test', 'lint') */ name: z.string().min(1), /** * Tasks that this task depends on * @example ['^build'] - depends on build task in dependencies */ dependsOn: z.array(z.string()).optional(), /** * Input files/globs that invalidate the cache when changed * @example ['src/**', 'package.json'] */ inputs: z.array(z.string()).optional(), /** * Output files/directories generated by this task * @example ['dist/**', '.next/**'] */ outputs: z.array(z.string()).optional(), /** * Whether to cache the task outputs * @default true */ cache: z.boolean().default(true), /** * Whether this is a long-running task (like dev server) */ persistent: z.boolean().optional(), }); const descriptorSchema = z.object({ /** * Array of tasks to configure in turbo.json */ tasks: z.array(taskSchema).default([]), }); /** * Generator for Turborepo configuration * * Creates turbo.json and adds turbo package to devDependencies. * Also adds convenience scripts to package.json for each task. * * This generator should only be used with root packages in a monorepo. */ export const turboGenerator = createGenerator({ name: 'node/turbo', generatorFileUrl: import.meta.url, descriptorSchema, scopes: [packageScope], buildTasks: (descriptor) => ({ main: createGeneratorTask({ dependencies: { node: nodeProvider, }, run({ node }) { // Add turbo to dev dependencies node.packages.addPackages({ dev: { turbo: CORE_PACKAGES.turbo, }, }); return { build: (builder) => { // Build turbo.json tasks configuration const tasks = Object.fromEntries(descriptor.tasks.map((task) => [ task.name, { dependsOn: task.dependsOn, inputs: task.inputs, outputs: task.outputs, cache: task.cache ? undefined : false, persistent: task.persistent, }, ])); const turboConfig = { $schema: 'https://turbo.build/schema.json', tasks, }; builder.writeFile({ id: 'turbo-config', destination: 'turbo.json', contents: stringifyPrettyStable(turboConfig), }); }, }; }, }), }), }); //# sourceMappingURL=turbo.generator.js.map