claude-apprentice 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/CHANGELOG.md +112 -0
  2. package/LICENSE +21 -0
  3. package/README.md +311 -0
  4. package/bin/apprentice.js +196 -0
  5. package/install.sh +92 -0
  6. package/package.json +38 -0
  7. package/templates/CLAUDE.md +60 -0
  8. package/templates/commands/backend.md +18 -0
  9. package/templates/commands/frontend.md +18 -0
  10. package/templates/commands/fullstack.md +18 -0
  11. package/templates/commands/scan-todos.md +123 -0
  12. package/templates/commands/spec.md +88 -0
  13. package/templates/memory/architecture.md +55 -0
  14. package/templates/memory/backend-standards.md +84 -0
  15. package/templates/memory/business-logic.md +59 -0
  16. package/templates/memory/frontend-standards.md +89 -0
  17. package/templates/memory/issues.md +34 -0
  18. package/templates/memory/learned-lessons.md +78 -0
  19. package/templates/memory/superpowers-config.md +46 -0
  20. package/templates/rules/INDEX.md +63 -0
  21. package/templates/rules/coding-standards.md +25 -0
  22. package/templates/rules/git-safety.md +9 -0
  23. package/templates/rules/superpowers-workflow.md +15 -0
  24. package/templates/scripts/auto-review.sh +77 -0
  25. package/templates/scripts/health-check.sh +189 -0
  26. package/templates/scripts/init.sh +2476 -0
  27. package/templates/settings.json +23 -0
  28. package/templates/skills/backend-workflow.md +87 -0
  29. package/templates/skills/code-review/SKILL.md +253 -0
  30. package/templates/skills/code-review/standards.md +189 -0
  31. package/templates/skills/frontend-workflow.md +75 -0
  32. package/templates/skills/fullstack-workflow.md +121 -0
  33. package/templates/specs/SPEC-GUIDE.md +99 -0
  34. package/templates/specs/active/README.md +7 -0
  35. package/templates/specs/archived/README.md +10 -0
  36. package/templates/usage-guides/README.md +84 -0
  37. package/templates/usage-guides/bottleneck-navigation.md +146 -0
  38. package/templates/usage-guides/usage-guide-v5.8.md +1261 -0
  39. package/templates/workflow/WORKFLOW-GUIDE.md +78 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,112 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ---
9
+
10
+ ## [Unreleased]
11
+
12
+ ### Planned
13
+ - Team collaboration features (shared spec workspace)
14
+ - Cursor native support
15
+ - Web UI generator (configure .claude/ visually)
16
+
17
+ ---
18
+
19
+ ## [1.0.0] — 2026-06-22
20
+
21
+ First public release. Open-sourced from internal v5.7.
22
+
23
+ ### Added — Core Workflow
24
+ - **Spec-driven development** (`commands/spec.md`)
25
+ - Lifecycle: PROPOSE → APPLY → SHIP → ARCHIVE
26
+ - Template with `Why`, `Scope`, `API`, `Acceptance Criteria`, `Risks`
27
+ - Delta spec support for incremental changes
28
+ - **6-phase workflow** (`workflow/WORKFLOW-GUIDE.md`)
29
+ - Brainstorm → Plan → TDD → Validate → Self-Review → Hand-off
30
+ - Specialized entry points: `/frontend`, `/backend`, `/fullstack`
31
+ - **6-dimension code review** (`skills/code-review/`)
32
+ - Security (18), Correctness (5), Performance (4), Design (4), Maintainability (6), Convention (3)
33
+ - Severity levels: CRITICAL / HIGH / MEDIUM / LOW
34
+ - Markdown + Excel export
35
+
36
+ ### Added — Memory System
37
+ - **Error-driven knowledge base** (`memory/learned-lessons.md`)
38
+ - Format: Symptom / Root cause / Rule
39
+ - 8 seed lessons (L-001 ~ L-008)
40
+ - **Project memory** (`memory/architecture.md`)
41
+ - System map, invariants, critical paths, conventions, gotchas
42
+
43
+ ### Added — Discipline Layer
44
+ - **Coding standards** (`rules/coding-standards.md`)
45
+ - Universal, type safety, function hygiene, file hygiene, testing, git
46
+ - **Git safety** (`rules/git-safety.md`)
47
+ - Forbidden destructive ops (force push, hard reset, branch -D)
48
+ - Branch naming, commit message format, pre-commit hook rules
49
+
50
+ ### Added — Tooling
51
+ - **CLI** (`bin/apprentice.js`)
52
+ - `init`, `update`, `doctor`, `version`, `help`
53
+ - Zero-dependency (Node >= 14)
54
+ - Auto project type detection (Java/Node/Go/Python/Rust/Generic)
55
+ - **One-line installer** (`install.sh`)
56
+ - `curl | bash` workflow
57
+ - Git clone preferred, curl fallback
58
+ - Auto-backup existing `.claude/`
59
+ - **Health check** (`scripts/health-check.sh`)
60
+ - Standalone — runs anywhere without Node
61
+
62
+ ### Added — Documentation
63
+ - **README.md** — full feature overview with quick start
64
+ - **LICENSE** — MIT
65
+ - **CHANGELOG.md** — this file
66
+ - **CONTRIBUTING.md** — contribution guidelines (TODO)
67
+
68
+ ### Migration
69
+ Project is a clean-slate open-source version of an internal tool (`tsc-toolkit`) that was iterated through v5.2 → v5.7. All proprietary/company-specific references have been removed.
70
+
71
+ ### Lessons Carried Over
72
+ 8 seed lessons (L-001 ~ L-008) distilled from ~50 hours of real-world Claude Code usage across Java Spring, Vue/React frontend, and Node.js backend projects.
73
+
74
+ ---
75
+
76
+ ## Version History (Pre-open-source)
77
+
78
+ These versions were internal and not publicly released. Documented here for context.
79
+
80
+ ### v5.7 (internal, 2026-06-10)
81
+ - Refined workflow phases
82
+ - Added 6-dimension code review (was 5)
83
+ - Fixed: spec drift problem (specs going stale)
84
+
85
+ ### v5.6 (internal, 2026-05-22)
86
+ - Added project type auto-detection
87
+ - Added multi-project workspace support
88
+ - Added health check command
89
+
90
+ ### v5.5 (internal, 2026-05-01)
91
+ - Refactored `learned-lessons.md` format
92
+ - Added L-005 ~ L-007
93
+ - Added git safety rules
94
+
95
+ ### v5.4 (internal, 2026-04-12)
96
+ - Added spec-driven development lifecycle
97
+ - Added delta spec support
98
+
99
+ ### v5.3 (internal, 2026-03-20)
100
+ - Compressed CLAUDE.md from 200+ lines to 53 lines
101
+ - Moved knowledge to memory/
102
+
103
+ ### v5.2 (internal, 2026-03-01)
104
+ - Initial 4-layer architecture (CLAUDE.md + commands + rules + skills)
105
+
106
+ ### Pre-v5.2
107
+ - Chaos. No structure. AI freelanced constantly. Many late nights.
108
+
109
+ ---
110
+
111
+ [Unreleased]: https://github.com/shuoyue/claude-apprentice/compare/v1.0.0...HEAD
112
+ [1.0.0]: https://github.com/shuoyue/claude-apprentice/releases/tag/v1.0.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 claude-apprentice contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,311 @@
1
+ # claude-apprentice
2
+
3
+ > Train Claude Code into a reliable apprentice — spec-driven, memory-backed, code-reviewed.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/claude-apprentice.svg)](https://www.npmjs.com/package/claude-apprentice)
6
+ [![GitHub stars](https://img.shields.io/github/stars/shuoyue/claude-apprentice.svg)](https://github.com/shuoyue/claude-apprentice/stargazers)
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8
+ [![Node](https://img.shields.io/badge/node-%3E%3D14-green)](https://nodejs.org)
9
+
10
+ AI 驱动全栈开发工程化体系 — 一条命令初始化项目。
11
+
12
+ 基于"三件套"(Claude Code + Superpowers + Spec)构建的轻量级 Harness 体系,解决 LLM 不可控和无状态的缺陷,让 AI 从"聊天玩具"进化为"可靠开发员工"。
13
+
14
+ ---
15
+
16
+ ## 快速开始
17
+
18
+ ### 推荐方式(npx)
19
+
20
+ ```bash
21
+ cd your-project
22
+ npx claude-apprentice init
23
+ ```
24
+
25
+ ### curl 一键安装(非 Node.js 用户)
26
+
27
+ ```bash
28
+ cd your-project
29
+ curl -fsSL https://raw.githubusercontent.com/shuoyue/claude-apprentice/main/install.sh | bash
30
+ ```
31
+
32
+ ### 初始化后做什么
33
+
34
+ 1. 检查 `CLAUDE.md` 中的技术栈信息
35
+ 2. 补充 `.claude/memory/business-logic.md` 中的业务逻辑
36
+ 3. 打开 Claude Code 直接开始工作
37
+
38
+ ---
39
+
40
+ ## 体系架构
41
+
42
+ ### 三层架构
43
+
44
+ | 层级 | 对应 | 职责 |
45
+ |------|------|------|
46
+ | **执行底盘** | Claude Code + `scripts/` | 文件读写、沙箱隔离、工具调用 — 让 AI 能操作真实世界 |
47
+ | **行为纪律** | `rules/` + Superpowers | TDD、代码审查、系统化调试 — 让 AI 不犯野路子错误 |
48
+ | **规格对齐** | `specs/`(`SPEC-GUIDE.md`)+ `memory/` + `CLAUDE.md` | 结构化需求文档作为共享锚点 — 防止理解偏差 |
49
+
50
+ ### 核心原则:原生优先
51
+
52
+ > **90% 的场景用原生 Claude Code 就够了。过度工程化是最大的浪费。**
53
+
54
+ | 复杂度 | 定义 | 策略 |
55
+ |--------|------|------|
56
+ | **简单** | 单文件修改,< 30 分钟 | 直接说需求,不走 workflow |
57
+ | **中等** | 新组件/新接口,30 分钟 - 2 小时 | brainstorming + spec + TDD |
58
+ | **复杂** | 新功能模块,> 2 小时 | 完整 workflow + spec + worktree + review |
59
+
60
+ ### 开发铁律
61
+
62
+ 1. **没设计不写代码** — 动手前先确认文件、范围、方案
63
+ 2. **没测试不写代码** — 先写失败测试,再写实现
64
+ 3. **没验证不说完成** — 必须运行验证命令并贴出结果
65
+
66
+ ---
67
+
68
+ ## 日常使用
69
+
70
+ ### 简单任务(推荐 90% 场景)
71
+
72
+ 直接说需求,不用切模式:
73
+
74
+ ```
75
+ 你: 修复登录接口返回 500 的问题
76
+ 你: 把分页改成每页 20 条
77
+ ```
78
+
79
+ ### 模式切换
80
+
81
+ | 命令 | 模式 | 适用场景 |
82
+ |------|------|----------|
83
+ | `/frontend` | 前端模式 | 页面开发、组件开发、样式调整 |
84
+ | `/backend` | 后端模式 | 接口开发、数据库操作、业务逻辑 |
85
+ | `/fullstack` | 全栈模式 | 新功能从 0 到 1、前后端联调 |
86
+ | `/spec` | Spec 模式 | 只想把需求理清楚,不立刻动手编码 |
87
+
88
+ ### `/spec` 命令
89
+
90
+ 强制产出 spec 文件,不进入实现阶段:
91
+
92
+ ```
93
+ 你: /spec 创建用户积分入账接口,需要幂等和并发控制
94
+ ```
95
+
96
+ | 入口 | 行为 |
97
+ |------|------|
98
+ | `/spec <需求>` | **只产出 spec,不写代码** |
99
+ | `/backend` `/frontend` `/fullstack` | 走完整 workflow(spec + 实现 + 验证 + 归档) |
100
+
101
+ ### 工作流步骤
102
+
103
+ | 步骤 | 名称 | 触发条件 | 产出 |
104
+ |------|------|---------|------|
105
+ | 0 | 需求入口 | 所有任务 | 复杂度判断 |
106
+ | 1 | 需求澄清 + Spec | 中等+复杂 | `specs/active/[feature].md` |
107
+ | 2 | 任务拆解 | 复杂 | 任务计划 |
108
+ | 3 | 实现 | 所有任务 | 代码(读取 spec 作为约束) |
109
+ | 4 | 验证 + 归档 | 所有任务 | 测试结果 + spec 移入 archived |
110
+
111
+ ### 代码评审
112
+
113
+ 自动触发或手动触发,按 7 个维度逐项检查:
114
+
115
+ | 优先级 | 维度 | 项数 |
116
+ |--------|------|------|
117
+ | 1 | 安全性 | 18 项 |
118
+ | 2 | 正确性 | 5 项 |
119
+ | 3 | 性能 | 4 项 |
120
+ | 4 | 设计与架构 | 4 项 |
121
+ | 5 | 可维护性 | 6 项 |
122
+ | 6 | 规范与一致性 | 3 项 |
123
+ | 7 | 文件类型专属 | 按后缀激活 |
124
+
125
+ 输出 MD 或 Excel 格式报告到 `.claude/reports/`。
126
+
127
+ ---
128
+
129
+ ## Spec 规格驱动
130
+
131
+ > 规格是唯一的真相来源。AI 每次从 spec 文件读约束,不靠聊天记录。
132
+
133
+ ```
134
+ Propose(提案)→ Apply(实施)→ Archive(归档)
135
+ ```
136
+
137
+ | 阶段 | 触发方式 | 做什么 |
138
+ |------|---------|--------|
139
+ | **Propose** | `/spec` 命令 或 workflow 步骤 1 | 在 `specs/active/` 创建 spec 文件 |
140
+ | **Apply** | 用户确认 spec 后 | 状态改为 Applied,后续 Session 从文件读约束 |
141
+ | **Archive** | workflow 最后一步 | 移入 `specs/archived/`,作为活文档保留 |
142
+
143
+ ---
144
+
145
+ ## 错题本机制
146
+
147
+ 每一条 `rules/` 和 `learned-lessons.md` 中的规则都对应一个真实失败案例:
148
+
149
+ ```markdown
150
+ ### L-001: AI 先写实现再补测试
151
+ - 错误现象:AI 直接写完整个实现,然后说"接下来补测试"
152
+ - 根因:没有强制 TDD 流程,AI 倾向走捷径
153
+ - 规避规则:workflow 步骤 3 必须先写失败测试,看到红灯再写实现
154
+ ```
155
+
156
+ **机制闭环**:AI 犯错 → 加一条规则 → 以后不再犯同类错误。
157
+
158
+ ---
159
+
160
+ ## CLI 命令
161
+
162
+ ```bash
163
+ apprentice init # 初始化项目(复制模板 + 运行 init.sh)
164
+ apprentice update # 增量更新(不覆盖已有文件)
165
+ apprentice doctor # 运行健康检查
166
+ apprentice version # 显示版本号
167
+ apprentice help # 显示帮助
168
+ ```
169
+
170
+ ---
171
+
172
+ ## 项目初始化后的目录结构
173
+
174
+ ```
175
+ your-project/
176
+ ├── CLAUDE.md # [自动加载] 项目最高优先级指令
177
+ └── .claude/
178
+ ├── settings.json # 主配置(团队共享,提交 git)
179
+ ├── MEMORY.md # 知识库索引
180
+ ├── commands/ # 斜杠命令
181
+ │ ├── frontend.md # /frontend
182
+ │ ├── backend.md # /backend
183
+ │ ├── fullstack.md # /fullstack
184
+ │ ├── spec.md # /spec
185
+ │ └── scan-todos.md # /scan-todos (Loop 层试点)
186
+ ├── rules/ # 条件规则(自动触发)
187
+ │ ├── INDEX.md # 规则触发索引
188
+ │ ├── coding-standards.md # 编码标准
189
+ │ ├── git-safety.md # Git 安全
190
+ │ └── superpowers-workflow.md # 工作流 + 调试规则
191
+ ├── skills/ # 工作流技能
192
+ │ ├── frontend-workflow.md
193
+ │ ├── backend-workflow.md
194
+ │ ├── fullstack-workflow.md
195
+ │ └── code-review/ # 代码评审(7 维度)
196
+ ├── reports/ # 评审报告输出
197
+ ├── specs/ # 功能规格(Propose → Apply → Archive)
198
+ │ ├── SPEC-GUIDE.md
199
+ │ ├── active/
200
+ │ └── archived/
201
+ ├── scripts/
202
+ │ ├── init.sh # 初始化(自动检测项目类型)
203
+ │ ├── health-check.sh # 健康巡检(9 项检查)
204
+ │ └── auto-review.sh # PostToolUse hook 自动评审
205
+ ├── usage-guides/ # 操作手册
206
+ │ ├── README.md
207
+ │ ├── usage-guide-v5.8.md
208
+ │ └── bottleneck-navigation.md # 瓶颈定位指南
209
+ └── memory/ # 项目知识库
210
+ ├── architecture.md
211
+ ├── frontend-standards.md
212
+ ├── backend-standards.md
213
+ ├── business-logic.md
214
+ ├── superpowers-config.md
215
+ ├── learned-lessons.md # 错误驱动的知识积累
216
+ └── issues.md
217
+ ```
218
+
219
+ ---
220
+
221
+ ## 多角色协同
222
+
223
+ | 阶段 | 参与角色 | 做什么 | 产出 |
224
+ |------|---------|--------|------|
225
+ | **混沌期** (Brainstorming) | PM + Tech Lead | 苏格拉底式对话,理清业务与技术选型 | 需求共识 |
226
+ | **规划期** (`/spec`) | PM 签收标准,Tech Lead 定方案 | 固化为 `specs/active/` 文档 | spec 文件 |
227
+ | **执行期** (Workflow) | Developer | TDD 流程:先写测试再实现,自我审查 | 代码 + 测试 |
228
+ | **验证期** (QA 左移) | QA | 基础单元测试已内建,专注集成/E2E/探索性测试 | 测试报告 |
229
+
230
+ ---
231
+
232
+ ## 渐进式落地
233
+
234
+ | 阶段 | 时间 | 聚焦 | 做什么 |
235
+ |------|------|------|--------|
236
+ | **起步期** | 第 1-2 周 | 单兵作战 | 统一工具链,建立 `CLAUDE.md` + `rules/`,跑通小需求闭环 |
237
+ | **进阶期** | 第 3-8 周 | 团队资产积累 | 封装 Skills,完善知识库和跨会话记忆 |
238
+ | **成熟期** | 第 9 周+ | 复杂场景扩展 | 按需引入 worktree 隔离、多 Agent 协调 |
239
+
240
+ ---
241
+
242
+ ## 常见问题
243
+
244
+ ### `/spec` 和 `/backend` 怎么选?
245
+
246
+ | 你的诉求 | 用哪个 |
247
+ |----------|--------|
248
+ | 只想把需求理清楚,不写代码 | `/spec` |
249
+ | 想直接做完一个功能 | `/backend` `/frontend` `/fullstack` |
250
+ | 多人协作,先对齐再分发 | `/spec` 写完分发 |
251
+
252
+ ### 新员工怎么上手?
253
+
254
+ ```bash
255
+ cd your-project
256
+ npx claude-apprentice init
257
+ claude
258
+ ```
259
+
260
+ ### 团队物理隔离怎么统一环境?
261
+
262
+ 所有规范配置提交到 Git 仓库。新成员拉取代码后,AI 自动读取配置文件,加载出完全一致的环境。**代码即配置,Git 即分发。**
263
+
264
+ ### 什么时候不用这套体系?
265
+
266
+ | 场景 | 建议 |
267
+ |------|------|
268
+ | 一次性脚本 | 直接写,跑通即可 |
269
+ | 探索性原型 | 快速验证,不走 spec |
270
+ | 紧急 Hotfix | 先修复,事后补测试和 spec |
271
+
272
+ ---
273
+
274
+ ## 兼容性
275
+
276
+ - ✅ **Claude Code** — 原生(slash commands + skills)
277
+ - ⚠️ **Cursor** — 手动(模板可用,无 slash commands)
278
+ - ⚠️ **GitHub Copilot Chat** — 手动(模板可用)
279
+ - ⚠️ **Windsurf** — 手动(模板可用)
280
+
281
+ ---
282
+
283
+ ## 版本更新
284
+
285
+ ```bash
286
+ # 增量更新,已有文件不覆盖
287
+ apprentice update
288
+ ```
289
+
290
+ ## 详细文档
291
+
292
+ - 完整使用手册见 `templates/usage-guides/` 目录
293
+ - 版本演进史见 [CHANGELOG.md](./CHANGELOG.md)
294
+
295
+ ---
296
+
297
+ ## 相关
298
+
299
+ - **Blog (中文)**: 公众号「造物手记」— author's writings on AI engineering
300
+ - **Mitchell Hashimoto's [AGENTS.md](https://github.com/mitchellh/agentkit)** — inspiration for `learned-lessons.md`
301
+ - **Anthropic's [Claude Code Skills](https://docs.claude.com/en/docs/agents-and-tools/claude-code/skills)** — the underlying mechanism
302
+
303
+ ---
304
+
305
+ ## License
306
+
307
+ [MIT](./LICENSE) — © 2026
308
+
309
+ ---
310
+
311
+ _Made with discipline. Crafted over 5 internal versions (v5.2 → v5.8), open-sourced as v1.0._
@@ -0,0 +1,196 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const { execSync } = require('child_process');
6
+
7
+ const PKG = require('../package.json');
8
+ const TEMPLATES_DIR = path.join(__dirname, '..', 'templates');
9
+
10
+ // ── Helpers ──────────────────────────────────────────────────────────
11
+
12
+ function log(msg) { console.log(` ${msg}`); }
13
+ function ok(msg) { console.log(`\x1b[32m ✓\x1b[0m ${msg}`); }
14
+ function warn(msg) { console.log(`\x1b[33m ⚠\x1b[0m ${msg}`); }
15
+ function err(msg) { console.error(`\x1b[31m ✗\x1b[0m ${msg}`); }
16
+
17
+ function copyDir(src, dest) {
18
+ if (!fs.existsSync(dest)) fs.mkdirSync(dest, { recursive: true });
19
+ for (const entry of fs.readdirSync(src)) {
20
+ if (entry === '.DS_Store') continue;
21
+ const srcPath = path.join(src, entry);
22
+ const destPath = path.join(dest, entry);
23
+ if (fs.statSync(srcPath).isDirectory()) {
24
+ copyDir(srcPath, destPath);
25
+ } else {
26
+ if (!fs.existsSync(destPath)) {
27
+ fs.copyFileSync(srcPath, destPath);
28
+ }
29
+ }
30
+ }
31
+ }
32
+
33
+ function getTargetDir() {
34
+ return process.cwd();
35
+ }
36
+
37
+ // ── Commands ─────────────────────────────────────────────────────────
38
+
39
+ function cmdInit(args) {
40
+ const target = getTargetDir();
41
+ const claudeDir = path.join(target, '.claude');
42
+
43
+ log(`目标目录: ${target}`);
44
+ log('');
45
+
46
+ // 1. 复制模板文件到 .claude/(增量:已有文件不覆盖)
47
+ ok('复制模板文件...');
48
+ copyDir(TEMPLATES_DIR, claudeDir);
49
+
50
+ // 2. 复制 CLAUDE.md 到项目根目录
51
+ const rootClaude = path.join(TEMPLATES_DIR, 'CLAUDE.md');
52
+ const targetClaude = path.join(target, 'CLAUDE.md');
53
+ if (fs.existsSync(rootClaude) && !fs.existsSync(targetClaude)) {
54
+ fs.copyFileSync(rootClaude, targetClaude);
55
+ ok('创建 CLAUDE.md');
56
+ } else if (fs.existsSync(targetClaude)) {
57
+ warn('CLAUDE.md 已存在,跳过');
58
+ }
59
+
60
+ // 3. 创建 reports 目录
61
+ const reportsDir = path.join(claudeDir, 'reports');
62
+ if (!fs.existsSync(reportsDir)) {
63
+ fs.mkdirSync(reportsDir, { recursive: true });
64
+ ok('创建 reports/ 目录');
65
+ }
66
+
67
+ // 4. 执行 init.sh
68
+ const initSh = path.join(claudeDir, 'scripts', 'init.sh');
69
+ if (fs.existsSync(initSh)) {
70
+ log('');
71
+ ok('执行 init.sh ...');
72
+ log('');
73
+ try {
74
+ execSync(`bash "${initSh}"`, { cwd: target, stdio: 'inherit' });
75
+ } catch (e) {
76
+ warn('init.sh 执行中出现警告(可忽略)');
77
+ }
78
+ }
79
+
80
+ log('');
81
+ ok('初始化完成!');
82
+ log('');
83
+ log('下一步:');
84
+ log(' 1. 检查 CLAUDE.md 中的技术栈信息');
85
+ log(' 2. 补充 .claude/memory/business-logic.md');
86
+ log(' 3. 运行 apprentice doctor 验证');
87
+ }
88
+
89
+ function cmdUpdate() {
90
+ const target = getTargetDir();
91
+ const claudeDir = path.join(target, '.claude');
92
+
93
+ if (!fs.existsSync(claudeDir)) {
94
+ err('.claude/ 目录不存在,请先运行 apprentice init');
95
+ process.exit(1);
96
+ }
97
+
98
+ log('增量更新...');
99
+ copyDir(TEMPLATES_DIR, claudeDir);
100
+
101
+ const initSh = path.join(claudeDir, 'scripts', 'init.sh');
102
+ if (fs.existsSync(initSh)) {
103
+ log('');
104
+ try {
105
+ execSync(`bash "${initSh}"`, { cwd: target, stdio: 'inherit' });
106
+ } catch (e) {
107
+ warn('init.sh 执行中出现警告(可忽略)');
108
+ }
109
+ }
110
+
111
+ log('');
112
+ ok('更新完成!');
113
+ }
114
+
115
+ function cmdDoctor() {
116
+ const target = getTargetDir();
117
+ const checkSh = path.join(target, '.claude', 'scripts', 'health-check.sh');
118
+
119
+ if (!fs.existsSync(checkSh)) {
120
+ err('.claude/scripts/health-check.sh 不存在,请先运行 apprentice init');
121
+ process.exit(1);
122
+ }
123
+
124
+ log('运行健康检查...');
125
+ log('');
126
+ try {
127
+ execSync(`bash "${checkSh}"`, { cwd: target, stdio: 'inherit' });
128
+ } catch (e) {
129
+ // health-check.sh may exit with non-zero on failures
130
+ }
131
+ }
132
+
133
+ function cmdVersion() {
134
+ console.log(`claude-apprentice v${PKG.version}`);
135
+ }
136
+
137
+ function cmdHelp() {
138
+ console.log(`
139
+ \x1b[1mclaude-apprentice\x1b[0m v${PKG.version} — Train Claude Code into a reliable apprentice
140
+
141
+ \x1b[1m用法:\x1b[0m
142
+ apprentice <command> [options]
143
+
144
+ \x1b[1m命令:\x1b[0m
145
+ init 初始化当前项目(复制模板 + 运行 init.sh)
146
+ update 增量更新(不覆盖已有文件)
147
+ doctor 运行健康检查
148
+ version 显示版本号
149
+ help 显示帮助信息
150
+
151
+ \x1b[1m使用方式:\x1b[0m
152
+ # npx 直接运行(推荐)
153
+ npx claude-apprentice init
154
+
155
+ # curl 一键安装(非 Node.js 用户)
156
+ curl -fsSL https://raw.githubusercontent.com/shuoyue/claude-apprentice/main/install.sh | bash
157
+
158
+ # 本地开发
159
+ node bin/apprentice.js init
160
+
161
+ \x1b[1m文档:\x1b[0m
162
+ https://github.com/shuoyue/claude-apprentice
163
+ `);
164
+ }
165
+
166
+ // ── Main ─────────────────────────────────────────────────────────────
167
+
168
+ const args = process.argv.slice(2);
169
+ const command = args[0];
170
+
171
+ switch (command) {
172
+ case 'init':
173
+ cmdInit(args.slice(1));
174
+ break;
175
+ case 'update':
176
+ cmdUpdate();
177
+ break;
178
+ case 'doctor':
179
+ cmdDoctor();
180
+ break;
181
+ case 'version':
182
+ case '-v':
183
+ case '--version':
184
+ cmdVersion();
185
+ break;
186
+ case 'help':
187
+ case '-h':
188
+ case '--help':
189
+ case undefined:
190
+ cmdHelp();
191
+ break;
192
+ default:
193
+ err(`未知命令: ${command}`);
194
+ console.log('运行 apprentice help 查看可用命令');
195
+ process.exit(1);
196
+ }