@leocm/flowcraft 2.1.0 → 2.3.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 (96) hide show
  1. package/README.md +19 -2
  2. package/bin/cli.js +108 -108
  3. package/flowcraft.yaml +30 -30
  4. package/install.sh +24 -24
  5. package/lib/args.js +38 -38
  6. package/lib/config.js +20 -20
  7. package/lib/copy.js +131 -131
  8. package/lib/fix.js +110 -110
  9. package/lib/fsutil.js +104 -104
  10. package/lib/global.js +229 -229
  11. package/lib/init.js +43 -43
  12. package/lib/runtime.js +84 -84
  13. package/lib/version.js +55 -55
  14. package/lib/wizard.js +125 -125
  15. package/package.json +33 -33
  16. package/presets/backend.yaml +20 -20
  17. package/presets/default.yaml +15 -15
  18. package/presets/frontend.yaml +20 -20
  19. package/presets/quick-fix.yaml +15 -15
  20. package/setup/.claude/hooks/check-phase-test.py +152 -152
  21. package/setup/.claude/hooks/check-phase-write.py +235 -235
  22. package/setup/.claude/hooks/dispatch.py +67 -67
  23. package/setup/.claude/hooks/file-suggestion.py +45 -45
  24. package/setup/.claude/hooks/flowcraft_env.py +162 -137
  25. package/setup/.claude/hooks/permission-request.py +132 -125
  26. package/setup/.claude/hooks/post-tool-failure.py +36 -36
  27. package/setup/.claude/hooks/pre-compact.py +66 -66
  28. package/setup/.claude/hooks/review-runner.py +846 -846
  29. package/setup/.claude/hooks/setup-keys.py +282 -282
  30. package/setup/.claude/hooks/statusline.py +63 -63
  31. package/setup/.claude/hooks/stop-check.py +186 -171
  32. package/setup/.claude/hooks/subagent-start.py +50 -50
  33. package/setup/.claude/hooks/subagent-stop.py +63 -63
  34. package/setup/.claude/hooks/task-completed.py +57 -57
  35. package/setup/.claude/hooks/task_manager.py +592 -592
  36. package/setup/.claude/hooks/user-prompt-submit.py +91 -85
  37. package/setup/.claude/settings.json +233 -237
  38. package/setup/.claude/skills/Pstatus/SKILL.md +68 -68
  39. package/setup/.claude/skills/archive/SKILL.md +195 -195
  40. package/setup/.claude/skills/brainstorming/SKILL.md +5 -5
  41. package/setup/.claude/skills/brainstorming/scripts/server.cjs +723 -723
  42. package/setup/.claude/skills/brainstorming/scripts/start-server.py +245 -245
  43. package/setup/.claude/skills/brainstorming/scripts/stop-server.py +178 -178
  44. package/setup/.claude/skills/brainstorming/visual-companion.md +298 -298
  45. package/setup/.claude/skills/chrome-integration/SKILL.md +66 -66
  46. package/setup/.claude/skills/code-review/SKILL.md +264 -264
  47. package/setup/.claude/skills/executing-plans/SKILL.md +1 -1
  48. package/setup/.claude/skills/gitnexus/gitnexus-cli/SKILL.md +67 -67
  49. package/setup/.claude/skills/gitnexus/gitnexus-debugging/SKILL.md +1 -1
  50. package/setup/.claude/skills/gitnexus/gitnexus-exploring/SKILL.md +1 -1
  51. package/setup/.claude/skills/gitnexus/gitnexus-guide/SKILL.md +1 -1
  52. package/setup/.claude/skills/gitnexus/gitnexus-impact-analysis/SKILL.md +1 -1
  53. package/setup/.claude/skills/gitnexus/gitnexus-refactoring/SKILL.md +1 -1
  54. package/setup/.claude/skills/methodology/SKILL.md +124 -0
  55. package/setup/.claude/skills/phase/SKILL.md +62 -62
  56. package/setup/.claude/skills/playwright/SKILL.md +85 -85
  57. package/setup/.claude/skills/review/SKILL.md +97 -97
  58. package/setup/.claude/skills/task/SKILL.md +26 -26
  59. package/setup/.claude/skills/test-driven-development/SKILL.md +1 -1
  60. package/setup/.claude/skills/ui-ux-pro-max/SKILL.md +1 -1
  61. package/setup/.claude/skills/verification-before-completion/SKILL.md +1 -1
  62. package/setup/.claude/skills/writing-plans/SKILL.md +1 -1
  63. package/setup/.codex/.agents/skills/archive/SKILL.md +195 -195
  64. package/setup/.codex/.agents/skills/brainstorming/SKILL.md +5 -5
  65. package/setup/.codex/.agents/skills/brainstorming/scripts/server.cjs +723 -723
  66. package/setup/.codex/.agents/skills/brainstorming/scripts/start-server.py +245 -245
  67. package/setup/.codex/.agents/skills/brainstorming/scripts/stop-server.py +178 -178
  68. package/setup/.codex/.agents/skills/brainstorming/visual-companion.md +298 -298
  69. package/setup/.codex/.agents/skills/chrome-integration/SKILL.md +66 -66
  70. package/setup/.codex/.agents/skills/code-review/SKILL.md +264 -264
  71. package/setup/.codex/.agents/skills/executing-plans/SKILL.md +1 -1
  72. package/setup/.codex/.agents/skills/gitnexus/gitnexus-cli/SKILL.md +67 -67
  73. package/setup/.codex/.agents/skills/gitnexus/gitnexus-debugging/SKILL.md +1 -1
  74. package/setup/.codex/.agents/skills/gitnexus/gitnexus-exploring/SKILL.md +1 -1
  75. package/setup/.codex/.agents/skills/gitnexus/gitnexus-guide/SKILL.md +1 -1
  76. package/setup/.codex/.agents/skills/gitnexus/gitnexus-impact-analysis/SKILL.md +1 -1
  77. package/setup/.codex/.agents/skills/gitnexus/gitnexus-refactoring/SKILL.md +1 -1
  78. package/setup/.codex/.agents/skills/methodology/SKILL.md +124 -0
  79. package/setup/.codex/.agents/skills/phase/SKILL.md +62 -62
  80. package/setup/.codex/.agents/skills/playwright/SKILL.md +85 -85
  81. package/setup/.codex/.agents/skills/review/SKILL.md +97 -97
  82. package/setup/.codex/.agents/skills/task/SKILL.md +27 -27
  83. package/setup/.codex/.agents/skills/test-driven-development/SKILL.md +1 -1
  84. package/setup/.codex/.agents/skills/ui-ux-pro-max/SKILL.md +1 -1
  85. package/setup/.codex/.agents/skills/verification-before-completion/SKILL.md +1 -1
  86. package/setup/.codex/.agents/skills/writing-plans/SKILL.md +1 -1
  87. package/setup/.codex/hooks/review-runner.py +851 -851
  88. package/setup/.codex/hooks/setup-keys.py +287 -287
  89. package/setup/AGENTS.md +11 -1
  90. package/setup/CLAUDE.md +11 -1
  91. package/setup/install.sh +24 -24
  92. package/setup/tests/test_decision_timeout.py +241 -241
  93. package/setup/tests/test_hook_guards.py +357 -357
  94. package/setup/tests/test_sdlc_bootstrap.py +167 -167
  95. package/setup/tests/test_sessionlog_switch.py +199 -0
  96. package/setup/tests/test_task_manager.py +148 -148
