import { Command } from 'commander'; import { AVAILABLE_BUILDERS } from '../lib/configuration/index.js'; import { ERROR_PREFIX } from '../lib/ui/index.js'; import { getRemainingFlags } from '../lib/utils/remaining-flags.js'; import { AbstractCommand } from './abstract.command.js'; import { StartCommandContext } from './context/index.js'; export class StartCommand extends AbstractCommand { public load(program: Command): void { const collect = (value: any, previous: any) => { return previous.concat([value]); }; program .command('start [app]') .allowUnknownOption() // Unknown options are collected into `args`, which commander otherwise // rejects as excess arguments once an app name is also present. .allowExcessArguments() .option('-c, --config [path]', 'Path to nest-cli configuration file.') .option('-p, --path [path]', 'Path to tsconfig file.') .option('-w, --watch', 'Run in watch mode (live-reload).') .option( '-b, --builder [name]', 'Builder to be used (tsc, webpack, swc, rspack).', ) .option('--watchAssets', 'Watch non-ts (e.g., .graphql) files mode.') .option( '-d, --debug [hostport] ', 'Run in debug mode (with --inspect flag).', ) .option( '--webpack', 'Use webpack for compilation (deprecated option, use --builder instead).', ) .option('--webpackPath [path]', 'Path to webpack configuration.') .option('--rspackPath [path]', 'Path to rspack configuration.') .option( '--type-check', 'Enable type checking (when SWC or rspack is used).', () => true, ) .option( '--no-type-check', 'Disable type checking (when SWC is used).', () => false, ) .option( '--bundle-dependencies', 'Bundle node_modules into the output (when rspack is used).', () => true, ) .option( '--no-bundle-dependencies', 'Leave node_modules out of the output (when rspack is used).', () => false, ) .option( '--emit-declarations', 'Emit declaration files (.d.ts) when using SWC builder.', ) .option('--silent', 'Suppress informational compiler logs.') .option('--tsc', 'Use typescript compiler for compilation.') .option( '--sourceRoot [sourceRoot]', 'Points at the root of the source code for the single project in standard mode structures, or the default project in monorepo mode structures.', ) .option( '--entryFile [entryFile]', "Path to the entry file where this command will work with. Defaults to the one defined at your Nest's CLI config file.", ) .option('-e, --exec [binary]', 'Binary to run (default: "node").') .option( '--preserveWatchOutput', 'Use "preserveWatchOutput" option when using tsc watch mode.', ) .option( '--shell', "Spawn child processes within a shell (see node's child_process.spawn() method docs). Default: true.", true, ) .option('--no-shell', 'Do not spawn child processes within a shell.') .option( '--env-file [path]', 'Path to an env file (.env) to be loaded into the environment.', collect, [], ) .description('Run Nest application.') .action( async (app: string, options: Record, command: Command) => { const isWebpackEnabled = options.tsc ? false : options.webpack; if ( options.builder !== undefined && !(AVAILABLE_BUILDERS as readonly string[]).includes(options.builder) ) { console.error( ERROR_PREFIX + ` Invalid builder option: ${ options.builder }. Available builders: ${AVAILABLE_BUILDERS.join(', ')}`, ); process.exit(1); } const context: StartCommandContext = { app, config: options.config, webpack: isWebpackEnabled, watch: !!options.watch, watchAssets: !!options.watchAssets, path: options.path, webpackPath: options.webpackPath, rspackPath: options.rspackPath, builder: options.tsc ? 'tsc' : options.builder, typeCheck: options.typeCheck, bundleDependencies: options.bundleDependencies, // Left undefined when the flag is absent so `getValueOrDefault` can // still fall through to `compilerOptions.emitDeclarations`. emitDeclarations: options.emitDeclarations, silent: !!options.silent, // Left undefined when the flag is absent so `WatchCompiler` can // fall through to the tsconfig 'preserveWatchOutput' option. preserveWatchOutput: !!options.watch && !isWebpackEnabled ? options.preserveWatchOutput : false, debug: options.debug, exec: options.exec, sourceRoot: options.sourceRoot, entryFile: options.entryFile, shell: !!options.shell, envFile: options.envFile ?? [], extraFlags: getRemainingFlags(command), }; try { await this.action.handle(context); } catch { process.exit(1); } }, ); } }