NestJS-CLI

参考:NestJS CLI 文档

概述

NestJS CLI 是一个命令行界面工具,用于创建、开发和维护 NestJS 应用程序。它提供了一系列命令来简化开发过程,包括生成项目结构、创建各种组件、构建和运行应用程序等。

安装

全局安装
# 使用 npm
npm install -g @nestjs/cli
验证安装
nest -v

核心功能

  • 项目生成:快速创建新的 NestJS 应用项目

  • 代码生成:生成各种 NestJS 组件并在项目中自动完成注册、配置等操作,如控制器、服务、模块等

  • 构建与运行:构建应用并运行开发服务器

  • 依赖管理:添加外部库支持

  • 项目信息:显示项目详情

基本命令

全局选项

选项

别名

描述

--version

-v

输出版本信息

--help

-h

输出使用信息

常用命令

命令

描述

new

生成新的 Nest 应用

build

构建 Nest 应用

start

运行 Nest 应用

info

显示 Nest 项目详情

add

为项目添加外部库支持

generate

生成 Nest 元素

命令格式

nest <command> [options] [arguments]

示例

# 创建新应用
nest new my-app

# 生成控制器
nest generate controller users

# 构建应用 
nest build

# 运行应用
nest start

工作区

工作区是一种项目结构,允许在单个仓库中管理多个 NestJS 应用程序和库。这种结构特别适合大型项目或由多个相关应用组成的系统。
cli工具支持标准模式与monorepo模式。

参考:NestJS 工作区文档

创建工作区

初始化工作区
# 创建新的工作区(标准模式)
nest new my-project

# 更新工作区(转换为monorepo模式)
cd my-project
# 创建新应用 my-app
nest generate app my-app
工作区结构

更新工作区(标准模式)后,项目结构将如下所示:

my-project/
├── apps/
|    └── my-app/
|    └── my-project/
├── package.json
├── tsconfig.json
├── tsconfig.base.json
└── nest-cli.json
  • apps/:存放应用程序的目录

  • package.json:工作区配置

  • tsconfig.base.json:TypeScript 基础配置

  • nest-cli.json:Nest CLI 配置

命令

参考:NestJS CLI 命令

# 查看所有可用命令
nest --help

项目中最主要使用的命令包括:
nest generate: 生成 Nest 元素,如控制器、服务、模块等。

$ nest generate <schematic> <name> [options]
$ nest g <schematic> <name> [options]
generate|g [options] <schematic> [name] [path]  Generate a Nest element.
  Schematics available on @nestjs/schematics collection:
    ┌───────────────┬─────────────┬──────────────────────────────────────────────┐
    │ name          │ alias       │ description                                  │
    │ application   │ application │ Generate a new application workspace         │
    │ class         │ cl          │ Generate a new class                         │
    │ configuration │ config      │ Generate a CLI configuration file            │
    │ controller    │ co          │ Generate a controller declaration            │
    │ decorator     │ d           │ Generate a custom decorator                  │
    │ filter        │ f           │ Generate a filter declaration                │
    │ gateway       │ ga          │ Generate a gateway declaration               │
    │ guard         │ gu          │ Generate a guard declaration                 │
    │ interceptor   │ itc         │ Generate an interceptor declaration          │
    │ interface     │ itf         │ Generate an interface                        │
    │ library       │ lib         │ Generate a new library within a monorepo     │
    │ middleware    │ mi          │ Generate a middleware declaration            │
    │ module        │ mo          │ Generate a module declaration                │
    │ pipe          │ pi          │ Generate a pipe declaration                  │
    │ provider      │ pr          │ Generate a provider declaration              │
    │ resolver      │ r           │ Generate a GraphQL resolver declaration      │
    │ resource      │ res         │ Generate a new CRUD resource                 │
    │ service       │ s           │ Generate a service declaration               │
    │ sub-app       │ app         │ Generate a new application within a monorepo │
    └───────────────┴─────────────┴──────────────────────────────────────────────┘

其他命令

  1. nest new: 生成新的 Nest 应用项目

  2. nest build: 构建 Nest 应用

  3. nest start: 运行 Nest 应用

  4. nest info: 显示 Nest 项目详情,包括开发环境、版本信息、依赖项等

  5. nest add: 为项目添加外部库支持

Commands:
  new|n [options] [name]                          Generate Nest application.
  build [options] [apps...]                       Build Nest application.
  start [options] [app]                           Run Nest application.
  info|i                                          Display Nest project details.
  add [options] <library>                         Adds support for an external library to your
                                                  project.

其他

除了上述命令,Nest-CLI还提供其他配置功能:库、脚本等