docfy generate

扫描你的项目,为每个控制器生成预填充好的 *.controller.docs.ts,仅做静态分析,不执行任何项目代码。

用法

bash
npx nestjs-docfy generate [options]

选项

OptionDefaultDescription
--root <path>.Project root directory
--tsconfig <path>auto-detectedPath to tsconfig.json
--pattern <glob>**/*.controller.tsGlob pattern to find controllers
--out <path>alongside each controllerOutput directory for generated files
--forcefalseMerge new methods into existing docs files (preserves user edits)
--overwritefalseDiscard existing docs file content and regenerate it from scratch (takes precedence over --force)
--dry-runfalsePrint what would be generated without writing files
--quietfalseSuppress all output except errors (CI-friendly)
--formattsOutput format: ts or js
--watchfalseRe-generate on controller file changes
--register-pluginfalseIf webpack: true is set without the CLI plugin, add nestjs-docfy to nest-cli.json's compilerOptions.plugins. See The CLI plugin.
--link-controllerfalseInsert @WithDocs() and its import into each controller automatically (opt-in, mutates controller source; idempotent — safe to run repeatedly).

项目类型

CLI 会自动识别你项目的结构,不需要任何配置:

LayoutDetected when
Simple projecttsconfig.json at root, no monorepo markers
Nx monoreponx.json present
Nest CLI monoreponest-cli.json with "monorepo": true
Generic monorepopackages/ or apps/ with sub-package.json files

幂等性:--force 与 --overwrite

ScenarioBehavior
Run generate on a clean projectCreates all docs files
Run generate again (no changes)Skips all existing files, safe to run repeatedly
Add a new endpoint, run generate --forceMerges new method block, preserves existing arrays
Edit a method's decorators, run --forceYour edits are preserved
Run generate --overwriteDiscards the file entirely, regenerates from scratch

npm script

为了方便,可以加进你的 package.json

json
{
  "scripts": {
    "docs:generate": "nestjs-docfy generate",
    "docs:preview": "nestjs-docfy generate --dry-run"
  }
}