package/lib/global.js CHANGED
@@ -1,229 +1,229 @@
1
- /**
2
- * global.js — 全局安装(R4:对齐 install.sh install_global 及其子步骤)
3
- */
4
- 'use strict';
5
-
6
- const fs = require('node:fs');
7
- const path = require('node:path');
8
- const { timestamp, backupFile, copyDir, nowStr } = require('./fsutil');
9
- const { fixLineEndings, replacePythonCommands } = require('./fix');
10
- const { FRAMEWORK_VERSION } = require('./config');
11
-
12
- /** hook 超时秒数(值与 legacy install.sh 完全一致,不改行为) */
13
- const TIMEOUT_SHORT = 3;
14
- const TIMEOUT_LONG = 5;
15
-
16
- /**
17
- * 构建 ~/.claude/settings.json 的 hooks 结构(对齐 install.sh 内联 JSON)
18
- * @param {string} hookCmd hook 命令前缀(如 "python ~/.flowcraft/hooks/dispatch.py")
19
- * @returns {object} settings 片段(hooks + statusLine)
20
- */
21
- function buildHookSettings(hookCmd) {
22
- const c = (sub, timeout, statusMessage) => {
23
- const o = { type: 'command', command: `${hookCmd} ${sub}`, timeout };
24
- if (statusMessage) o.statusMessage = statusMessage;
25
- return o;
26
- };
27
- return {
28
- hooks: {
29
- SessionStart: [{ matcher: 'startup|resume|compact', hooks: [c('session-start', TIMEOUT_LONG)] }],
30
- UserPromptSubmit: [{ hooks: [c('user-prompt-submit', TIMEOUT_SHORT)] }],
31
- PreToolUse: [
32
- {
33
- matcher: 'Write|Edit',
34
- hooks: [
35
- c('check-phase-advancement', TIMEOUT_SHORT, '检查阶段产出物...'),
36
- c('check-phase-write', TIMEOUT_SHORT, '检查写入权限...'),
37
- ],
38
- },
39
- { matcher: 'Bash', hooks: [c('check-phase-test', TIMEOUT_SHORT, '检查命令权限...')] },
40
- ],
41
- Stop: [{ hooks: [c('stop-check', TIMEOUT_LONG)] }],
42
- PreCompact: [{ hooks: [c('pre-compact', TIMEOUT_LONG)] }],
43
- SubagentStart: [{ hooks: [c('subagent-start', TIMEOUT_LONG, '注入 SDLC 上下文...')] }],
44
- SubagentStop: [{ hooks: [c('subagent-stop', TIMEOUT_LONG, '验证子 Agent 输出...')] }],
45
- TaskCompleted: [{ hooks: [c('task-completed', TIMEOUT_SHORT)] }],
46
- PostToolUseFailure: [{ hooks: [c('post-tool-failure', TIMEOUT_LONG)] }],
47
- PermissionRequest: [{ hooks: [c('permission-request', TIMEOUT_LONG, 'SDLC 权限决策...')] }],
48
- SessionEnd: [{ hooks: [c('session-end', TIMEOUT_LONG)] }],
49
- },
50
- statusLine: { type: 'command', command: `${hookCmd} statusline`, padding: 1 },
51
- };
52
- }
53
-
54
- /**
55
- * 合并 ~/.claude/settings.json(仅覆盖 hooks/statusLine,其余键无损;覆盖前备份)
56
- * @param {string} settingsPath settings.json 路径
57
- * @param {string} hookCmd hook 命令前缀
58
- * @returns {void}
59
- */
60
- function mergeClaudeSettings(settingsPath, hookCmd) {
61
- const built = buildHookSettings(hookCmd);
62
- fs.mkdirSync(path.dirname(settingsPath), { recursive: true });
63
- if (!fs.existsSync(settingsPath)) {
64
- const fresh = { $schema: 'https://json.schemastore.org/claude-code-settings.json', ...built };
65
- fs.writeFileSync(settingsPath, JSON.stringify(fresh, null, 2));
66
- console.log(`✓ 已创建 ${settingsPath}`);
67
- return;
68
- }
69
- console.log(`! 检测到已有 ${settingsPath},合并 hook 配置...`);
70
- backupFile(settingsPath, timestamp());
71
- const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
72
- settings.hooks = built.hooks;
73
- settings.statusLine = built.statusLine;
74
- fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2));
75
- console.log(' hooks 配置已合并');
76
- }
77
-
78
- /**
79
- * 合并 Codex hooks.json:模板相对路径改写为用户级绝对路径(覆盖前备份)
80
- * @param {string} tplPath 模板 hooks.json(setup/.codex/hooks.json)
81
- * @param {string} dstPath 目标(~/.codex/hooks.json)
82
- * @param {string} hooksDir 全局 hook 脚本目录(正斜杠)
83
- * @returns {void}
84
- */
85
- function mergeCodexHooksJson(tplPath, dstPath, hooksDir) {
86
- if (fs.existsSync(dstPath)) backupFile(dstPath, timestamp());
87
- const tpl = JSON.parse(fs.readFileSync(tplPath, 'utf8'));
88
- for (const groups of Object.values(tpl.hooks || {})) {
89
- for (const g of groups) {
90
- for (const h of g.hooks || []) {
91
- if (h.type === 'command' && typeof h.command === 'string') {
92
- h.command = h.command.replace(/\.codex\/hooks\//g, `${hooksDir}/`);
93
- }
94
- }
95
- }
96
- }
97
- const existing = fs.existsSync(dstPath) ? JSON.parse(fs.readFileSync(dstPath, 'utf8')) : {};
98
- const out = { ...existing, hooks: { ...(existing.hooks || {}), ...(tpl.hooks || {}) } };
99
- fs.mkdirSync(path.dirname(dstPath), { recursive: true });
100
- fs.writeFileSync(dstPath, JSON.stringify(out, null, 2));
101
- console.log(` ✓ Codex hooks 注册 -> ${dstPath}`);
102
- }
103
-
104
- /**
105
- * 合并 Codex 实验特性开关([features] skills/codex_hooks=true,已有配置无损)
106
- * @param {string} configPath ~/.codex/config.toml
107
- * @returns {void}
108
- */
109
- function mergeCodexFeatures(configPath) {
110
- if (fs.existsSync(configPath)) backupFile(configPath, timestamp());
111
- const want = { skills: 'true', codex_hooks: 'true' };
112
- let lines = fs.existsSync(configPath) ? fs.readFileSync(configPath, 'utf8').split('\n') : [];
113
- const start = lines.findIndex((l) => /^\s*\[features\]\s*$/.test(l));
114
- if (start === -1) {
115
- if (lines.length && !lines[lines.length - 1].endsWith('\n')) lines[lines.length - 1] += '\n';
116
- lines.push('[features]');
117
- for (const [k, v] of Object.entries(want)) lines.push(`${k} = ${v}`);
118
- } else {
119
- let end = start + 1;
120
- const have = new Set();
121
- while (end < lines.length && !/^\s*\[/.test(lines[end])) {
122
- const m = /^\s*([A-Za-z0-9_.-]+)\s*=/.exec(lines[end]);
123
- if (m) have.add(m[1]);
124
- end += 1;
125
- }
126
- const add = Object.entries(want).filter(([k]) => !have.has(k)).map(([k, v]) => `${k} = ${v}`);
127
- lines = [...lines.slice(0, end), ...add, ...lines.slice(end)];
128
- }
129
- fs.mkdirSync(path.dirname(configPath), { recursive: true });
130
- fs.writeFileSync(configPath, lines.join('\n'));
131
- console.log(` ✓ Codex 特性开关已启用 -> ${configPath} [features]`);
132
- }
133
-
134
- /**
135
- * 分发 skills/commands 到用户级 Claude 与 Codex(全局兜底)
136
- * @param {string} setupClaude setup/.claude
137
- * @param {string} setupCodex setup/.codex
138
- * @param {string} home 用户主目录
139
- * @returns {void}
140
- */
141
- function installGlobalSkills(setupClaude, setupCodex, home) {
142
- const pairs = [
143
- [path.join(setupClaude, 'skills'), path.join(home, '.claude', 'skills'), 'Claude skills'],
144
- [path.join(setupClaude, 'commands'), path.join(home, '.claude', 'commands'), 'Claude commands'],
145
- [path.join(setupCodex, '.agents', 'skills'), path.join(home, '.codex', 'skills'), 'Codex skills'],
146
- ];
147
- for (const [src, dst, label] of pairs) {
148
- if (!fs.existsSync(src)) continue;
149
- copyDir(src, dst);
150
- console.log(` ✓ ${label} -> ${dst}(全局兜底)`);
151
- }
152
- }
153
-
154
- /**
155
- * 安装 Codex 全局 hooks:脚本入 ~/.flowcraft/codex-hooks 并注册 hooks.json
156
- * @param {string} setupCodex setup/.codex
157
- * @param {string} globalDir ~/.flowcraft
158
- * @param {string} home 用户主目录
159
- * @returns {void}
160
- */
161
- function installGlobalCodexHooks(setupCodex, globalDir, home) {
162
- const tplPath = path.join(setupCodex, 'hooks.json');
163
- if (!fs.existsSync(tplPath)) {
164
- console.log(' 跳过: setup/.codex/hooks.json 不存在');
165
- return;
166
- }
167
- const codexHooksDir = path.join(globalDir, 'codex-hooks');
168
- fs.mkdirSync(codexHooksDir, { recursive: true });
169
- const hooksSrc = path.join(setupCodex, 'hooks');
170
- if (fs.existsSync(hooksSrc)) {
171
- copyDir(hooksSrc, codexHooksDir);
172
- console.log(` ✓ Codex hook 脚本 -> ${codexHooksDir}`);
173
- }
174
- mergeCodexHooksJson(tplPath, path.join(home, '.codex', 'hooks.json'), codexHooksDir.replace(/\\/g, '/'));
175
- }
176
-
177
- /**
178
- * 写入全局版本文件(对齐 install.sh 第 8 步)
179
- * @param {string} globalDir ~/.flowcraft
180
- * @returns {void}
181
- */
182
- function writeGlobalVersion(globalDir) {
183
- fs.mkdirSync(globalDir, { recursive: true });
184
- const body = `${FRAMEWORK_VERSION}\ninstalled: ${nowStr()}\nmode: global\nskills: claude+codex\ncodex_hooks: global\n`;
185
- fs.writeFileSync(path.join(globalDir, 'flowcraft-version'), body);
186
- console.log('✓ 版本文件已写入');
187
- }
188
-
189
- /**
190
- * 全局安装主流程(对齐 install_global 步骤 1-8)
191
- * @param {string} pkgRoot 包根
192
- * @param {string} home 用户主目录
193
- * @param {string} pythonCmd 探测到的 python 命令
194
- * @returns {void}
195
- */
196
- function installGlobal(pkgRoot, home, pythonCmd) {
197
- const setupClaude = path.join(pkgRoot, 'setup', '.claude');
198
- const setupCodex = path.join(pkgRoot, 'setup', '.codex');
199
- const globalDir = path.join(home, '.flowcraft');
200
- const hooksDir = path.join(globalDir, 'hooks');
201
- console.log('[全局模式] 安装 FlowCraft 到用户级(所有项目共享)');
202
- console.log(` 全局目录: ${globalDir}`);
203
- console.log(` 用户配置: ${path.join(home, '.claude', 'settings.json')}`);
204
- console.log('');
205
- if (!fs.existsSync(path.join(setupClaude, 'hooks'))) throw new Error('setup/.claude/hooks 目录不存在');
206
- copyDir(path.join(setupClaude, 'hooks'), hooksDir);
207
- console.log(`✓ hooks 已复制到 ${hooksDir}`);
208
- fixLineEndings(hooksDir);
209
- replacePythonCommands(hooksDir, pythonCmd);
210
- let pyCmd = pythonCmd || 'python';
211
- if (process.platform === 'win32' && (pyCmd === 'python3' || pyCmd === 'py')) pyCmd = 'python';
212
- const dispatchPath = path.join(hooksDir, 'dispatch.py').replace(/\\/g, '/');
213
- mergeClaudeSettings(path.join(home, '.claude', 'settings.json'), `${pyCmd} ${dispatchPath}`);
214
- const tplDir = path.join(globalDir, 'templates');
215
- fs.mkdirSync(tplDir, { recursive: true });
216
- for (const tpl of ['CLAUDE.md', 'AGENTS.md']) {
217
- const src = path.join(pkgRoot, 'setup', tpl);
218
- if (fs.existsSync(src)) fs.copyFileSync(src, path.join(tplDir, tpl));
219
- }
220
- fs.copyFileSync(path.join(setupClaude, 'project-state.md'), path.join(tplDir, 'claude-project-state.md'));
221
- fs.copyFileSync(path.join(setupCodex, 'project-state.md'), path.join(tplDir, 'codex-project-state.md'));
222
- console.log(`✓ 模板目录已创建: ${tplDir}`);
223
- installGlobalSkills(setupClaude, setupCodex, home);
224
- installGlobalCodexHooks(setupCodex, globalDir, home);
225
- mergeCodexFeatures(path.join(home, '.codex', 'config.toml'));
226
- writeGlobalVersion(globalDir);
227
- }
228
-
229
- module.exports = { buildHookSettings, mergeClaudeSettings, mergeCodexHooksJson, mergeCodexFeatures, installGlobalSkills, installGlobalCodexHooks, writeGlobalVersion, installGlobal };
1
+ /**
2
+ * global.js — 全局安装(R4:对齐 install.sh install_global 及其子步骤)
3
+ */
4
+ 'use strict';
5
+
6
+ const fs = require('node:fs');
7
+ const path = require('node:path');
8
+ const { timestamp, backupFile, copyDir, nowStr } = require('./fsutil');
9
+ const { fixLineEndings, replacePythonCommands } = require('./fix');
10
+ const { FRAMEWORK_VERSION } = require('./config');
11
+
12
+ /** hook 超时秒数(值与 legacy install.sh 完全一致,不改行为) */
13
+ const TIMEOUT_SHORT = 3;
14
+ const TIMEOUT_LONG = 5;
15
+
16
+ /**
17
+ * 构建 ~/.claude/settings.json 的 hooks 结构(对齐 install.sh 内联 JSON)
18
+ * @param {string} hookCmd hook 命令前缀(如 "python ~/.flowcraft/hooks/dispatch.py")
19
+ * @returns {object} settings 片段(hooks + statusLine)
20
+ */
21
+ function buildHookSettings(hookCmd) {
22
+ const c = (sub, timeout, statusMessage) => {
23
+ const o = { type: 'command', command: `${hookCmd} ${sub}`, timeout };
24
+ if (statusMessage) o.statusMessage = statusMessage;
25
+ return o;
26
+ };
27
+ return {
28
+ hooks: {
29
+ SessionStart: [{ matcher: 'startup|resume|compact', hooks: [c('session-start', TIMEOUT_LONG)] }],
30
+ UserPromptSubmit: [{ hooks: [c('user-prompt-submit', TIMEOUT_SHORT)] }],
31
+ PreToolUse: [
32
+ {
33
+ matcher: 'Write|Edit',
34
+ hooks: [
35
+ c('check-phase-advancement', TIMEOUT_SHORT, '检查阶段产出物...'),
36
+ c('check-phase-write', TIMEOUT_SHORT, '检查写入权限...'),
37
+ ],
38
+ },
39
+ { matcher: 'Bash', hooks: [c('check-phase-test', TIMEOUT_SHORT, '检查命令权限...')] },
40
+ ],
41
+ Stop: [{ hooks: [c('stop-check', TIMEOUT_LONG)] }],
42
+ PreCompact: [{ hooks: [c('pre-compact', TIMEOUT_LONG)] }],
43
+ SubagentStart: [{ hooks: [c('subagent-start', TIMEOUT_LONG, '注入 SDLC 上下文...')] }],
44
+ SubagentStop: [{ hooks: [c('subagent-stop', TIMEOUT_LONG, '验证子 Agent 输出...')] }],
45
+ TaskCompleted: [{ hooks: [c('task-completed', TIMEOUT_SHORT)] }],
46
+ PostToolUseFailure: [{ hooks: [c('post-tool-failure', TIMEOUT_LONG)] }],
47
+ PermissionRequest: [{ hooks: [c('permission-request', TIMEOUT_LONG, 'SDLC 权限决策...')] }],
48
+ SessionEnd: [{ hooks: [c('session-end', TIMEOUT_LONG)] }],
49
+ },
50
+ statusLine: { type: 'command', command: `${hookCmd} statusline`, padding: 1 },
51
+ };
52
+ }
53
+
54
+ /**
55
+ * 合并 ~/.claude/settings.json(仅覆盖 hooks/statusLine,其余键无损;覆盖前备份)
56
+ * @param {string} settingsPath settings.json 路径
57
+ * @param {string} hookCmd hook 命令前缀
58
+ * @returns {void}
59
+ */
60
+ function mergeClaudeSettings(settingsPath, hookCmd) {
61
+ const built = buildHookSettings(hookCmd);
62
+ fs.mkdirSync(path.dirname(settingsPath), { recursive: true });
63
+ if (!fs.existsSync(settingsPath)) {
64
+ const fresh = { $schema: 'https://json.schemastore.org/claude-code-settings.json', ...built };
65
+ fs.writeFileSync(settingsPath, JSON.stringify(fresh, null, 2));
66
+ console.log(`✓ 已创建 ${settingsPath}`);
67
+ return;
68
+ }
69
+ console.log(`! 检测到已有 ${settingsPath},合并 hook 配置...`);
70
+ backupFile(settingsPath, timestamp());
71
+ const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
72
+ settings.hooks = built.hooks;
73
+ settings.statusLine = built.statusLine;
74
+ fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2));
75
+ console.log(' hooks 配置已合并');
76
+ }
77
+
78
+ /**
79
+ * 合并 Codex hooks.json:模板相对路径改写为用户级绝对路径(覆盖前备份)
80
+ * @param {string} tplPath 模板 hooks.json(setup/.codex/hooks.json)
81
+ * @param {string} dstPath 目标(~/.codex/hooks.json)
82
+ * @param {string} hooksDir 全局 hook 脚本目录(正斜杠)
83
+ * @returns {void}
84
+ */
85
+ function mergeCodexHooksJson(tplPath, dstPath, hooksDir) {
86
+ if (fs.existsSync(dstPath)) backupFile(dstPath, timestamp());
87
+ const tpl = JSON.parse(fs.readFileSync(tplPath, 'utf8'));
88
+ for (const groups of Object.values(tpl.hooks || {})) {
89
+ for (const g of groups) {
90
+ for (const h of g.hooks || []) {
91
+ if (h.type === 'command' && typeof h.command === 'string') {
92
+ h.command = h.command.replace(/\.codex\/hooks\//g, `${hooksDir}/`);
93
+ }
94
+ }
95
+ }
96
+ }
97
+ const existing = fs.existsSync(dstPath) ? JSON.parse(fs.readFileSync(dstPath, 'utf8')) : {};
98
+ const out = { ...existing, hooks: { ...(existing.hooks || {}), ...(tpl.hooks || {}) } };
99
+ fs.mkdirSync(path.dirname(dstPath), { recursive: true });
100
+ fs.writeFileSync(dstPath, JSON.stringify(out, null, 2));
101
+ console.log(` ✓ Codex hooks 注册 -> ${dstPath}`);
102
+ }
103
+
104
+ /**
105
+ * 合并 Codex 实验特性开关([features] skills/codex_hooks=true,已有配置无损)
106
+ * @param {string} configPath ~/.codex/config.toml
107
+ * @returns {void}
108
+ */
109
+ function mergeCodexFeatures(configPath) {
110
+ if (fs.existsSync(configPath)) backupFile(configPath, timestamp());
111
+ const want = { skills: 'true', codex_hooks: 'true' };
112
+ let lines = fs.existsSync(configPath) ? fs.readFileSync(configPath, 'utf8').split('\n') : [];
113
+ const start = lines.findIndex((l) => /^\s*\[features\]\s*$/.test(l));
114
+ if (start === -1) {
115
+ if (lines.length && !lines[lines.length - 1].endsWith('\n')) lines[lines.length - 1] += '\n';
116
+ lines.push('[features]');
117
+ for (const [k, v] of Object.entries(want)) lines.push(`${k} = ${v}`);
118
+ } else {
119
+ let end = start + 1;
120
+ const have = new Set();
121
+ while (end < lines.length && !/^\s*\[/.test(lines[end])) {
122
+ const m = /^\s*([A-Za-z0-9_.-]+)\s*=/.exec(lines[end]);
123
+ if (m) have.add(m[1]);
124
+ end += 1;
125
+ }
126
+ const add = Object.entries(want).filter(([k]) => !have.has(k)).map(([k, v]) => `${k} = ${v}`);
127
+ lines = [...lines.slice(0, end), ...add, ...lines.slice(end)];
128
+ }
129
+ fs.mkdirSync(path.dirname(configPath), { recursive: true });
130
+ fs.writeFileSync(configPath, lines.join('\n'));
131
+ console.log(` ✓ Codex 特性开关已启用 -> ${configPath} [features]`);
132
+ }
133
+
134
+ /**
135
+ * 分发 skills/commands 到用户级 Claude 与 Codex(全局兜底)
136
+ * @param {string} setupClaude setup/.claude
137
+ * @param {string} setupCodex setup/.codex
138
+ * @param {string} home 用户主目录
139
+ * @returns {void}
140
+ */
141
+ function installGlobalSkills(setupClaude, setupCodex, home) {
142
+ const pairs = [
143
+ [path.join(setupClaude, 'skills'), path.join(home, '.claude', 'skills'), 'Claude skills'],
144
+ [path.join(setupClaude, 'commands'), path.join(home, '.claude', 'commands'), 'Claude commands'],
145
+ [path.join(setupCodex, '.agents', 'skills'), path.join(home, '.codex', 'skills'), 'Codex skills'],
146
+ ];
147
+ for (const [src, dst, label] of pairs) {
148
+ if (!fs.existsSync(src)) continue;
149
+ copyDir(src, dst);
150
+ console.log(` ✓ ${label} -> ${dst}(全局兜底)`);
151
+ }
152
+ }
153
+
154
+ /**
155
+ * 安装 Codex 全局 hooks:脚本入 ~/.flowcraft/codex-hooks 并注册 hooks.json
156
+ * @param {string} setupCodex setup/.codex
157
+ * @param {string} globalDir ~/.flowcraft
158
+ * @param {string} home 用户主目录
159
+ * @returns {void}
160
+ */
161
+ function installGlobalCodexHooks(setupCodex, globalDir, home) {
162
+ const tplPath = path.join(setupCodex, 'hooks.json');
163
+ if (!fs.existsSync(tplPath)) {
164
+ console.log(' 跳过: setup/.codex/hooks.json 不存在');
165
+ return;
166
+ }
167
+ const codexHooksDir = path.join(globalDir, 'codex-hooks');
168
+ fs.mkdirSync(codexHooksDir, { recursive: true });
169
+ const hooksSrc = path.join(setupCodex, 'hooks');
170
+ if (fs.existsSync(hooksSrc)) {
171
+ copyDir(hooksSrc, codexHooksDir);
172
+ console.log(` ✓ Codex hook 脚本 -> ${codexHooksDir}`);
173
+ }
174
+ mergeCodexHooksJson(tplPath, path.join(home, '.codex', 'hooks.json'), codexHooksDir.replace(/\\/g, '/'));
175
+ }
176
+
177
+ /**
178
+ * 写入全局版本文件(对齐 install.sh 第 8 步)
179
+ * @param {string} globalDir ~/.flowcraft
180
+ * @returns {void}
181
+ */
182
+ function writeGlobalVersion(globalDir) {
183
+ fs.mkdirSync(globalDir, { recursive: true });
184
+ const body = `${FRAMEWORK_VERSION}\ninstalled: ${nowStr()}\nmode: global\nskills: claude+codex\ncodex_hooks: global\n`;
185
+ fs.writeFileSync(path.join(globalDir, 'flowcraft-version'), body);
186
+ console.log('✓ 版本文件已写入');
187
+ }
188
+
189
+ /**
190
+ * 全局安装主流程(对齐 install_global 步骤 1-8)
191
+ * @param {string} pkgRoot 包根
192
+ * @param {string} home 用户主目录
193
+ * @param {string} pythonCmd 探测到的 python 命令
194
+ * @returns {void}
195
+ */
196
+ function installGlobal(pkgRoot, home, pythonCmd) {
197
+ const setupClaude = path.join(pkgRoot, 'setup', '.claude');
198
+ const setupCodex = path.join(pkgRoot, 'setup', '.codex');
199
+ const globalDir = path.join(home, '.flowcraft');
200
+ const hooksDir = path.join(globalDir, 'hooks');
201
+ console.log('[全局模式] 安装 FlowCraft 到用户级(所有项目共享)');
202
+ console.log(` 全局目录: ${globalDir}`);
203
+ console.log(` 用户配置: ${path.join(home, '.claude', 'settings.json')}`);
204
+ console.log('');
205
+ if (!fs.existsSync(path.join(setupClaude, 'hooks'))) throw new Error('setup/.claude/hooks 目录不存在');
206
+ copyDir(path.join(setupClaude, 'hooks'), hooksDir);
207
+ console.log(`✓ hooks 已复制到 ${hooksDir}`);
208
+ fixLineEndings(hooksDir);
209
+ replacePythonCommands(hooksDir, pythonCmd);
210
+ let pyCmd = pythonCmd || 'python';
211
+ if (process.platform === 'win32' && (pyCmd === 'python3' || pyCmd === 'py')) pyCmd = 'python';
212
+ const dispatchPath = path.join(hooksDir, 'dispatch.py').replace(/\\/g, '/');
213
+ mergeClaudeSettings(path.join(home, '.claude', 'settings.json'), `${pyCmd} ${dispatchPath}`);
214
+ const tplDir = path.join(globalDir, 'templates');
215
+ fs.mkdirSync(tplDir, { recursive: true });
216
+ for (const tpl of ['CLAUDE.md', 'AGENTS.md']) {
217
+ const src = path.join(pkgRoot, 'setup', tpl);
218
+ if (fs.existsSync(src)) fs.copyFileSync(src, path.join(tplDir, tpl));
219
+ }
220
+ fs.copyFileSync(path.join(setupClaude, 'project-state.md'), path.join(tplDir, 'claude-project-state.md'));
221
+ fs.copyFileSync(path.join(setupCodex, 'project-state.md'), path.join(tplDir, 'codex-project-state.md'));
222
+ console.log(`✓ 模板目录已创建: ${tplDir}`);
223
+ installGlobalSkills(setupClaude, setupCodex, home);
224
+ installGlobalCodexHooks(setupCodex, globalDir, home);
225
+ mergeCodexFeatures(path.join(home, '.codex', 'config.toml'));
226
+ writeGlobalVersion(globalDir);
227
+ }
228
+
229
+ module.exports = { buildHookSettings, mergeClaudeSettings, mergeCodexHooksJson, mergeCodexFeatures, installGlobalSkills, installGlobalCodexHooks, writeGlobalVersion, installGlobal };
package/lib/init.js CHANGED
@@ -1,43 +1,43 @@
1
- /**
2
- * init.js — flowcraft init 最小化初始化(R2:对齐 install.sh --init/init_project)
3
- */
4
- 'use strict';
5
-
6
- const fs = require('node:fs');
7
- const path = require('node:path');
8
-
9
- /**
10
- * 在目标目录创建最小化 .claude 与 flowcraft.yaml(已存在一律保留)
11
- * @param {string} pkgRoot 包根(模板来源)
12
- * @param {string} target 目标目录(默认 cwd)
13
- * @returns {void}
14
- */
15
- function initProject(pkgRoot, target) {
16
- const dest = target || process.cwd();
17
- const claudeDir = path.join(dest, '.claude');
18
- console.log('[初始化] 在当前目录创建 FlowCraft 项目文件...');
19
- console.log(` 目录: ${dest}`);
20
- console.log('');
21
- fs.mkdirSync(claudeDir, { recursive: true });
22
- const stateDst = path.join(claudeDir, 'project-state.md');
23
- const stateSrc = path.join(pkgRoot, 'setup', '.claude', 'project-state.md');
24
- if (fs.existsSync(stateDst)) console.log(' 保留 .claude/project-state.md(已存在)');
25
- else if (fs.existsSync(stateSrc)) {
26
- fs.copyFileSync(stateSrc, stateDst);
27
- console.log(' ✓ .claude/project-state.md');
28
- }
29
- for (const d of ['reports', 'reviews', 'archive']) fs.mkdirSync(path.join(claudeDir, d), { recursive: true });
30
- console.log(' ✓ reports/ reviews/ archive/');
31
- const cfgDst = path.join(dest, 'flowcraft.yaml');
32
- const cfgSrc = path.join(pkgRoot, 'flowcraft.yaml');
33
- if (fs.existsSync(cfgDst)) console.log(' 保留 flowcraft.yaml(已存在)');
34
- else if (fs.existsSync(cfgSrc)) {
35
- fs.copyFileSync(cfgSrc, cfgDst);
36
- console.log(' ✓ flowcraft.yaml');
37
- }
38
- console.log('');
39
- console.log('项目初始化完成!');
40
- console.log(' 下一步: 1. 重启 Claude Code 2. 输入任务,SDLC 框架自动激活');
41
- }
42
-
43
- module.exports = { initProject };
1
+ /**
2
+ * init.js — flowcraft init 最小化初始化(R2:对齐 install.sh --init/init_project)
3
+ */
4
+ 'use strict';
5
+
6
+ const fs = require('node:fs');
7
+ const path = require('node:path');
8
+
9
+ /**
10
+ * 在目标目录创建最小化 .claude 与 flowcraft.yaml(已存在一律保留)
11
+ * @param {string} pkgRoot 包根(模板来源)
12
+ * @param {string} target 目标目录(默认 cwd)
13
+ * @returns {void}
14
+ */
15
+ function initProject(pkgRoot, target) {
16
+ const dest = target || process.cwd();
17
+ const claudeDir = path.join(dest, '.claude');
18
+ console.log('[初始化] 在当前目录创建 FlowCraft 项目文件...');
19
+ console.log(` 目录: ${dest}`);
20
+ console.log('');
21
+ fs.mkdirSync(claudeDir, { recursive: true });
22
+ const stateDst = path.join(claudeDir, 'project-state.md');
23
+ const stateSrc = path.join(pkgRoot, 'setup', '.claude', 'project-state.md');
24
+ if (fs.existsSync(stateDst)) console.log(' 保留 .claude/project-state.md(已存在)');
25
+ else if (fs.existsSync(stateSrc)) {
26
+ fs.copyFileSync(stateSrc, stateDst);
27
+ console.log(' ✓ .claude/project-state.md');
28
+ }
29
+ for (const d of ['reports', 'reviews', 'archive']) fs.mkdirSync(path.join(claudeDir, d), { recursive: true });
30
+ console.log(' ✓ reports/ reviews/ archive/');
31
+ const cfgDst = path.join(dest, 'flowcraft.yaml');
32
+ const cfgSrc = path.join(pkgRoot, 'flowcraft.yaml');
33
+ if (fs.existsSync(cfgDst)) console.log(' 保留 flowcraft.yaml(已存在)');
34
+ else if (fs.existsSync(cfgSrc)) {
35
+ fs.copyFileSync(cfgSrc, cfgDst);
36
+ console.log(' ✓ flowcraft.yaml');
37
+ }
38
+ console.log('');
39
+ console.log('项目初始化完成!');
40
+ console.log(' 下一步: 1. 重启 Claude Code 2. 输入任务,SDLC 框架自动激活');
41
+ }
42
+
43
+ module.exports = { initProject };