Nest CLI

repository·master·Indexed 24 days ago

https://github.com/nestjs/nest-cli

A command-line tool for initializing, developing, and maintaining NestJS applications. The Nest CLI provides capabilities for scaffolding new projects and components via schematics, serving applications in development mode, and building or bundling applications for production using builders such as tsc, webpack, and swc.

Tokens
3.8K
Snippets
3
Records
25
Agent score
80%

What's inside @nestjs/cli

  1. Overview of Nest CLI capabilities

    master

    The Nest CLI is a command-line interface tool designed to assist with the full lifecycle of a Nest application. Its primary functions include:

    • Scaffolding: Initializing new projects and generating code components using schematics.
    • Development: Serving the application in development mode.
    • Production: Building and bundling the application for production distribution.

    The CLI utilizes schematics and includes built-in support for the @nestjs/schematics collection to encourage best-practice architectural patterns.

  2. Configure compilation builders for `nest start`

    master

    You can specify which compiler to use for your Nest application via the --builder flag. The available builders are:

    • tsc: The standard TypeScript compiler.
    • webpack: Uses webpack for compilation (Note: the --webpack flag is deprecated in favor of --builder webpack).
    • swc: Uses the Speed Of Light Compiler (SWC).

    When using swc, you can also enable type checking using the --type-check flag.

  3. Watch static assets during development

    master

    The Nest CLI can automatically re-copy static assets to your output directory whenever they change. This is controlled by two mechanisms:

    1. Global Watch Setting: Setting compilerOptions.watchAssets: true in nest-cli.json enables watching for all assets defined in the assets array.
    2. Per-Asset Watch Setting: Setting watchAssets: true within a specific asset object allows you to enable watching only for that specific group of files.

    When running nest build --watch, the CLI uses chokidar to monitor the specified files and performs the following actions:

    • Add/Change: Copies the file to the destination directory.
    • Unlink (Delete): Removes the corresponding file from the destination directory.

    Note: If watchAssets is disabled, assets are only copied once at the start of the build process.

  4. Use the Nest CLI

    master

    The Nest CLI is a command-line tool used to manage NestJS projects. You can invoke it using the nest command.

    Basic usage follows the pattern: nest <command> [options]

    If you run the command without any arguments, the CLI will output the help information automatically.

  5. Configure PluginMetadataGenerator options

    master

    The PluginMetadataGenerateOptions interface defines the configuration required to run the PluginMetadataGenerator. This is used to traverse the TypeScript AST and generate metadata for Nest CLI plugins.

    Key options include:

    • visitors: An array of ReadonlyVisitor instances used to traverse the AST and collect metadata.
    • outputDir: The directory where the generated metadata files will be written.
    • watch: (Optional) Whether to watch the project for changes.
    • tsconfigPath: (Optional) The path to the tsconfig file, relative to the current working directory.
    • filename: (Optional) The specific filename to write the metadata to.
    • tsProgramRef: (Optional) An existing ts.Program instance to use instead of initializing a new one.
    • printDiagnostics: (Optional) Whether to print TypeScript diagnostics to the console. Defaults to true.
    export interface PluginMetadataGenerateOptions {
      visitors: ReadonlyVisitor[];
      outputDir: string;
      watch?: boolean;
      tsconfigPath?: string;
      filename?: string;
      tsProgramRef?: ts.Program;
      printDiagnostics?: boolean;
    }
  6. Configure generation options

    master

    The generateOptions property in nest-cli.json configures the behavior of the nest generate command.

    Available Options

    • spec: Boolean or Record<string, boolean> to control the generation of test files.
    • flat: Boolean; if true, files are generated without a nested directory structure.
    • specFileSuffix: The suffix used for specification (test) files.
    • baseDir: The base directory for generated files.
  7. Configure assets for the build process

    master

    You can specify static assets to be included in the build using the assets array in compilerOptions. An asset can be a simple string (glob pattern) or an AssetEntry object for more control.

    AssetEntry Options

    • glob: The pattern to match files.
    • include: Optional pattern to include specific files.
    • exclude: Optional pattern to exclude specific files.
    • flat: Boolean; if true, files are copied without their directory structure.
    • outDir: The destination directory for the assets.
    • watchAssets: Boolean; if true, the CLI will watch these assets for changes.
  8. Configure compiler options and builders

    master

    The compilerOptions object controls how NestJS compiles your code. You can specify a builder to choose between tsc, swc, or webpack.

    Builder Variants

    • tsc: Uses the TypeScript compiler. Can be configured via configPath.
    • swc: Uses the Speed-up Compiler. Options include swcrcPath, outDir, filenames, sync, extensions, copyFiles, includeDotfiles, and quiet.
    • webpack: Uses Webpack. Can be configured via configPath.

    Other Compiler Options

    • tsConfigPath: Path to the tsconfig.json file.
    • plugins: An array of plugin names or PluginOptions objects.
    • assets: An array of Asset (string or AssetEntry) to be copied during build.
    • deleteOutDir: Boolean to delete the output directory before building.
    • manualRestart: Boolean to control automatic restart behavior.

    Note: webpack and webpackConfigPath are deprecated. Use builder instead.

  9. Configure the nest-cli.json structure

    master

    The nest-cli.json file defines how the Nest CLI manages your project. It can be configured as a single project or as a monorepo containing multiple projects.

    Top-level keys include:

    • language: The programming language used.
    • monorepo: Boolean indicating if the workspace is a monorepo.
    • projects: An object where keys are project names and values are ProjectConfiguration objects.
    • compilerOptions: Global compiler settings applied to all projects.
    • sourceRoot: The root directory for source files.
    • entryFile: The entry point file (e.g., main.ts).
  10. Configure static assets in nest-cli.json

    master

    You can specify static assets to be copied to the output directory during the build process using the compilerOptions.assets configuration key in your nest-cli.json.

    Assets can be defined as simple strings (representing a path to be included) or as objects for more granular control. When using objects, you can specify which files to include, which to exclude, the destination directory, and whether to watch for changes.

    Asset Configuration Options

    • include (string): The glob pattern or path to include. (Required for object configuration)
    • exclude (string): A glob pattern to exclude from the inclusion.
    • outDir (string): The destination directory within the build output folder. If not specified, it defaults to the project's output directory.
    • watchAssets (boolean): If true, the CLI will watch these specific assets for changes and re-copy them automatically during nest build --watch.
    • flat (boolean): Deprecated. Controls whether the directory structure is preserved.
  11. Use PluginMetadataGenerator to generate plugin metadata

    master

    The PluginMetadataGenerator class is responsible for traversing the project's AST via provided visitors and printing the collected metadata to the filesystem. You can instantiate it and call the generate method with a configuration object.

    If a tsProgramRef is provided, it uses that program directly. Otherwise, it uses a TypeCheckerHost to manage the TypeScript program lifecycle, supporting watch mode and automatic re-generation on changes.

    const generator = new PluginMetadataGenerator();
    generator.generate({
     visitors: [
       new ReadonlyVisitor({ introspectComments: true, pathToSource: __dirname }),
     ],
     outputDir: __dirname,
     watch: true,
     tsconfigPath: 'tsconfig.build.json',
    });