子命令实现
何时使用
为 DevKeel CLI 新增一个子命令时(如 harness <new-cmd>)。
流程
Step 1: 注册命令
在 src/index.ts 中添加:
import { runNewCmd } from './commands/new-cmd.js'
program
.command('new-cmd')
.description('中文功能描述')
.option('--flag', '选项说明')
.action((opts) => runNewCmd(opts))
Step 2: 实现命令层
创建 src/commands/new-cmd.ts:
import * as p from '@clack/prompts'
import { someLogic } from '../lib/new-cmd.js'
export async function runNewCmd(opts?: { flag?: boolean }): Promise<void> {
p.intro('harness new-cmd')
// 交互 + 调用 lib
p.outro('完成')
}
命令层只做:读参数 → 交互 → 调用 lib → 输出结果。
Step 3: 实现逻辑层
创建 src/lib/new-cmd.ts:
- 纯函数,不引入
@clack/prompts - 文件操作前
fs.existsSync守卫 - 返回 plain object 或 null,不 throw
Step 4: 编写测试
创建 tests/new-cmd.test.ts:
- 临时目录隔离(
fs.mkdtempSync) - 真实文件系统验证
- 覆盖正常路径 + 缺失输入 + 幂等性
Step 5: 验证
pnpm lint && pnpm test && pnpm build
node bin/devkeel.js new-cmd --help
Checklist
-
src/index.ts注册命令 -
src/commands/new-cmd.ts交互层 -
src/lib/new-cmd.ts逻辑层(如有复杂逻辑) -
tests/new-cmd.test.ts测试 -
pnpm lint+pnpm test通过
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |