xiaohongshu
xiaohongshu
cli-creator
创建高质量 Node.js CLI 工具的完整指南。当用户需要构建命令行工具时使用:(1)创建新的 CLI 项目,(2)选择合适的框架和依赖,(3)实现 CLI 功能,(4)配置测试和打包,(5)应用 CLI 最佳实践
Full skill instructions
创建高质量、现代化的 Node.js CLI 工具。
本技能提供从零开始创建专业级 CLI 工具的完整工作流,包括框架选择、项目初始化、功能实现、测试配置和打包分发。
# 使用本技能的初始化脚本
npx ts-node skills/cli-creator/scripts/init_cli.ts my-cli
# 进入项目目录
cd my-cli
# 运行 CLI
npx . --help
npx . --version
用户请求创建 CLI
│
├─ 步骤 1: 框架选择
│ ├─ 评估项目复杂度 (命令数量、交互需求、插件需求)
│ ├─ 参考 references/framework_comparison.md
│ └─ 推荐: Commander.js (默认) / oclif / Ink
│
├─ 步骤 2: 项目初始化
│ ├─ 运行 scripts/init_cli.ts 生成项目结构
│ ├─ 选择模板级别 (minimal/standard/advanced)
│ └─ 配置项目元数据 (名称、描述、版本)
│
├─ 步骤 3: 开发指导
│ ├─ 实现命令逻辑 (src/commands/)
│ ├─ 配置 UI 增强 (chalk, ora)
│ ├─ 添加配置管理 (cosmiconfig + zod)
│ └─ 参考 references/best_practices.md
│
├─ 步骤 4: 测试和打包
│ ├─ 配置测试 (vitest)
│ ├─ 运行 scripts/validate_cli.ts 验证
│ ├─ 参考 references/testing_strategies.md
│ └─ 参考 references/packaging_guide.md
│
└─ 步骤 5: 发布分发
├─ npm publish (推荐)
├─ npx 一键运行
└─ 可选: Node.js SEA 单文件打包
CLI 项目初始化主脚本。
用法:
npx ts-node skills/cli-creator/scripts/init_cli.ts <cli-name> [options]
选项:
--framework <name>: 指定框架 (commander/oclif/yargs/ink/citty/cac)--template <type>: 模板级别 (minimal/standard/advanced)--ui: 包含 UI 库 (chalk + ora)--config: 包含配置管理 (cosmiconfig + zod)--testing: 包含测试配置 (vitest)示例:
# 最小化 CLI (Commander.js)
npx ts-node skills/cli-creator/scripts/init_cli.ts my-tool
# 标准配置,包含 UI 和测试
npx ts-node skills/cli-creator/scripts/init_cli.ts my-tool --template standard --ui --testing
# 高级配置,oclif 框架
npx ts-node skills/cli-creator/scripts/init_cli.ts my-tool --framework oclif --template advanced
# React UI CLI (Ink)
npx ts-node skills/cli-creator/scripts/init_cli.ts my-tool --framework ink
基于配置生成完整项目结构。
自动调用: 由 init_cli.ts 内部调用
根据配置安装精确的依赖版本。
自动调用: 由 init_cli.ts 内部调用
验证生成的 CLI 项目完整性。
用法:
npx ts-node skills/cli-creator/scripts/validate_cli.ts <project-path>
验证检查:
何时读取: 用户询问"哪个框架最好?"或需要框架对比时
# 搜索关键词
grep -r "framework comparison" skills/cli-creator/references/
grep -r "Commander.js vs oclif" skills/cli-creator/references/
文件: references/framework_comparison.md
何时读取: 实现 CLI 功能或改进用户体验时
# 搜索关键词
grep -r "UX principles" skills/cli-creator/references/
grep -r "POSIX" skills/cli-creator/references/
grep -r "error handling" skills/cli-creator/references/
文件: references/best_practices.md
何时读取: 选择和配置依赖时
# 搜索关键词
grep -r "chalk" skills/cli-creator/references/
grep -r "vitest" skills/cli-creator/references/
grep -r "dependencies" skills/cli-creator/references/
文件: references/dependency_guide.md
何时读取: 配置测试或调试测试问题时
# 搜索关键词
grep -r "vitest" skills/cli-creator/references/
grep -r "test" skills/cli-creator/references/
grep -r "coverage" skills/cli-creator/references/
文件: references/testing_strategies.md
何时读取: 准备发布 CLI 时
# 搜索关键词
grep -r "npm publish" skills/cli-creator/references/
grep -r "SEA" skills/cli-creator/references/
grep -r "npx" skills/cli-creator/references/
文件: references/packaging_guide.md
何时读取: 需要深入技术细节或查阅完整库列表时
# 搜索关键词
grep -r "execa" skills/cli-creator/references/
grep -r "zod" skills/cli-creator/references/
grep -r "cosmiconfig" skills/cli-creator/references/
文件: references/original_reference.md
| 框架 | 默认 | 特点 | 推荐场景 |
|---|---|---|---|
| Commander.js | ✅ | 简单、流行、社区成熟 | 中小型 CLI,快速开发 |
| oclif | - | 企业级、插件系统、自动文档 | 大型 CLI、插件化需求 |
| Yargs | - | 功能丰富、中间件支持 | 复杂参数验证 |
| Ink | - | React 组件化、富交互 | 现代 UI、交互式 CLI |
| citty | - | 轻量、现代 ESM | UnJS 生态 |
| cac | - | 极简、零依赖 | 超轻量级工具 |
项目复杂度评估:
├── 1-3 个简单命令 → Commander.js / cac
├── 3-10 个命令,中等复杂 → Commander.js / Yargs
├── 10+ 个命令,需要插件 → oclif
├── 富 UI/交互式 → Ink
└── 极简/零依赖追求 → cac / citty
特点: ~5 文件,快速开始 适用: 学习、原型、简单脚本 内容:
特点: ~15 文件,标准开发流程 适用: 大多数 CLI 项目 内容:
特点: ~25 文件,企业级质量 适用: 大型项目、团队协作 内容:
用户: "帮我创建一个 CLI 工具,叫 file-organizer"
Claude:
1. 收集配置信息 (名称、描述、功能)
2. 推荐框架 (默认 Commander.js)
3. 运行 init_cli.ts
4. 生成项目结构
5. 安装依赖
6. 提供使用指南
用户: "Commander.js 和 oclif 哪个更好?"
Claude:
1. 加载 framework_comparison.md
2. 提供对比分析
3. 根据用户需求推荐
用户: "我的 CLI 测试失败了"
Claude:
1. 加载 testing_strategies.md
2. 诊断问题
3. 提供解决方案
用户: "我想在我的 CLI 中添加配置文件支持"
Claude:
1. 加载 dependency_guide.md
2. 推荐 cosmiconfig + zod
3. 提供实现示例
{
"dependencies": {
"commander": "^12.0.0",
"chalk": "^5.3.0",
"ora": "^8.0.0"
},
"devDependencies": {
"typescript": "^5.6.0",
"tsx": "^4.19.0",
"vitest": "^2.1.0",
"@biomejs/biome": "^1.9.0",
"@types/node": "^22.0.0"
}
}
{
"dependencies": {
"cosmiconfig": "^9.0.0",
"zod": "^3.23.0",
"execa": "^9.5.0"
}
}
my-cli/
├── src/
│ ├── commands/ # 命令实现
│ │ ├── init.ts
│ │ └── build.ts
│ ├── lib/ # 共享库
│ │ ├── config.ts # 配置管理
│ │ ├── logger.ts # 日志工具
│ │ └── api.ts # API客户端
│ ├── utils/ # 工具函数
│ └── index.ts # 入口
├── test/ # 测试文件
├── bin/
│ └── run.js # 可执行文件
├── package.json
├── tsconfig.json
├── vitest.config.ts
└── README.md
生成的 CLI 项目包含以下 npm scripts:
{
"scripts": {
"dev": "tsx watch src/index.ts",
"build": "tsdown src/index.ts --format cjs,esm",
"test": "vitest",
"test:coverage": "vitest run --coverage",
"lint": "biome check .",
"format": "biome format . --write",
"typecheck": "tsc --noEmit"
}
}
在发布 CLI 前,确保:
--help 显示帮助信息--version 显示版本号NO_COLOR 环境变量xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience