UNPKG

@nestjs/cli

Version:

The Nest CLI: create NestJS applications, generate modules, controllers and CRUD resources, and build, run and watch projects and monorepos.

127 lines (126 loc) • 6.86 kB
import { red } from 'ansis'; import * as path from 'path'; import { getValueOrDefault } from '../lib/compiler/helpers/get-value-or-default.js'; import { Collection, CollectionFactory, describeSkippedPrompt, getSkippedPrompts, SchematicOption, } from '../lib/schematics/index.js'; import { MESSAGES } from '../lib/ui/index.js'; import { isInteractive } from '../lib/utils/is-interactive.js'; import { loadConfiguration } from '../lib/utils/load-configuration.js'; import { askForProjectName, getSpecFileSuffix, hasTestingPackage, moveDefaultProjectToStart, shouldAskForProject, shouldGenerateFlat, shouldGenerateSpec, } from '../lib/utils/project-utils.js'; import { AbstractAction } from './abstract.action.js'; export class GenerateAction extends AbstractAction { async handle(context) { try { await generateFiles(context); } catch (error) { if (error && error.message) { console.error(red(error.message)); } throw error; } } } const generateFiles = async (context) => { const configuration = await loadConfiguration(); const collectionOption = context.collection; const schematic = context.schematic; let appName = context.project ?? ''; const specFileSuffix = context.specFileSuffix; const collectionName = collectionOption || configuration.collection || Collection.NESTJS; const collection = CollectionFactory.create(collectionName); const schematicOptions = mapContextToSchematicOptions(context); schematicOptions.push(new SchematicOption('language', configuration.language)); const configurationProjects = configuration.projects; let sourceRoot = appName ? getValueOrDefault(configuration, 'sourceRoot', appName) : configuration.sourceRoot; const specValue = typeof context.spec === 'boolean' ? context.spec : context.spec.value; const specPassedAsInput = typeof context.spec === 'boolean' ? false : context.spec.passedAsInput; // A spec file can't run without `@nestjs/testing`, so a project that doesn't // list it, like one from `nest new --minimal`, gets one only if it asks. const specDefault = specPassedAsInput === false ? specValue && hasTestingPackage() : specValue; const flatValue = context.flat !== undefined ? !!context.flat : false; const specFileSuffixValue = specFileSuffix; let generateSpec = shouldGenerateSpec(configuration, schematic, appName, specDefault, specPassedAsInput); let generateFlat = shouldGenerateFlat(configuration, appName, flatValue); let generateSpecFileSuffix = getSpecFileSuffix(configuration, appName, specFileSuffixValue); // If you only add a `lib` we actually don't have monorepo: true BUT we do have "projects" // Ensure we don't run for new app/libs schematics if (shouldAskForProject(schematic, configurationProjects, appName)) { const defaultLabel = ' [ Default ]'; let defaultProjectName = configuration.sourceRoot + defaultLabel; for (const property in configurationProjects) { if (configurationProjects[property].sourceRoot === configuration.sourceRoot) { defaultProjectName = property + defaultLabel; break; } } const projects = moveDefaultProjectToStart(configuration, defaultProjectName, defaultLabel); let selectedProjectName; if (isInteractive()) { selectedProjectName = (await askForProjectName(MESSAGES.PROJECT_SELECTION_QUESTION, projects)); } else { selectedProjectName = projects[0]; console.info(MESSAGES.PROJECT_SELECTION_DEFAULTED(selectedProjectName.replace(defaultLabel, ''))); } const project = selectedProjectName.replace(defaultLabel, ''); if (project !== configuration.sourceRoot) { sourceRoot = configurationProjects[project].sourceRoot; } appName = project; generateSpec = shouldGenerateSpec(configuration, schematic, project, specDefault, specPassedAsInput); generateFlat = shouldGenerateFlat(configuration, project, flatValue); generateSpecFileSuffix = getSpecFileSuffix(configuration, project, specFileSuffixValue); } const baseDir = getValueOrDefault(configuration, 'generateOptions.baseDir', appName); if (baseDir) { sourceRoot = path.join(sourceRoot, baseDir); } schematicOptions.push(new SchematicOption('sourceRoot', sourceRoot)); schematicOptions.push(new SchematicOption('spec', generateSpec)); schematicOptions.push(new SchematicOption('flat', generateFlat)); schematicOptions.push(new SchematicOption('specFileSuffix', generateSpecFileSuffix)); schematicOptions.push(new SchematicOption('format', context.format)); if (!schematic) { throw new Error('Unable to find a schematic for this configuration'); } if (isInteractive()) { await collection.execute(schematic, schematicOptions); return; } // The schematics binary only checks stdout, so with a terminal there but // nothing on stdin (a coding agent's shell) it would still ask, and fail. const skipped = await getSkippedPrompts(collectionName, schematic, schematicOptions); if (skipped.some(({ name, default: value }) => name === 'name' && value === undefined)) { throw new Error(MESSAGES.SCHEMATIC_NAME_REQUIRED(schematic)); } if (skipped.length > 0) { console.info(MESSAGES.SCHEMATIC_PROMPTS_DEFAULTED(skipped.map(describeSkippedPrompt))); } await collection.execute(schematic, schematicOptions, '--no-interactive'); }; const mapContextToSchematicOptions = (context) => { const options = []; // Only include fields that schematics expect; exclude those handled separately if (context.name !== undefined) options.push(new SchematicOption('name', context.name)); if (context.path !== undefined) options.push(new SchematicOption('path', context.path)); if (context.dryRun) options.push(new SchematicOption('dry-run', true)); if (context.collection !== undefined) options.push(new SchematicOption('collection', context.collection)); if (context.project !== undefined) options.push(new SchematicOption('project', context.project)); if (context.skipImport !== undefined) options.push(new SchematicOption('skipImport', context.skipImport)); if (context.type !== undefined) options.push(new SchematicOption('type', context.type)); if (context.crud !== undefined) options.push(new SchematicOption('crud', context.crud)); if (context.prefix !== undefined) options.push(new SchematicOption('prefix', context.prefix)); // 'schematic', 'spec', 'flat', 'specFileSuffix' are handled separately return options; };