@leocm/flowcraft 2.2.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 +7 -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 -162
  25. package/setup/.claude/hooks/permission-request.py +132 -132
  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 -186
  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 -91
  37. package/setup/.claude/settings.json +233 -233
  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 -199
  96. package/setup/tests/test_task_manager.py +148 -148
package/lib/copy.js CHANGED
@@ -1,131 +1,131 @@
1
- /**
2
- * copy.js — 项目级安装复制(R4:项目数据永不覆盖/备份策略,对齐 copy_claude_files/copy_codex_files)
3
- */
4
- 'use strict';
5
-
6
- const fs = require('node:fs');
7
- const path = require('node:path');
8
- const { timestamp, backupFile, copyDir } = require('./fsutil');
9
-
10
- /**
11
- * 复制 setup/.claude 到项目 .claude(项目数据永不覆盖/不删除/不备份)
12
- * @param {string} setupClaude 源目录(setup/.claude,其父目录含 CLAUDE.md)
13
- * @param {string} projectRoot 项目根
14
- * @returns {number} 目标 .claude 内文件总数
15
- */
16
- function copyClaudeFiles(setupClaude, projectRoot) {
17
- const setupRoot = path.dirname(setupClaude);
18
- console.log('[1/3] 复制 setup/.claude 到项目根目录(项目数据永不覆盖)...');
19
- if (!fs.existsSync(setupClaude)) throw new Error(`setup/.claude 目录不存在: ${setupClaude}`);
20
- const target = path.join(projectRoot, '.claude');
21
- fs.mkdirSync(target, { recursive: true });
22
- const ts = timestamp();
23
- for (const d of ['reports', 'reviews', 'archive']) fs.mkdirSync(path.join(target, d), { recursive: true });
24
- for (const f of ['project-state.md', 'settings.local.json']) {
25
- const dst = path.join(target, f);
26
- if (fs.existsSync(dst)) {
27
- if (f === 'project-state.md') backupFile(dst, ts);
28
- console.log(` 保留 ${f}(不覆盖)`);
29
- } else if (fs.existsSync(path.join(setupClaude, f))) {
30
- fs.copyFileSync(path.join(setupClaude, f), dst);
31
- }
32
- }
33
- for (const sub of ['hooks', 'commands', 'skills', 'sdlc']) fs.mkdirSync(path.join(target, sub), { recursive: true });
34
- const settingsSrc = path.join(setupClaude, 'settings.json');
35
- if (fs.existsSync(settingsSrc)) {
36
- backupFile(path.join(target, 'settings.json'), ts);
37
- fs.copyFileSync(settingsSrc, path.join(target, 'settings.json'));
38
- }
39
- const claudeMd = path.join(setupRoot, 'CLAUDE.md');
40
- if (fs.existsSync(claudeMd)) {
41
- backupFile(path.join(projectRoot, 'CLAUDE.md'), ts);
42
- fs.copyFileSync(claudeMd, path.join(projectRoot, 'CLAUDE.md'));
43
- console.log(' ✓ CLAUDE.md');
44
- }
45
- const count = countFiles(target);
46
- console.log(`✓ 复制完成: ${count} 个文件(项目数据已保护)`);
47
- return count;
48
- }
49
-
50
- /**
51
- * 统计目录内文件数
52
- * @param {string} dir 目录
53
- * @returns {number} 文件数
54
- */
55
- function countFiles(dir) {
56
- let n = 0;
57
- for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
58
- n += e.isDirectory() ? countFiles(path.join(dir, e.name)) : 1;
59
- }
60
- return n;
61
- }
62
-
63
- /**
64
- * 复制 setup/.codex 与 AGENTS.md 到项目根(Codex 支持,项目数据永不覆盖)
65
- * @param {string} setupCodex 源目录(setup/.codex,其父目录含 AGENTS.md)
66
- * @param {string} projectRoot 项目根
67
- * @returns {void}
68
- */
69
- function copyCodexFiles(setupCodex, projectRoot) {
70
- const setupRoot = path.dirname(setupCodex);
71
- console.log('[Codex] 复制 setup/.codex 与 AGENTS.md 到项目根目录(项目数据永不覆盖)...');
72
- if (!fs.existsSync(setupCodex)) {
73
- console.log(' 跳过: setup/.codex 目录不存在');
74
- return;
75
- }
76
- const target = path.join(projectRoot, '.codex');
77
- fs.mkdirSync(target, { recursive: true });
78
- const ts = timestamp();
79
- const stateDst = path.join(target, 'project-state.md');
80
- if (fs.existsSync(stateDst)) {
81
- backupFile(stateDst, ts);
82
- console.log(' 保留 .codex/project-state.md(不覆盖)');
83
- } else if (fs.existsSync(path.join(setupCodex, 'project-state.md'))) {
84
- fs.copyFileSync(path.join(setupCodex, 'project-state.md'), stateDst);
85
- }
86
- for (const f of ['config.toml', 'model-catalog.json']) {
87
- const dst = path.join(target, f);
88
- if (fs.existsSync(dst)) console.log(` 保留 .codex/${f}(不覆盖)`);
89
- else if (fs.existsSync(path.join(setupCodex, f))) fs.copyFileSync(path.join(setupCodex, f), dst);
90
- }
91
- fs.mkdirSync(path.join(target, 'hooks'), { recursive: true });
92
- console.log(' ✓ .codex(hooks 框架文件已更新,项目数据已保护)');
93
- const agentsSrc = path.join(setupRoot, 'AGENTS.md');
94
- if (fs.existsSync(agentsSrc)) {
95
- const agentsDst = path.join(projectRoot, 'AGENTS.md');
96
- if (fs.existsSync(agentsDst)) {
97
- fs.copyFileSync(agentsDst, `${agentsDst}.bak`);
98
- console.log(' ✓ AGENTS.md.bak(旧文件备份)');
99
- }
100
- fs.copyFileSync(agentsSrc, agentsDst);
101
- console.log(' ✓ AGENTS.md(已强制覆盖)');
102
- }
103
- fs.mkdirSync(path.join(projectRoot, '.agents', 'skills'), { recursive: true });
104
- console.log(' ✓ .agents/skills(空目录,用户级兜底)');
105
- }
106
-
107
- /**
108
- * 复制 flowcraft.yaml(不覆盖已有配置;指定预设时应用预设内容)
109
- * @param {string} pkgRoot 包根(含 flowcraft.yaml 与 presets/)
110
- * @param {string} projectRoot 项目根
111
- * @param {string|null} presetFile 已解析的预设文件路径
112
- * @returns {void}
113
- */
114
- function copyFlowcraftConfig(pkgRoot, projectRoot, presetFile) {
115
- const src = path.join(pkgRoot, 'flowcraft.yaml');
116
- const dst = path.join(projectRoot, 'flowcraft.yaml');
117
- if (!fs.existsSync(src)) return;
118
- if (fs.existsSync(dst)) {
119
- console.log('! flowcraft.yaml 已存在,保留现有配置');
120
- return;
121
- }
122
- if (presetFile && fs.existsSync(presetFile)) {
123
- fs.copyFileSync(presetFile, dst);
124
- console.log(`✓ 已应用预设: ${presetFile}`);
125
- } else {
126
- fs.copyFileSync(src, dst);
127
- console.log('✓ 已生成 flowcraft.yaml(默认配置)');
128
- }
129
- }
130
-
131
- module.exports = { copyClaudeFiles, copyCodexFiles, copyFlowcraftConfig };
1
+ /**
2
+ * copy.js — 项目级安装复制(R4:项目数据永不覆盖/备份策略,对齐 copy_claude_files/copy_codex_files)
3
+ */
4
+ 'use strict';
5
+
6
+ const fs = require('node:fs');
7
+ const path = require('node:path');
8
+ const { timestamp, backupFile, copyDir } = require('./fsutil');
9
+
10
+ /**
11
+ * 复制 setup/.claude 到项目 .claude(项目数据永不覆盖/不删除/不备份)
12
+ * @param {string} setupClaude 源目录(setup/.claude,其父目录含 CLAUDE.md)
13
+ * @param {string} projectRoot 项目根
14
+ * @returns {number} 目标 .claude 内文件总数
15
+ */
16
+ function copyClaudeFiles(setupClaude, projectRoot) {
17
+ const setupRoot = path.dirname(setupClaude);
18
+ console.log('[1/3] 复制 setup/.claude 到项目根目录(项目数据永不覆盖)...');
19
+ if (!fs.existsSync(setupClaude)) throw new Error(`setup/.claude 目录不存在: ${setupClaude}`);
20
+ const target = path.join(projectRoot, '.claude');
21
+ fs.mkdirSync(target, { recursive: true });
22
+ const ts = timestamp();
23
+ for (const d of ['reports', 'reviews', 'archive']) fs.mkdirSync(path.join(target, d), { recursive: true });
24
+ for (const f of ['project-state.md', 'settings.local.json']) {
25
+ const dst = path.join(target, f);
26
+ if (fs.existsSync(dst)) {
27
+ if (f === 'project-state.md') backupFile(dst, ts);
28
+ console.log(` 保留 ${f}(不覆盖)`);
29
+ } else if (fs.existsSync(path.join(setupClaude, f))) {
30
+ fs.copyFileSync(path.join(setupClaude, f), dst);
31
+ }
32
+ }
33
+ for (const sub of ['hooks', 'commands', 'skills', 'sdlc']) fs.mkdirSync(path.join(target, sub), { recursive: true });
34
+ const settingsSrc = path.join(setupClaude, 'settings.json');
35
+ if (fs.existsSync(settingsSrc)) {
36
+ backupFile(path.join(target, 'settings.json'), ts);
37
+ fs.copyFileSync(settingsSrc, path.join(target, 'settings.json'));
38
+ }
39
+ const claudeMd = path.join(setupRoot, 'CLAUDE.md');
40
+ if (fs.existsSync(claudeMd)) {
41
+ backupFile(path.join(projectRoot, 'CLAUDE.md'), ts);
42
+ fs.copyFileSync(claudeMd, path.join(projectRoot, 'CLAUDE.md'));
43
+ console.log(' ✓ CLAUDE.md');
44
+ }
45
+ const count = countFiles(target);
46
+ console.log(`✓ 复制完成: ${count} 个文件(项目数据已保护)`);
47
+ return count;
48
+ }
49
+
50
+ /**
51
+ * 统计目录内文件数
52
+ * @param {string} dir 目录
53
+ * @returns {number} 文件数
54
+ */
55
+ function countFiles(dir) {
56
+ let n = 0;
57
+ for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
58
+ n += e.isDirectory() ? countFiles(path.join(dir, e.name)) : 1;
59
+ }
60
+ return n;
61
+ }
62
+
63
+ /**
64
+ * 复制 setup/.codex 与 AGENTS.md 到项目根(Codex 支持,项目数据永不覆盖)
65
+ * @param {string} setupCodex 源目录(setup/.codex,其父目录含 AGENTS.md)
66
+ * @param {string} projectRoot 项目根
67
+ * @returns {void}
68
+ */
69
+ function copyCodexFiles(setupCodex, projectRoot) {
70
+ const setupRoot = path.dirname(setupCodex);
71
+ console.log('[Codex] 复制 setup/.codex 与 AGENTS.md 到项目根目录(项目数据永不覆盖)...');
72
+ if (!fs.existsSync(setupCodex)) {
73
+ console.log(' 跳过: setup/.codex 目录不存在');
74
+ return;
75
+ }
76
+ const target = path.join(projectRoot, '.codex');
77
+ fs.mkdirSync(target, { recursive: true });
78
+ const ts = timestamp();
79
+ const stateDst = path.join(target, 'project-state.md');
80
+ if (fs.existsSync(stateDst)) {
81
+ backupFile(stateDst, ts);
82
+ console.log(' 保留 .codex/project-state.md(不覆盖)');
83
+ } else if (fs.existsSync(path.join(setupCodex, 'project-state.md'))) {
84
+ fs.copyFileSync(path.join(setupCodex, 'project-state.md'), stateDst);
85
+ }
86
+ for (const f of ['config.toml', 'model-catalog.json']) {
87
+ const dst = path.join(target, f);
88
+ if (fs.existsSync(dst)) console.log(` 保留 .codex/${f}(不覆盖)`);
89
+ else if (fs.existsSync(path.join(setupCodex, f))) fs.copyFileSync(path.join(setupCodex, f), dst);
90
+ }
91
+ fs.mkdirSync(path.join(target, 'hooks'), { recursive: true });
92
+ console.log(' ✓ .codex(hooks 框架文件已更新,项目数据已保护)');
93
+ const agentsSrc = path.join(setupRoot, 'AGENTS.md');
94
+ if (fs.existsSync(agentsSrc)) {
95
+ const agentsDst = path.join(projectRoot, 'AGENTS.md');
96
+ if (fs.existsSync(agentsDst)) {
97
+ fs.copyFileSync(agentsDst, `${agentsDst}.bak`);
98
+ console.log(' ✓ AGENTS.md.bak(旧文件备份)');
99
+ }
100
+ fs.copyFileSync(agentsSrc, agentsDst);
101
+ console.log(' ✓ AGENTS.md(已强制覆盖)');
102
+ }
103
+ fs.mkdirSync(path.join(projectRoot, '.agents', 'skills'), { recursive: true });
104
+ console.log(' ✓ .agents/skills(空目录,用户级兜底)');
105
+ }
106
+
107
+ /**
108
+ * 复制 flowcraft.yaml(不覆盖已有配置;指定预设时应用预设内容)
109
+ * @param {string} pkgRoot 包根(含 flowcraft.yaml 与 presets/)
110
+ * @param {string} projectRoot 项目根
111
+ * @param {string|null} presetFile 已解析的预设文件路径
112
+ * @returns {void}
113
+ */
114
+ function copyFlowcraftConfig(pkgRoot, projectRoot, presetFile) {
115
+ const src = path.join(pkgRoot, 'flowcraft.yaml');
116
+ const dst = path.join(projectRoot, 'flowcraft.yaml');
117
+ if (!fs.existsSync(src)) return;
118
+ if (fs.existsSync(dst)) {
119
+ console.log('! flowcraft.yaml 已存在,保留现有配置');
120
+ return;
121
+ }
122
+ if (presetFile && fs.existsSync(presetFile)) {
123
+ fs.copyFileSync(presetFile, dst);
124
+ console.log(`✓ 已应用预设: ${presetFile}`);
125
+ } else {
126
+ fs.copyFileSync(src, dst);
127
+ console.log('✓ 已生成 flowcraft.yaml(默认配置)');
128
+ }
129
+ }
130
+
131
+ module.exports = { copyClaudeFiles, copyCodexFiles, copyFlowcraftConfig };
package/lib/fix.js CHANGED
@@ -1,110 +1,110 @@
1
- /**
2
- * fix.js — 修复类函数(R4:行尾符/python 命令/SESSIONLOG_URL/PATH 注入)
3
- */
4
- 'use strict';
5
-
6
- const fs = require('node:fs');
7
- const path = require('node:path');
8
- const { hasCrlf, isTextFile, listFiles } = require('./fsutil');
9
- const { HOOK_FILES } = require('./config');
10
-
11
- /**
12
- * 修复行尾符(CRLF → LF,.py 与 .ps1,对齐 install.sh fix_line_endings)
13
- * @param {string} targetDir 目标目录(.claude 或全局 hooks 仓)
14
- * @returns {number} 修复文件数
15
- */
16
- function fixLineEndings(targetDir) {
17
- console.log('[2/3] 修复行尾符(CRLF -> LF)...');
18
- let n = 0;
19
- for (const f of listFiles(targetDir)) {
20
- if (!/\.(py|ps1)$/i.test(f)) continue;
21
- const buf = fs.readFileSync(f);
22
- if (!hasCrlf(buf)) continue;
23
- fs.writeFileSync(f, Buffer.from(buf.toString('binary').replace(/\r/g, ''), 'binary'));
24
- console.log(` ✓ ${f}`);
25
- n += 1;
26
- }
27
- console.log(`✓ 行尾符修复完成(修复 ${n} 个文件)`);
28
- return n;
29
- }
30
-
31
- /**
32
- * 替换 Python 命令(python3/python 均替换为探测命令,对齐 sed 保护式替换)
33
- * 非 UTF-8 文本跳过并告警,避免解码损坏(.claude 内文件约定 UTF-8)
34
- * @param {string} targetDir 目标目录
35
- * @param {string} pythonCmd 探测到的 python 命令
36
- * @returns {number} 修改文件数
37
- */
38
- function replacePythonCommands(targetDir, pythonCmd) {
39
- console.log(`[3/3] 替换 Python 命令为: ${pythonCmd}`);
40
- let n = 0;
41
- for (const f of listFiles(targetDir)) {
42
- if (!isTextFile(f)) continue;
43
- const buf = fs.readFileSync(f);
44
- let text;
45
- try {
46
- text = new TextDecoder('utf-8', { fatal: true }).decode(buf);
47
- } catch {
48
- console.log(` 跳过(非UTF-8): ${f}`);
49
- continue;
50
- }
51
- if (!/python3|python/.test(text)) continue;
52
- fs.writeFileSync(f, text.replace(/python3|python/g, pythonCmd));
53
- n += 1;
54
- console.log(` ✓ ${f}`);
55
- }
56
- console.log(`✓ 替换完成(修改 ${n} 个文件)`);
57
- return n;
58
- }
59
-
60
- /**
61
- * 分发 sessionlog URL:.py 中 MCP_URL= 的值替换为新 URL(目标与 setup 源同步)
62
- * @param {string} url 新的 MCP 地址
63
- * @param {...string} dirs 要处理的目录列表
64
- * @returns {number} 更新文件数
65
- */
66
- function replaceSessionlogUrl(url, ...dirs) {
67
- console.log(`[4/4] 分发 sessionlog URL: ${url}`);
68
- let n = 0;
69
- for (const dir of dirs) {
70
- for (const f of listFiles(dir)) {
71
- if (!/\.py$/i.test(f)) continue;
72
- const text = fs.readFileSync(f, 'utf8');
73
- if (!/MCP_URL\s*=/.test(text)) continue;
74
- const out = text.replace(/MCP_URL\s*=\s*".*/, `MCP_URL="${url}"`);
75
- if (out !== text) {
76
- fs.writeFileSync(f, out);
77
- n += 1;
78
- console.log(` ✓ ${f}`);
79
- }
80
- }
81
- }
82
- console.log(`✓ URL 分发完成(更新 ${n} 个文件)`);
83
- return n;
84
- }
85
-
86
- /**
87
- * 对 bash 型 hook 注入 PATH 设置(Python 脚本跳过,避免破坏语法)
88
- * @param {string} targetDir .claude 目录
89
- * @returns {number} 注入文件数
90
- */
91
- function addPathToHooks(targetDir) {
92
- console.log('[2/3] 添加 PATH 设置到 hook 文件...');
93
- let n = 0;
94
- for (const name of HOOK_FILES) {
95
- const f = path.join(targetDir, 'hooks', name);
96
- if (!fs.existsSync(f)) continue;
97
- const lines = fs.readFileSync(f, 'utf8').split('\n');
98
- const shebang = lines[0] || '';
99
- if (!/^#!.*(bash|sh)/.test(shebang)) continue;
100
- if (lines.some((l) => l.includes('export PATH='))) continue;
101
- lines.splice(1, 0, 'export PATH="/usr/local/bin:/usr/bin:/bin:$PATH"');
102
- fs.writeFileSync(f, lines.join('\n'));
103
- console.log(` ✓ ${name}`);
104
- n += 1;
105
- }
106
- console.log('✓ PATH 设置添加完成');
107
- return n;
108
- }
109
-
110
- module.exports = { fixLineEndings, replacePythonCommands, replaceSessionlogUrl, addPathToHooks };
1
+ /**
2
+ * fix.js — 修复类函数(R4:行尾符/python 命令/SESSIONLOG_URL/PATH 注入)
3
+ */
4
+ 'use strict';
5
+
6
+ const fs = require('node:fs');
7
+ const path = require('node:path');
8
+ const { hasCrlf, isTextFile, listFiles } = require('./fsutil');
9
+ const { HOOK_FILES } = require('./config');
10
+
11
+ /**
12
+ * 修复行尾符(CRLF → LF,.py 与 .ps1,对齐 install.sh fix_line_endings)
13
+ * @param {string} targetDir 目标目录(.claude 或全局 hooks 仓)
14
+ * @returns {number} 修复文件数
15
+ */
16
+ function fixLineEndings(targetDir) {
17
+ console.log('[2/3] 修复行尾符(CRLF -> LF)...');
18
+ let n = 0;
19
+ for (const f of listFiles(targetDir)) {
20
+ if (!/\.(py|ps1)$/i.test(f)) continue;
21
+ const buf = fs.readFileSync(f);
22
+ if (!hasCrlf(buf)) continue;
23
+ fs.writeFileSync(f, Buffer.from(buf.toString('binary').replace(/\r/g, ''), 'binary'));
24
+ console.log(` ✓ ${f}`);
25
+ n += 1;
26
+ }
27
+ console.log(`✓ 行尾符修复完成(修复 ${n} 个文件)`);
28
+ return n;
29
+ }
30
+
31
+ /**
32
+ * 替换 Python 命令(python3/python 均替换为探测命令,对齐 sed 保护式替换)
33
+ * 非 UTF-8 文本跳过并告警,避免解码损坏(.claude 内文件约定 UTF-8)
34
+ * @param {string} targetDir 目标目录
35
+ * @param {string} pythonCmd 探测到的 python 命令
36
+ * @returns {number} 修改文件数
37
+ */
38
+ function replacePythonCommands(targetDir, pythonCmd) {
39
+ console.log(`[3/3] 替换 Python 命令为: ${pythonCmd}`);
40
+ let n = 0;
41
+ for (const f of listFiles(targetDir)) {
42
+ if (!isTextFile(f)) continue;
43
+ const buf = fs.readFileSync(f);
44
+ let text;
45
+ try {
46
+ text = new TextDecoder('utf-8', { fatal: true }).decode(buf);
47
+ } catch {
48
+ console.log(` 跳过(非UTF-8): ${f}`);
49
+ continue;
50
+ }
51
+ if (!/python3|python/.test(text)) continue;
52
+ fs.writeFileSync(f, text.replace(/python3|python/g, pythonCmd));
53
+ n += 1;
54
+ console.log(` ✓ ${f}`);
55
+ }
56
+ console.log(`✓ 替换完成(修改 ${n} 个文件)`);
57
+ return n;
58
+ }
59
+
60
+ /**
61
+ * 分发 sessionlog URL:.py 中 MCP_URL= 的值替换为新 URL(目标与 setup 源同步)
62
+ * @param {string} url 新的 MCP 地址
63
+ * @param {...string} dirs 要处理的目录列表
64
+ * @returns {number} 更新文件数
65
+ */
66
+ function replaceSessionlogUrl(url, ...dirs) {
67
+ console.log(`[4/4] 分发 sessionlog URL: ${url}`);
68
+ let n = 0;
69
+ for (const dir of dirs) {
70
+ for (const f of listFiles(dir)) {
71
+ if (!/\.py$/i.test(f)) continue;
72
+ const text = fs.readFileSync(f, 'utf8');
73
+ if (!/MCP_URL\s*=/.test(text)) continue;
74
+ const out = text.replace(/MCP_URL\s*=\s*".*/, `MCP_URL="${url}"`);
75
+ if (out !== text) {
76
+ fs.writeFileSync(f, out);
77
+ n += 1;
78
+ console.log(` ✓ ${f}`);
79
+ }
80
+ }
81
+ }
82
+ console.log(`✓ URL 分发完成(更新 ${n} 个文件)`);
83
+ return n;
84
+ }
85
+
86
+ /**
87
+ * 对 bash 型 hook 注入 PATH 设置(Python 脚本跳过,避免破坏语法)
88
+ * @param {string} targetDir .claude 目录
89
+ * @returns {number} 注入文件数
90
+ */
91
+ function addPathToHooks(targetDir) {
92
+ console.log('[2/3] 添加 PATH 设置到 hook 文件...');
93
+ let n = 0;
94
+ for (const name of HOOK_FILES) {
95
+ const f = path.join(targetDir, 'hooks', name);
96
+ if (!fs.existsSync(f)) continue;
97
+ const lines = fs.readFileSync(f, 'utf8').split('\n');
98
+ const shebang = lines[0] || '';
99
+ if (!/^#!.*(bash|sh)/.test(shebang)) continue;
100
+ if (lines.some((l) => l.includes('export PATH='))) continue;
101
+ lines.splice(1, 0, 'export PATH="/usr/local/bin:/usr/bin:/bin:$PATH"');
102
+ fs.writeFileSync(f, lines.join('\n'));
103
+ console.log(` ✓ ${name}`);
104
+ n += 1;
105
+ }
106
+ console.log('✓ PATH 设置添加完成');
107
+ return n;
108
+ }
109
+
110
+ module.exports = { fixLineEndings, replacePythonCommands, replaceSessionlogUrl, addPathToHooks };