YargsCLI脚手架生成器Skill yargs-scaffolder

Yargs CLI脚手架生成器是一个专业的命令行工具开发辅助技能,用于快速生成基于Yargs框架的TypeScript CLI应用程序。该工具支持命令模块化设计、中间件集成、参数验证和类型安全,帮助开发者快速搭建符合最佳实践的CLI项目结构。关键词:Yargs脚手架、CLI开发、命令行工具、TypeScript、中间件、命令模块、参数解析、Node.js开发、工具链自动化。

后端开发 0 次安装 0 次浏览 更新于 2/23/2026

名称: yargs脚手架工具 描述: 生成基于Yargs的CLI应用程序,支持命令、位置参数、中间件和TypeScript。创建具有现代模式的完整脚手架CLI应用程序。 允许的工具: 读取、写入、编辑、Bash、Glob、Grep

Yargs脚手架工具

生成一个完整的Yargs CLI应用程序,支持TypeScript、中间件和最佳实践。

功能

  • 生成基于TypeScript的Yargs CLI项目
  • 创建带有位置参数的命令模块
  • 设置常见操作的中间件(日志记录、配置加载)
  • 配置类型转换和验证
  • 实现严格模式和失败处理程序
  • 设置构建和开发工作流

使用场景

在以下情况下调用此技能:

  • 使用Yargs引导新的CLI应用程序
  • 创建采用命令模块模式的CLI
  • 设置基于中间件的处理
  • 配置复杂的参数解析

输入参数

参数 类型 是否必需 描述
项目名称 字符串 CLI项目的名称(kebab-case格式)
描述 字符串 CLI的简短描述
命令 数组 要脚手架的命令列表
typescript 布尔值 使用TypeScript(默认:true)
包管理器 字符串 npm、yarn或pnpm(默认:npm)
严格模式 布尔值 启用严格模式(默认:true)

命令结构

{
  "commands": [
    {
      "name": "serve",
      "description": "启动服务器",
      "aliases": ["s"],
      "positional": [
        { "name": "port", "type": "number", "default": 3000 }
      ],
      "options": [
        { "name": "host", "type": "string", "default": "localhost" },
        { "name": "watch", "type": "boolean", "alias": "w" }
      ]
    }
  ]
}

输出结构

<项目名称>/
├── package.json
├── tsconfig.json
├── .gitignore
├── README.md
├── src/
│   ├── index.ts              # 入口点
│   ├── cli.ts                # Yargs设置
│   ├── commands/
│   │   ├── index.ts          # 命令导出
│   │   └── <命令>.ts         # 命令模块
│   ├── middleware/
│   │   ├── logger.ts         # 日志记录中间件
│   │   └── config.ts         # 配置加载中间件
│   ├── utils/
│   │   └── helpers.ts        # 辅助工具
│   └── types/
│       └── index.ts          # 类型定义
└── tests/
    └── commands/
        └── <命令>.test.ts

生成的代码模式

主CLI入口(src/cli.ts)

import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import * as commands from './commands';
import { loggerMiddleware } from './middleware/logger';

export const cli = yargs(hideBin(process.argv))
  .scriptName('<项目名称>')
  .usage('$0 <cmd> [args]')
  .middleware([loggerMiddleware])
  .command(commands.serveCommand)
  .command(commands.buildCommand)
  .demandCommand(1, '您至少需要一个命令')
  .strict()
  .fail((msg, err, yargs) => {
    if (err) throw err;
    console.error(msg);
    console.error(yargs.help());
    process.exit(1);
  })
  .help()
  .alias('help', 'h')
  .version()
  .alias('version', 'v')
  .wrap(Math.min(120, process.stdout.columns || 80));

命令模块模板

import { CommandModule, Argv } from 'yargs';

interface ServeArgs {
  port: number;
  host: string;
  watch: boolean;
}

export const serveCommand: CommandModule<{}, ServeArgs> = {
  command: 'serve [port]',
  aliases: ['s'],
  describe: '启动开发服务器',
  builder: (yargs: Argv) => {
    return yargs
      .positional('port', {
        type: 'number',
        default: 3000,
        describe: '监听的端口'
      })
      .option('host', {
        type: 'string',
        default: 'localhost',
        describe: '绑定的主机'
      })
      .option('watch', {
        alias: 'w',
        type: 'boolean',
        default: false,
        describe: '启用监视模式'
      });
  },
  handler: async (argv) => {
    console.log(`在 ${argv.host}:${argv.port} 上启动服务器`);
  }
};

依赖项

{
  "dependencies": {
    "yargs": "^17.0.0"
  },
  "devDependencies": {
    "@types/node": "^20.0.0",
    "@types/yargs": "^17.0.0",
    "typescript": "^5.0.0",
    "tsx": "^4.0.0",
    "vitest": "^1.0.0"
  }
}

工作流程

  1. 验证输入 - 检查项目名称、命令结构
  2. 创建目录结构 - 设置文件夹和基础文件
  3. 生成package.json - 配置项目元数据
  4. 生成tsconfig.json - TypeScript配置
  5. 创建CLI入口点 - 带有中间件的Yargs设置
  6. 生成命令模块 - 单独的命令文件
  7. 创建中间件 - 日志记录器、配置中间件
  8. 设置测试 - 命令的测试结构
  9. 初始化git - 可选的git初始化

应用的最佳实践

  • 启用TypeScript严格模式
  • 命令模块模式,便于扩展
  • 中间件处理横切关注点
  • 带有自定义失败处理程序的严格模式
  • 参数的适当类型定义
  • 支持补全脚本

参考

目标流程

  • cli-application-bootstrap
  • argument-parser-setup
  • cli-command-structure-design