flow-comet 1.5.0-rc.3
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.
- package/.flow-comet/rules/flow-comet-orchestration.md +13 -0
- package/.flow-comet/skills/flow-comet/GUIDANCE.md +196 -0
- package/.flow-comet/skills/flow-comet/INSTALLED_VERSION +1 -0
- package/.flow-comet/skills/flow-comet/SKILL.md +196 -0
- package/.flow-comet/skills/flow-comet/reference/KNOWN-ISSUES-template.md +19 -0
- package/.flow-comet/skills/flow-comet/reference/TEST-MATRIX.md +115 -0
- package/.flow-comet/skills/flow-comet/reference/authoring-lanes.json +33 -0
- package/.flow-comet/skills/flow-comet/reference/composition-report.md +65 -0
- package/.flow-comet/skills/flow-comet/reference/decision-points.md +64 -0
- package/.flow-comet/skills/flow-comet/reference/dirty-worktree.md +30 -0
- package/.flow-comet/skills/flow-comet/reference/dsh-platform.md +169 -0
- package/.flow-comet/skills/flow-comet/reference/init-detection.md +58 -0
- package/.flow-comet/skills/flow-comet/reference/recovery.md +20 -0
- package/.flow-comet/skills/flow-comet/reference/resolved-skills.json +661 -0
- package/.flow-comet/skills/flow-comet/reference/skill-review.md +13 -0
- package/.flow-comet/skills/flow-comet/reference/subagents/script-author.md +7 -0
- package/.flow-comet/skills/flow-comet/reference/workflow-protocol.json +681 -0
- package/.flow-comet/skills/flow-comet/reference/worktree-notes.md +52 -0
- package/.flow-comet/skills/flow-comet/scripts/comet-check.mjs +61 -0
- package/.flow-comet/skills/flow-comet/scripts/comet-hook-guard.mjs +909 -0
- package/.flow-comet/skills/flow-comet/scripts/comet-plan.mjs +2 -0
- package/.flow-comet/skills/flow-comet/scripts/context-init.mjs +312 -0
- package/.flow-comet/skills/flow-comet/scripts/contract-check.mjs +136 -0
- package/.flow-comet/skills/flow-comet/scripts/guard-self-test.mjs +7331 -0
- package/.flow-comet/skills/flow-comet/scripts/protocol-utils.mjs +376 -0
- package/.flow-comet/skills/flow-comet/scripts/route-node.mjs +230 -0
- package/.flow-comet/skills/flow-comet/scripts/state-schema.mjs +99 -0
- package/.flow-comet/skills/flow-comet/scripts/system-test.mjs +4017 -0
- package/.flow-comet/skills/flow-comet/scripts/task-parsing.mjs +28 -0
- package/.flow-comet/skills/flow-comet/scripts/workflow-guard.mjs +2396 -0
- package/.flow-comet/skills/flow-comet/scripts/workflow-handoff.mjs +378 -0
- package/.flow-comet/skills/flow-comet/scripts/workflow-state.mjs +1263 -0
- package/.flow-comet/skills/flow-comet-archive/SKILL.md +195 -0
- package/.flow-comet/skills/flow-comet-change/SKILL.md +29 -0
- package/.flow-comet/skills/flow-comet-compose/SKILL.md +249 -0
- package/.flow-comet/skills/flow-comet-design/GUIDANCE.md +23 -0
- package/.flow-comet/skills/flow-comet-design/SKILL.md +161 -0
- package/.flow-comet/skills/flow-comet-dev/SKILL.md +51 -0
- package/.flow-comet/skills/flow-comet-evolve/SKILL.md +26 -0
- package/.flow-comet/skills/flow-comet-execute/SKILL.md +182 -0
- package/.flow-comet/skills/flow-comet-health/SKILL.md +31 -0
- package/.flow-comet/skills/flow-comet-integration/SKILL.md +45 -0
- package/.flow-comet/skills/flow-comet-open/GUIDANCE.md +36 -0
- package/.flow-comet/skills/flow-comet-open/SKILL.md +161 -0
- package/.flow-comet/skills/flow-comet-plan/GUIDANCE.md +33 -0
- package/.flow-comet/skills/flow-comet-plan/SKILL.md +169 -0
- package/.flow-comet/skills/flow-comet-requirement/SKILL.md +27 -0
- package/.flow-comet/skills/flow-comet-review/SKILL.md +204 -0
- package/.flow-comet/skills/flow-comet-subagent-execute/GUIDANCE.md +29 -0
- package/.flow-comet/skills/flow-comet-subagent-execute/SKILL.md +214 -0
- package/.flow-comet/skills/flow-comet-task/SKILL.md +33 -0
- package/.flow-comet/skills/flow-comet-test/SKILL.md +41 -0
- package/.flow-comet/skills/flow-comet-ui-design/SKILL.md +26 -0
- package/.flow-comet/skills/flow-comet-verify/SKILL.md +164 -0
- package/LICENSE +21 -0
- package/README.md +255 -0
- package/package.json +27 -0
- package/scripts/dsh-bridge.mjs +461 -0
- package/scripts/prepare-env.mjs +1526 -0
|
@@ -0,0 +1,1526 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* prepare-env.mjs — flow-comet 环境安装脚本(T-FIX-08 创建 / T-FIX-13 非破坏化 / 1.4.0 多平台化)
|
|
4
|
+
*
|
|
5
|
+
* 用途:从权威源 `.flow-comet/`(含 skills/ 与 rules/)安装/更新目标仓库的环境,
|
|
6
|
+
* 使目标环境获得完整约束——rules 行为层 + hook 物理层 + skills 协议层。
|
|
7
|
+
* 这是 flow-comet 的**安装器**:适用于新项目安装 flow-comet、e2e 验证载体准备、
|
|
8
|
+
* 源仓库自我改进 worktree 环境准备。
|
|
9
|
+
*
|
|
10
|
+
* 多平台(1.4.0 / dsh 扩展):平台描述符表 PLATFORMS 驱动——claude-code(默认,现状语义)
|
|
11
|
+
* + codex + dsh(新增)。平台差异全部封装在描述符条目内:技能安装位置 skillRoot /
|
|
12
|
+
* SKILL 命令路径替换 pathReplacements / hook 注入 installHooks
|
|
13
|
+
* (CC=settings.local.json;Codex=.codex/hooks.json + config.toml 启用;
|
|
14
|
+
* dsh=$DSH_HOME/plugins/ 桥接 loader + cordis.patch.yml 托管块——全局挂载) / rules 注入
|
|
15
|
+
* installRules (CC=.claude/rules 自动加载;Codex/.dsh=AGENTS.md 托管区共用注入,
|
|
16
|
+
* .codex/rules 为命令批准规则目录,不混用) / 清理 purge / 覆盖清单 overwriteDescription——
|
|
17
|
+
* main 统一调度,新增平台 = 描述符条目 + 安装/清理函数,main 零改动。
|
|
18
|
+
*
|
|
19
|
+
* hook 命令项目根锚定:CC 命令引用 CC 注入的 CLAUDE_PROJECT_DIR 运行期展开(全平台 bash
|
|
20
|
+
* ${VAR} 词形——CC 以 /bin/bash -c 执行 hook 命令,含 Windows;单一来源常量派生,
|
|
21
|
+
* 见 PROJECT_ROOT_ENV_VAR);Codex 官方 hooks 无等价
|
|
22
|
+
* 项目根变量(查证 openai/codex 上游执行层,结论与出处见 injectCodexHook 注释)→ 安装期
|
|
23
|
+
* 字面注入 target 绝对路径(迁移目录需重跑安装器)。两平台命令均不加外层引号,以保住
|
|
24
|
+
* isManagedHookCommand 的 basename 幂等识别(容忍外层引号——含空格路径支持,见常量区说明)。
|
|
25
|
+
*
|
|
26
|
+
* 非破坏设计(T-FIX-13,2026-08-08 用户裁决):
|
|
27
|
+
* - 默认(无 --purge):**不删除整个 .claude/(或 .agents/ / .dsh/)**——只精确覆盖生成物
|
|
28
|
+
* (rules/ + skills/ 等),保留其他一切内容(commands/、自定义 hook、自定义 skill)。
|
|
29
|
+
* - settings.local.json / .codex/hooks.json 采用**注入**方式:读现有配置 → 保留已有
|
|
30
|
+
* 字段 → 过滤已管理的 comet hook → 合并新 hook → 写回。不覆盖用户已有配置。
|
|
31
|
+
* - 显式 `--purge` 参数才允许删除生成物(打印删除清单 + 警告 + 需 --yes 二次确认)。
|
|
32
|
+
* - 覆盖前打印将覆盖的生成物清单。
|
|
33
|
+
*
|
|
34
|
+
* 幂等:默认模式重复运行——rules/skills 覆盖一致;settings/hooks 注入幂等
|
|
35
|
+
* (已管理的 comet-hook-guard 命令被过滤后重新合并,不产生重复条目);
|
|
36
|
+
* AGENTS.md 托管区幂等替换(移除旧托管区后重新生成);
|
|
37
|
+
* 运行时位置迁移幂等(旧件已搬走 → 跳过);.gitignore 纳管幂等(已有条目 → 保持原样)。
|
|
38
|
+
*
|
|
39
|
+
* 运行时位置迁移(旧命名空间 → 新命名空间):目标项目 `.comet/` 下的 flow-comet 运行时
|
|
40
|
+
* 文件按白名单枚举(RUNTIME_FILE_MIGRATIONS)搬移到 `.flow-comet/`——搬移语义 =
|
|
41
|
+
* 迁移前强制备份 → 写新位置 → 回读校验(逐字节)→ 删旧件;备份在迁移成功后保留
|
|
42
|
+
* (无 git 载体项目的唯一回退依据)。边界情形(新旧并存 / 符号链接 / JSON 损坏)
|
|
43
|
+
* 一律报错中止并保留全部原件,绝不静默覆盖或丢弃。`.comet/` 是三方共占目录——
|
|
44
|
+
* 白名单之外的内容(Comet 资产、用户自有文件)一律原位不动。
|
|
45
|
+
* 执行顺序:迁移 → 部署 → .gitignore 纳管;任一步失败即中止(保持可重跑的一致态)。
|
|
46
|
+
* .gitignore 纳管取保守策略:既有条目一律保留,仅缺失时追加 `.flow-comet/` 条目。
|
|
47
|
+
*
|
|
48
|
+
* 用法:node scripts/prepare-env.mjs [--target <dir>] [--platform <claude-code|codex|dsh|claude-code,dsh|all>] [--purge --yes]
|
|
49
|
+
* --target 缺省 = 当前工作目录(cwd)。脚本自身定位:__dirname 上一级 = 仓库根
|
|
50
|
+
* (scripts/ 与 .flow-comet/ 同级),据此解析权威源。
|
|
51
|
+
* --platform 显式指定平台:单平台 / 逗号分隔多平台(claude-code,codex,dsh)/
|
|
52
|
+
* all(全部平台,安装顺序 = PLATFORMS 表顺序);未知平台报错(旧 both 语义已移除——
|
|
53
|
+
* 多平台用逗号列表或 all);缺省走选择链:
|
|
54
|
+
* TTY 交互多选(@clack/prompts 方向键多选——依赖安装后为主路径,try-import 失败
|
|
55
|
+
* (未装/离线)或 stdin 非真实 TTY(无 setRawMode,如管道喂入模拟)自动回退 readline
|
|
56
|
+
* 数字/逗号多选;基于目标项目痕迹 .claude/ .codex/ .dsh/ 预勾选,回车默认 = 探测推荐;
|
|
57
|
+
* FLOW_COMET_FORCE_READLINE=1 仅测试面强制走回退)
|
|
58
|
+
* > 探测目标项目(仅 .codex/ → codex;仅 .dsh/ → dsh;含 .claude/ → claude-code;
|
|
59
|
+
* 多痕迹默认 claude-code 并输出提示)> 默认 claude-code。
|
|
60
|
+
* --purge 破坏性:删除目标平台生成物后重新生成(打印清单 + 警告 + --yes 确认)。
|
|
61
|
+
*
|
|
62
|
+
* 输出:打印生成摘要(平台 / 目录数 / 文件数 / skills 数 / 注入状态),exit 0;失败 exit 1。
|
|
63
|
+
*/
|
|
64
|
+
import fs from 'node:fs';
|
|
65
|
+
import path from 'node:path';
|
|
66
|
+
import os from 'node:os';
|
|
67
|
+
import { execFileSync } from 'node:child_process';
|
|
68
|
+
import { createInterface } from 'node:readline/promises';
|
|
69
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
70
|
+
|
|
71
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
72
|
+
const REPO_ROOT = path.resolve(__dirname, '..');
|
|
73
|
+
// 权威源根(含 skills/ 与 rules/——目录结构与安装副本 .claude/ 对称:<根>/skills/flow-comet/)
|
|
74
|
+
const BUNDLE_DRAFTS = path.join(REPO_ROOT, '.flow-comet');
|
|
75
|
+
|
|
76
|
+
// ---------- 运行时文件位置迁移(旧命名空间 → 新命名空间) ----------
|
|
77
|
+
//
|
|
78
|
+
// 【过渡性模块——有明确退役条件,不是永久耦合】
|
|
79
|
+
// 本模块存在的唯一理由:让已安装项目把状态文件从旧命名空间搬到新命名空间,避免破坏性变更
|
|
80
|
+
// 导致用户进行中的流程状态丢失。它**只在安装/升级时执行一次**,从不进入任何日常运行路径
|
|
81
|
+
// (写入守卫、工作流脚本、协议解析均已完全不含对旧命名空间的感知)。
|
|
82
|
+
//
|
|
83
|
+
// 因此这里的 `.comet` 字面量是**迁移源**,不是 flow-comet 的命名归属——它标记的是"要从哪里
|
|
84
|
+
// 搬走",而非"我们是谁"。这是任何破坏性变更都需要的过渡桥(同"改名迁移脚本必须知道旧表名")。
|
|
85
|
+
//
|
|
86
|
+
// **退役条件**(满足任一即可整体删除本模块及其常量):
|
|
87
|
+
// ① 下一个破坏性版本发布时——迁移公告期结束(建议至少覆盖一个完整的 minor 周期,让下游
|
|
88
|
+
// 项目有时间升级);
|
|
89
|
+
// ② 或:连续多次安装运行中迁移报告**全部为「已跳过」**(说明已知项目均已迁移完毕)。
|
|
90
|
+
// 判据取证:每次安装都会打印迁移明细(已迁移 / 已跳过);长期全为「已跳过」即为退役信号。
|
|
91
|
+
//
|
|
92
|
+
// 位置迁移白名单(单一来源):目标项目的 `.comet/` 是三方共占目录——flow-comet 运行时文件
|
|
93
|
+
// + Comet 资产(`runs/`、`config.yaml` 等)+ 用户自有文件(`*.bak-*` 等)。只有下表列出的
|
|
94
|
+
// 文件属 flow-comet 运行时域,其余一律原位不动(不搬迁、不删除、不改写)——整目录搬迁或
|
|
95
|
+
// 前缀正则匹配都会越界处置非 flow-comet 资产。新增运行时文件时必须同步本表。
|
|
96
|
+
const LEGACY_RUNTIME_DIR = '.comet';
|
|
97
|
+
const RUNTIME_DIR = '.flow-comet';
|
|
98
|
+
const RUNTIME_FILE_MIGRATIONS = [
|
|
99
|
+
{ name: 'flow-comet-state.json', validateJson: true },
|
|
100
|
+
];
|
|
101
|
+
|
|
102
|
+
// comet-hook-guard 的 command 特征(用于识别"已管理的 hook 命令",注入时替换避免重复)
|
|
103
|
+
const MANAGED_HOOK_COMMAND_MARKER = 'comet-hook-guard.mjs';
|
|
104
|
+
|
|
105
|
+
// ---------- hook 命令项目根锚定(单一事实来源) ----------
|
|
106
|
+
// Claude Code 运行 hook 时注入 CLAUDE_PROJECT_DIR 环境变量(值 = 项目根绝对路径,官方 hooks
|
|
107
|
+
// 文档契约)。变量名是唯一事实来源;命令形态**全平台统一**为 POSIX/bash 词形
|
|
108
|
+
// (`${VAR}` + 正斜杠——CC 在所有平台含 Windows 都以 /bin/bash -c 执行 hook 命令),
|
|
109
|
+
// 由 projectRootGuardScriptRef 派生,不按宿主 OS 分叉。
|
|
110
|
+
const PROJECT_ROOT_ENV_VAR = 'CLAUDE_PROJECT_DIR';
|
|
111
|
+
|
|
112
|
+
// hook 命令加整体外层引号(2026-09-10 演进——原「不加引号」取舍的前提是识别函数按空白分词
|
|
113
|
+
// 判等、外层引号会并入 token 使 basename 失配;识别函数已演进为容忍引号,故放开):未加
|
|
114
|
+
// 引号时,含空格的项目根路径在变量展开后会被词法分词切断(静默失效,与反斜杠转义同类)。
|
|
115
|
+
function projectRootGuardScriptRef(rootDirName) {
|
|
116
|
+
// CC 全平台以 /bin/bash -c 执行 hook 命令(含 Windows——Git Bash 语义),并注入
|
|
117
|
+
// CLAUDE_PROJECT_DIR 环境变量;故统一 POSIX 形态:${VAR} 花括号词形(CC 自身亦做该词形
|
|
118
|
+
// 插值——字符串替换,双保险)+ 正斜杠路径(反斜杠在 bash 词法里会被当转义吞掉,如 \. \f)。
|
|
119
|
+
// 整体加引号:含空格的项目根路径不因词法分词断裂;识别层容忍引号(isManagedHookCommand
|
|
120
|
+
// 去引号后取 basename 判等),幂等升级不受影响。
|
|
121
|
+
const segments = [rootDirName, 'skills', 'flow-comet', 'scripts', MANAGED_HOOK_COMMAND_MARKER];
|
|
122
|
+
return `"\${${PROJECT_ROOT_ENV_VAR}}/${segments.join('/')}"`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// AGENTS.md 托管区标记(Codex/dsh 平台 rules 注入共用——幂等替换边界,任一平台卸载可清)
|
|
126
|
+
const MANAGED_AGENTS_START = '<!-- Managed by flow-comet prepare-env -->';
|
|
127
|
+
const MANAGED_AGENTS_END = '<!-- /Managed by flow-comet prepare-env -->';
|
|
128
|
+
|
|
129
|
+
// $DSH_HOME/cordis.patch.yml 托管块标记(dsh 平台 home patch 注入——幂等替换边界;
|
|
130
|
+
// 参照本机既有惯例:# --- dsh-skin managed --- ... # --- end dsh-skin managed ---)
|
|
131
|
+
const MANAGED_CORDIS_START = '# --- flow-comet managed ---';
|
|
132
|
+
const MANAGED_CORDIS_END = '# --- end flow-comet managed ---';
|
|
133
|
+
|
|
134
|
+
// ---------- 平台描述符表(单一来源——新增平台 = 描述符条目 + 安装/清理函数,main 零改动) ----------
|
|
135
|
+
// D6:flow-kit 永不纳入清理域——下述各平台 purge 清单均不含 <target>/flow-kit/(含 --purge --yes);
|
|
136
|
+
// 它不是本安装器的生成物域(ADR-008),由安装器首次创建的 flow-kit 残留属预期。
|
|
137
|
+
|
|
138
|
+
const PLATFORMS = {
|
|
139
|
+
'claude-code': {
|
|
140
|
+
label: 'Claude Code',
|
|
141
|
+
// 技能安装根(相对 target;与权威源 .claude 形态一致——零路径替换)
|
|
142
|
+
skillRoot: (target) => path.join(target, '.claude', 'skills'),
|
|
143
|
+
// 平台路径替换表(from 正则 → to):claude-code 为空 = 权威源即目标形态
|
|
144
|
+
pathReplacements: [],
|
|
145
|
+
// hook 注入:settings.local.json 读-合并-写(保留既有字段;matcher Write|Edit)
|
|
146
|
+
installHooks(target, stats) {
|
|
147
|
+
const claudeDir = path.join(target, '.claude');
|
|
148
|
+
fs.mkdirSync(claudeDir, { recursive: true });
|
|
149
|
+
stats.dirs++;
|
|
150
|
+
const settings = injectSettingsHook(claudeDir);
|
|
151
|
+
const injected = !!(settings.hooks && settings.hooks.PreToolUse);
|
|
152
|
+
stats.files++;
|
|
153
|
+
console.log(`[prepare-env] settings.local.json ${injected ? '注入完成(保留既有字段)' : '已生成'}`);
|
|
154
|
+
},
|
|
155
|
+
// rules 注入:.claude/rules/ 整树复制(自动加载 markdown 指令)
|
|
156
|
+
installRules(target, stats) {
|
|
157
|
+
const rulesSrc = path.join(BUNDLE_DRAFTS, 'rules');
|
|
158
|
+
if (fs.existsSync(rulesSrc)) {
|
|
159
|
+
copyTree(rulesSrc, path.join(target, '.claude', 'rules'), stats);
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
// purge 清理:整删 .claude/(CC 的 .claude 即 flow-comet 生成物域);返回删除清单供打印
|
|
163
|
+
purge(target) {
|
|
164
|
+
const claudeDir = path.join(target, '.claude');
|
|
165
|
+
const removed = [];
|
|
166
|
+
if (fs.existsSync(claudeDir)) {
|
|
167
|
+
for (const entry of fs.readdirSync(claudeDir)) {
|
|
168
|
+
removed.push(path.join(claudeDir, entry));
|
|
169
|
+
}
|
|
170
|
+
fs.rmSync(claudeDir, { recursive: true, force: true });
|
|
171
|
+
}
|
|
172
|
+
return removed;
|
|
173
|
+
},
|
|
174
|
+
// 覆盖清单描述(默认非破坏:只覆盖生成物)
|
|
175
|
+
overwriteDescription(target) {
|
|
176
|
+
return [
|
|
177
|
+
path.join(target, '.claude', 'rules'),
|
|
178
|
+
path.join(target, '.claude', 'settings.local.json') + '(注入方式:保留既有字段,仅更新 hooks.PreToolUse)',
|
|
179
|
+
];
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
'codex': {
|
|
183
|
+
label: 'Codex',
|
|
184
|
+
// 技能安装根:Codex 自动发现位置($CWD/.agents/skills/ 最高优先)
|
|
185
|
+
skillRoot: (target) => path.join(target, '.agents', 'skills'),
|
|
186
|
+
// SKILL/GUIDANCE 命令路径平台化替换(权威源保持 .claude 形态,安装时替换)
|
|
187
|
+
pathReplacements: [
|
|
188
|
+
{ from: /\.claude\/skills\/flow-comet\/scripts\//g, to: '.agents/skills/flow-comet/scripts/' },
|
|
189
|
+
],
|
|
190
|
+
// hook 注入:.codex/hooks.json(顶层 hooks 包裹层 + matcher *)+ config.toml features 启用
|
|
191
|
+
installHooks(target, stats) {
|
|
192
|
+
const codexDir = path.join(target, '.codex');
|
|
193
|
+
injectCodexHook(codexDir, target);
|
|
194
|
+
injectCodexConfig(codexDir);
|
|
195
|
+
stats.files += 2;
|
|
196
|
+
console.log('[prepare-env] .codex/hooks.json + config.toml 注入完成(hooks 启用,保留既有字段)');
|
|
197
|
+
},
|
|
198
|
+
// rules 注入:AGENTS.md 托管区内联(Codex 指令唯一自动加载路径;.codex/rules/ 是命令批准规则目录,不混用;
|
|
199
|
+
// 注入函数与托管区标记 codex/dsh 共用——任一平台卸载可清,另一平台重装恢复)
|
|
200
|
+
installRules(target, stats) {
|
|
201
|
+
if (injectManagedRules(target, stats)) {
|
|
202
|
+
console.log('[prepare-env] AGENTS.md 托管区注入完成(保留托管区外用户内容)');
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
// purge 清理:只清 flow-comet 技能(.agents/ 为多工具共享位置——非 flow-comet 条目保留)
|
|
206
|
+
// + .codex/hooks.json 托管条目(保留用户条目)+ AGENTS.md 托管区(保留文件本身)
|
|
207
|
+
purge(target) {
|
|
208
|
+
const removed = [];
|
|
209
|
+
const agentsSkillsDir = path.join(target, '.agents', 'skills');
|
|
210
|
+
if (fs.existsSync(agentsSkillsDir)) {
|
|
211
|
+
for (const entry of fs.readdirSync(agentsSkillsDir)) {
|
|
212
|
+
if (!/^flow-comet/.test(entry)) continue;
|
|
213
|
+
const managed = path.join(agentsSkillsDir, entry);
|
|
214
|
+
removed.push(managed);
|
|
215
|
+
fs.rmSync(managed, { recursive: true, force: true });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const hooksPath = path.join(target, '.codex', 'hooks.json');
|
|
219
|
+
if (fs.existsSync(hooksPath)) {
|
|
220
|
+
removed.push(hooksPath + '(移除托管 hook 条目,保留用户条目)');
|
|
221
|
+
// 移除托管条目(重建流程随后重新注入新托管条目——purge = 移除旧生成物后重建)
|
|
222
|
+
removeManagedCodexHooks(path.join(target, '.codex'));
|
|
223
|
+
}
|
|
224
|
+
const agentsPath = path.join(target, 'AGENTS.md');
|
|
225
|
+
if (fs.existsSync(agentsPath)) {
|
|
226
|
+
const content = fs.readFileSync(agentsPath, 'utf8');
|
|
227
|
+
if (content.includes(MANAGED_AGENTS_START)) {
|
|
228
|
+
removed.push(agentsPath + '(托管区)');
|
|
229
|
+
const stripped = stripManagedBlock(content, MANAGED_AGENTS_START, MANAGED_AGENTS_END);
|
|
230
|
+
fs.writeFileSync(agentsPath, stripped.trim() + '\n', 'utf8');
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return removed;
|
|
234
|
+
},
|
|
235
|
+
// 覆盖清单描述(默认非破坏:只覆盖生成物)
|
|
236
|
+
overwriteDescription(target) {
|
|
237
|
+
return [
|
|
238
|
+
path.join(target, 'AGENTS.md') + '(托管区注入:保留托管区外用户内容)',
|
|
239
|
+
path.join(target, '.codex', 'hooks.json') + '(注入方式:保留既有字段,仅更新托管 hook 条目)',
|
|
240
|
+
path.join(target, '.codex', 'config.toml') + '(注入方式:补 [features] hooks 启用键)',
|
|
241
|
+
];
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
'dsh': {
|
|
245
|
+
label: 'DeepSeek Harness',
|
|
246
|
+
// 技能安装根:dsh 项目级 skill 发现(rank 100 自动发现,免重启;未安装该目录的项目不可见该 skill)
|
|
247
|
+
skillRoot: (target) => path.join(target, '.dsh', 'skills'),
|
|
248
|
+
// SKILL/GUIDANCE 命令路径平台化替换(权威源保持 .claude 形态,安装时替换)
|
|
249
|
+
pathReplacements: [
|
|
250
|
+
{ from: /\.claude\/skills\/flow-comet\/scripts\//g, to: '.dsh/skills/flow-comet/scripts/' },
|
|
251
|
+
],
|
|
252
|
+
// hook 注入:全局挂载桥接 loader($DSH_HOME/plugins/ 复制 + cordis.patch.yml 托管块——
|
|
253
|
+
// 所有 profile 生效;$DSH_HOME 解析 = 显式 DSH_HOME > ~/.dsh)
|
|
254
|
+
installHooks(target, stats) {
|
|
255
|
+
const dshHome = resolveDshHome();
|
|
256
|
+
const pluginsDir = path.join(dshHome, 'plugins');
|
|
257
|
+
const loaderSrc = path.join(REPO_ROOT, 'scripts', 'dsh-bridge.mjs');
|
|
258
|
+
const loaderDst = path.join(pluginsDir, 'dsh-flow-comet-bridge.mjs');
|
|
259
|
+
if (fs.existsSync(loaderSrc)) {
|
|
260
|
+
fs.mkdirSync(pluginsDir, { recursive: true });
|
|
261
|
+
// 覆盖前版本比对明示(版本戳标记契约):输出首次安装/升级/降级/版本一致之一;
|
|
262
|
+
// 任一侧提取失败只告警不中断——覆盖照常执行(幂等语义不变)
|
|
263
|
+
reportLoaderVersionTransition(loaderSrc, loaderDst);
|
|
264
|
+
fs.copyFileSync(loaderSrc, loaderDst);
|
|
265
|
+
stats.files++;
|
|
266
|
+
console.log('[prepare-env] 桥接 loader 已复制到 $DSH_HOME/plugins/dsh-flow-comet-bridge.mjs');
|
|
267
|
+
injectDshCordisPatch(dshHome, loaderDst);
|
|
268
|
+
stats.files++;
|
|
269
|
+
console.log('[prepare-env] $DSH_HOME/cordis.patch.yml 托管块注入完成(读-合并-写,保留既有块)');
|
|
270
|
+
} else {
|
|
271
|
+
// 容错:scripts/dsh-bridge.mjs 由并行任务新建——源缺失时 WARN 跳过 loader 复制
|
|
272
|
+
// **并跳过 cordis.patch.yml 托管块注入**(避免注入指向不存在文件的 file:// 引用,
|
|
273
|
+
// 导致 dsh 每次启动尝试加载不存在的插件);仍完成其余安装
|
|
274
|
+
// (AGENTS.md 托管区 / skills 复制照常)
|
|
275
|
+
console.warn(
|
|
276
|
+
'[prepare-env] 警告: scripts/dsh-bridge.mjs 不存在——跳过 loader 复制与 cordis.patch.yml 托管块注入' +
|
|
277
|
+
'(dsh 桥接拦截暂不生效,待源文件就位后重跑 prepare-env)'
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
},
|
|
281
|
+
// rules 注入:AGENTS.md 托管区(与 codex 共用注入函数与托管区标记——任一平台卸载可清)
|
|
282
|
+
installRules(target, stats) {
|
|
283
|
+
if (injectManagedRules(target, stats)) {
|
|
284
|
+
console.log('[prepare-env] AGENTS.md 托管区注入完成(保留托管区外用户内容)');
|
|
285
|
+
}
|
|
286
|
+
},
|
|
287
|
+
// purge 清理:项目级 .dsh/skills 下全部 flow-comet* 技能(.dsh/skills 为 dsh 技能位置——
|
|
288
|
+
// 非 flow-comet 条目保留,与 codex purge 同语义)+ 空 .dsh 目录 + AGENTS.md 托管区
|
|
289
|
+
// (保留文件与用户内容)+ $DSH_HOME/cordis.patch.yml 托管块 + plugins/ loader
|
|
290
|
+
// (写 home 的敏感面——purge 需 --yes 显式确认)
|
|
291
|
+
purge(target) {
|
|
292
|
+
const removed = [];
|
|
293
|
+
const dshSkillsDir = path.join(target, '.dsh', 'skills');
|
|
294
|
+
if (fs.existsSync(dshSkillsDir)) {
|
|
295
|
+
for (const entry of fs.readdirSync(dshSkillsDir)) {
|
|
296
|
+
if (!/^flow-comet/.test(entry)) continue;
|
|
297
|
+
const managed = path.join(dshSkillsDir, entry);
|
|
298
|
+
removed.push(managed);
|
|
299
|
+
fs.rmSync(managed, { recursive: true, force: true });
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
// 空目录清理:仅删空目录(skills 下无剩余条目 / .dsh 下无其它内容时)——有内容则保留
|
|
303
|
+
if (fs.existsSync(dshSkillsDir) && fs.readdirSync(dshSkillsDir).length === 0) {
|
|
304
|
+
fs.rmdirSync(dshSkillsDir);
|
|
305
|
+
removed.push(dshSkillsDir);
|
|
306
|
+
}
|
|
307
|
+
const dshDir = path.join(target, '.dsh');
|
|
308
|
+
if (fs.existsSync(dshDir) && fs.readdirSync(dshDir).length === 0) {
|
|
309
|
+
fs.rmdirSync(dshDir);
|
|
310
|
+
removed.push(dshDir);
|
|
311
|
+
}
|
|
312
|
+
const agentsPath = path.join(target, 'AGENTS.md');
|
|
313
|
+
if (fs.existsSync(agentsPath)) {
|
|
314
|
+
const content = fs.readFileSync(agentsPath, 'utf8');
|
|
315
|
+
if (content.includes(MANAGED_AGENTS_START)) {
|
|
316
|
+
removed.push(agentsPath + '(托管区)');
|
|
317
|
+
const stripped = stripManagedBlock(content, MANAGED_AGENTS_START, MANAGED_AGENTS_END);
|
|
318
|
+
fs.writeFileSync(agentsPath, stripped.trim() + '\n', 'utf8');
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
// home patch 托管块移除 + loader 删除($DSH_HOME——写 home 的敏感面)
|
|
322
|
+
const dshHome = resolveDshHome();
|
|
323
|
+
for (const entry of removeDshCordisPatch(dshHome)) {
|
|
324
|
+
removed.push(entry);
|
|
325
|
+
}
|
|
326
|
+
const loaderPath = path.join(dshHome, 'plugins', 'dsh-flow-comet-bridge.mjs');
|
|
327
|
+
if (fs.existsSync(loaderPath)) {
|
|
328
|
+
removed.push(loaderPath + '($DSH_HOME/plugins/ loader)');
|
|
329
|
+
fs.rmSync(loaderPath, { force: true });
|
|
330
|
+
// 全局挂载语义警示:loader 位于 $DSH_HOME(所有 profile / 所有项目共享)——
|
|
331
|
+
// 任一项目 purge 都会连带移除,其它已安装 flow-comet 的项目将静默失去拦截
|
|
332
|
+
console.warn(
|
|
333
|
+
'[prepare-env] 全局桥接 loader 被移除,其它已安装 flow-comet 的项目将停止拦截' +
|
|
334
|
+
'($DSH_HOME 全局挂载——如需恢复请重跑 prepare-env --platform dsh)'
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
return removed;
|
|
338
|
+
},
|
|
339
|
+
// 覆盖清单描述(默认非破坏:只覆盖生成物;声明写 home 的敏感面)
|
|
340
|
+
overwriteDescription(target) {
|
|
341
|
+
return [
|
|
342
|
+
path.join(target, 'AGENTS.md') + '(托管区注入:保留托管区外用户内容)',
|
|
343
|
+
path.join(resolveDshHome(), 'cordis.patch.yml') + '(托管块注入:保留 dsh-skin 等既有块)',
|
|
344
|
+
path.join(resolveDshHome(), 'plugins', 'dsh-flow-comet-bridge.mjs') + '($DSH_HOME 全局挂载——安装器写 home 目录的敏感面)',
|
|
345
|
+
];
|
|
346
|
+
},
|
|
347
|
+
},
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
// ---------- 平台选择 ----------
|
|
351
|
+
|
|
352
|
+
function parseArgs(argv) {
|
|
353
|
+
const args = argv.slice(2);
|
|
354
|
+
let target = null;
|
|
355
|
+
let purge = false;
|
|
356
|
+
let yes = false;
|
|
357
|
+
let platform = null;
|
|
358
|
+
// 命令形态容忍(npm bin 形态 `fcomet init`):包管理器垫片把 `init` 当位置词元传给脚本。
|
|
359
|
+
// 只吃掉**一个**精确等于 `init` 且位于位置参数首位的词元(其前只允许选项词元);
|
|
360
|
+
// 其它非 `--` 开头词元仍按既有语义报「未知参数」——变形词元(如 `int`/`inti`)
|
|
361
|
+
// 绝不被静默吞成默认安装。
|
|
362
|
+
let initTokenConsumed = false;
|
|
363
|
+
for (let i = 0; i < args.length; i++) {
|
|
364
|
+
const a = args[i];
|
|
365
|
+
if (a === 'init' && !initTokenConsumed) {
|
|
366
|
+
initTokenConsumed = true;
|
|
367
|
+
} else if (a === '--target') {
|
|
368
|
+
if (i + 1 >= args.length) {
|
|
369
|
+
throw new Error('--target 缺少目录参数');
|
|
370
|
+
}
|
|
371
|
+
target = args[++i];
|
|
372
|
+
} else if (a.startsWith('--target=')) {
|
|
373
|
+
target = a.slice('--target='.length);
|
|
374
|
+
} else if (a === '--platform') {
|
|
375
|
+
if (i + 1 >= args.length) {
|
|
376
|
+
throw new Error('--platform 缺少平台参数');
|
|
377
|
+
}
|
|
378
|
+
platform = args[++i];
|
|
379
|
+
} else if (a.startsWith('--platform=')) {
|
|
380
|
+
platform = a.slice('--platform='.length);
|
|
381
|
+
} else if (a === '--purge') {
|
|
382
|
+
purge = true;
|
|
383
|
+
} else if (a === '--yes') {
|
|
384
|
+
yes = true;
|
|
385
|
+
} else if (a === '--help' || a === '-h') {
|
|
386
|
+
printUsage();
|
|
387
|
+
process.exit(0);
|
|
388
|
+
} else {
|
|
389
|
+
throw new Error(`未知参数: ${a}`);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return { target: target ? path.resolve(target) : process.cwd(), purge, yes, platform };
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function printUsage() {
|
|
396
|
+
console.log('用法: fcomet init [--target <dir>] [--platform <claude-code|codex|dsh|claude-code,dsh|all>] [--purge --yes]');
|
|
397
|
+
console.log(' (`fcomet` 与 `flow-comet` 是同一安装器的两个命令名;`init` 词元可省略——裸 `fcomet` 等价)');
|
|
398
|
+
console.log(' 仓库内直调: node scripts/prepare-env.mjs [--target <dir>] [--platform <...>] [--purge --yes]');
|
|
399
|
+
console.log(' 从权威源 .flow-comet/ 安装/更新 <dir> 环境(含运行时位置迁移与 .gitignore 纳管)');
|
|
400
|
+
console.log(' --target 缺省 = 当前工作目录(cwd)');
|
|
401
|
+
console.log(' --platform 指定平台(claude-code / codex / dsh;逗号分隔多选或 all 全部平台)');
|
|
402
|
+
console.log(' 缺省 = TTY 交互多选(@clack/prompts,未安装回退 readline)> 探测目标项目 > 默认 claude-code');
|
|
403
|
+
console.log(' --purge 破坏性:先删除目标平台生成物再重新生成(默认不删除;需 --yes 确认)');
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// 探测目标项目既有平台痕迹(三痕迹,一次计算复用于探测 / TTY 预勾选 / 多痕迹判定):
|
|
407
|
+
// 仅 .codex/ → codex;仅 .dsh/ → dsh;含 .claude/(单独或与其它痕迹并存)→ claude-code;皆无 → probe=null。
|
|
408
|
+
// 多痕迹(.claude/ .codex/ .dsh/ 中 ≥2 个并存)不武断二选一——默认主平台 claude-code,resolvePlatform 输出提示。
|
|
409
|
+
function detectTraces(target) {
|
|
410
|
+
const hasCodex = fs.existsSync(path.join(target, '.codex'));
|
|
411
|
+
const hasClaude = fs.existsSync(path.join(target, '.claude'));
|
|
412
|
+
const hasDsh = fs.existsSync(path.join(target, '.dsh'));
|
|
413
|
+
const probe = hasClaude ? 'claude-code' : hasCodex ? 'codex' : hasDsh ? 'dsh' : null;
|
|
414
|
+
return {
|
|
415
|
+
probe,
|
|
416
|
+
// TTY 多选预勾选依据(三痕迹各自独立判定)
|
|
417
|
+
traces: { 'claude-code': hasClaude, codex: hasCodex, dsh: hasDsh },
|
|
418
|
+
multiTrace: [hasClaude, hasCodex, hasDsh].filter(Boolean).length > 1,
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// TTY 交互多选(缺省路径——用户裁决:交互式选择为主;无 TTY 自动走探测/默认)。
|
|
423
|
+
// 首选 @clack/prompts 方向键多选(Claude Code / Codex / dsh,基于 detectTraces 痕迹预勾选:
|
|
424
|
+
// .claude/ / .codex/ / .dsh/);try-import 失败(依赖未安装/离线)或 stdin 无法 raw mode
|
|
425
|
+
// (isTTY 缺失/伪真且 setRawMode 不存在——如管道喂入的自动化形态)自动回退 readline
|
|
426
|
+
// 数字/逗号多选(回车默认 = 探测推荐)。仅测试面环境开关 FLOW_COMET_FORCE_READLINE=1
|
|
427
|
+
// 强制走 readline 回退分支(即便 @clack/prompts 可加载)——供自动化套件断言回退可测,
|
|
428
|
+
// 不得用于生产规避。
|
|
429
|
+
async function promptPlatformSelection(probe, traces, multiTrace) {
|
|
430
|
+
if (process.env.FLOW_COMET_FORCE_READLINE === '1') {
|
|
431
|
+
return promptPlatformSelectionReadline(probe, traces, multiTrace);
|
|
432
|
+
}
|
|
433
|
+
const options = Object.entries(PLATFORMS).map(([id, p]) => ({ value: id, label: `${p.label} (${id})` }));
|
|
434
|
+
const initialValues = options.filter((o) => traces[o.value]).map((o) => o.value);
|
|
435
|
+
let clack = null;
|
|
436
|
+
try {
|
|
437
|
+
clack = await import('@clack/prompts');
|
|
438
|
+
} catch { /* 依赖未安装/离线——自动回退 readline 多选 */ }
|
|
439
|
+
if (clack && typeof clack.multiselect === 'function') {
|
|
440
|
+
// @clack 依赖 stdin raw mode:isTTY 为真但 stdin 实为管道(无 setRawMode)时调用
|
|
441
|
+
// multiselect 会崩溃(o.setRawMode is not a function)——与强制回退同路径直接走 readline。
|
|
442
|
+
if (!process.stdin.isTTY || typeof process.stdin.setRawMode !== 'function') {
|
|
443
|
+
return promptPlatformSelectionReadline(probe, traces, multiTrace);
|
|
444
|
+
}
|
|
445
|
+
const selected = await clack.multiselect({
|
|
446
|
+
message: '[prepare-env] 选择要安装的平台(方向键移动、空格勾选、回车确认)',
|
|
447
|
+
options,
|
|
448
|
+
required: true,
|
|
449
|
+
initialValues: initialValues.length > 0 ? initialValues : undefined,
|
|
450
|
+
});
|
|
451
|
+
if (clack.isCancel(selected)) {
|
|
452
|
+
throw new Error('平台选择已取消');
|
|
453
|
+
}
|
|
454
|
+
return selected;
|
|
455
|
+
}
|
|
456
|
+
return promptPlatformSelectionReadline(probe, traces, multiTrace);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// readline 多选回退(无第三方依赖路径——@clack/prompts 未安装/离线/强制开关下):
|
|
460
|
+
// 数字/平台名逗号分隔多选;回车 = 探测推荐(无探测 → claude-code)。
|
|
461
|
+
async function promptPlatformSelectionReadline(probe, traces, multiTrace) {
|
|
462
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
463
|
+
try {
|
|
464
|
+
const ids = Object.keys(PLATFORMS);
|
|
465
|
+
const defaultChoice = probe ?? 'claude-code';
|
|
466
|
+
const traceNote = (id, traceLabel) => (traces[id] ? `(检测到 ${traceLabel} 痕迹)` : '');
|
|
467
|
+
const probeNote = multiTrace
|
|
468
|
+
? '(检测到目标项目多平台痕迹并存——默认主平台 Claude Code,不武断推荐)'
|
|
469
|
+
: probe
|
|
470
|
+
? `(检测到目标项目已有 ${probe === 'claude-code' ? '.claude/' : probe === 'codex' ? '.codex/' : '.dsh/'} 痕迹)`
|
|
471
|
+
: '';
|
|
472
|
+
const answer = await rl.question(
|
|
473
|
+
`[prepare-env] 选择要安装的平台(可多选)${probeNote}:\n` +
|
|
474
|
+
` 1) Claude Code (claude-code)${traceNote('claude-code', '.claude/')}\n` +
|
|
475
|
+
` 2) Codex (codex)${traceNote('codex', '.codex/')}\n` +
|
|
476
|
+
` 3) DeepSeek Harness (dsh)${traceNote('dsh', '.dsh/')}\n` +
|
|
477
|
+
` 输入序号或平台名(逗号分隔多选)或回车(默认 ${defaultChoice}): `
|
|
478
|
+
);
|
|
479
|
+
const choice = String(answer ?? '').trim().toLowerCase();
|
|
480
|
+
if (choice === '') return [defaultChoice];
|
|
481
|
+
const selected = [];
|
|
482
|
+
for (const part of choice.split(',').map((s) => s.trim()).filter(Boolean)) {
|
|
483
|
+
if (/^\d+$/.test(part)) {
|
|
484
|
+
const idx = parseInt(part, 10) - 1;
|
|
485
|
+
if (idx < 0 || idx >= ids.length) {
|
|
486
|
+
throw new Error(`无效序号: ${part}(可用: 1-${ids.length})`);
|
|
487
|
+
}
|
|
488
|
+
if (!selected.includes(ids[idx])) selected.push(ids[idx]);
|
|
489
|
+
} else {
|
|
490
|
+
// hasOwnProperty 防原型链属性(如 toString/__proto__)通过查找——仅接受自有描述符键
|
|
491
|
+
if (!Object.prototype.hasOwnProperty.call(PLATFORMS, part)) {
|
|
492
|
+
throw new Error(`未知平台: ${part}(可用: ${ids.join(', ')})`);
|
|
493
|
+
}
|
|
494
|
+
if (!selected.includes(part)) selected.push(part);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
return selected;
|
|
498
|
+
} finally {
|
|
499
|
+
rl.close();
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// 显式 --platform 参数解析:单平台 / 逗号分隔多平台 / all(全部平台,顺序 = PLATFORMS 表顺序)。
|
|
504
|
+
// 重复 id 去重(claude-code,claude-code 只装一次);任一未知报错(含逗号列表中任一未知);
|
|
505
|
+
// 旧 both 语义已移除——显式 both 报错提示改用逗号列表或 all。
|
|
506
|
+
function parsePlatformArg(value) {
|
|
507
|
+
const raw = String(value ?? '').trim();
|
|
508
|
+
if (raw === '') {
|
|
509
|
+
throw new Error('--platform 参数为空(可用: claude-code, codex, dsh 逗号多选或 all)');
|
|
510
|
+
}
|
|
511
|
+
if (raw === 'all') return Object.keys(PLATFORMS);
|
|
512
|
+
const ids = raw.split(',').map((s) => s.trim()).filter(Boolean);
|
|
513
|
+
if (ids.length === 0) {
|
|
514
|
+
throw new Error('--platform 参数为空(可用: claude-code, codex, dsh 逗号多选或 all)');
|
|
515
|
+
}
|
|
516
|
+
for (const id of ids) {
|
|
517
|
+
// hasOwnProperty 防原型链属性(如 toString/__proto__)通过查找——仅接受自有描述符键
|
|
518
|
+
if (!Object.prototype.hasOwnProperty.call(PLATFORMS, id)) {
|
|
519
|
+
if (id === 'both') {
|
|
520
|
+
throw new Error('--platform both 已移除——多平台请用逗号分隔(如 claude-code,codex,dsh)或 all');
|
|
521
|
+
}
|
|
522
|
+
throw new Error(`未知平台: ${id}(可用: ${Object.keys(PLATFORMS).join(', ')};逗号分隔多选或 all)`);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
return [...new Set(ids)];
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// 平台解析链:--platform 显式(逗号多选/all)> TTY 交互多选(@clack/prompts,回退 readline)> 探测 > 默认 claude-code。
|
|
529
|
+
// 返回平台描述符数组(安装顺序 = 数组顺序:显式 = 参数顺序;all = PLATFORMS 表顺序;交互 = 勾选顺序)。
|
|
530
|
+
async function resolvePlatform(target, platformArg) {
|
|
531
|
+
if (platformArg) {
|
|
532
|
+
const ids = parsePlatformArg(platformArg);
|
|
533
|
+
return ids.map((id) => PLATFORMS[id]);
|
|
534
|
+
}
|
|
535
|
+
const { probe, traces, multiTrace } = detectTraces(target);
|
|
536
|
+
if (process.stdin.isTTY) {
|
|
537
|
+
const selected = await promptPlatformSelection(probe, traces, multiTrace);
|
|
538
|
+
return selected.map((id) => PLATFORMS[id]);
|
|
539
|
+
}
|
|
540
|
+
if (multiTrace) {
|
|
541
|
+
const fallbackId = probe ?? 'claude-code';
|
|
542
|
+
console.log(`[prepare-env] 检测到目标项目同时有 .claude/、.codex/ 或 .dsh/ 中的多个痕迹——默认安装 ${PLATFORMS[fallbackId].label}。`);
|
|
543
|
+
console.log(' 如需其它平台或组合:交互终端运行,或显式 --platform dsh / claude-code,dsh / all。');
|
|
544
|
+
}
|
|
545
|
+
return [PLATFORMS[probe ?? 'claude-code']];
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// ---------- 文件工具 ----------
|
|
549
|
+
|
|
550
|
+
/** 递归收集 srcDir 下所有目录与文件的绝对路径 */
|
|
551
|
+
function collect(srcDir) {
|
|
552
|
+
const dirs = [];
|
|
553
|
+
const files = [];
|
|
554
|
+
(function walk(dir) {
|
|
555
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
556
|
+
const abs = path.join(dir, entry.name);
|
|
557
|
+
if (entry.isDirectory()) {
|
|
558
|
+
dirs.push(abs);
|
|
559
|
+
walk(abs);
|
|
560
|
+
} else if (entry.isFile()) {
|
|
561
|
+
files.push(abs);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
})(srcDir);
|
|
565
|
+
return { dirs, files };
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/** 整树复制 srcDir → dstRoot(保持相对结构),计数写入 stats */
|
|
569
|
+
function copyTree(srcDir, dstRoot, stats) {
|
|
570
|
+
fs.mkdirSync(dstRoot, { recursive: true });
|
|
571
|
+
stats.dirs++;
|
|
572
|
+
const { dirs, files } = collect(srcDir);
|
|
573
|
+
for (const d of dirs) {
|
|
574
|
+
fs.mkdirSync(path.join(dstRoot, path.relative(srcDir, d)), { recursive: true });
|
|
575
|
+
stats.dirs++;
|
|
576
|
+
}
|
|
577
|
+
for (const f of files) {
|
|
578
|
+
fs.copyFileSync(f, path.join(dstRoot, path.relative(srcDir, f)));
|
|
579
|
+
stats.files++;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* 平台路径替换:对复制后的技能包内全部 .md 文件(SKILL/GUIDANCE/reference)做命令路径替换。
|
|
585
|
+
* 权威源保持 .claude 形态;claude-code 平台替换表为空(零改动,可复现性 diff 不变)。
|
|
586
|
+
* 幂等:同平台重复安装替换结果一致。
|
|
587
|
+
*/
|
|
588
|
+
function applyPathReplacements(root, replacements) {
|
|
589
|
+
if (!replacements || replacements.length === 0) return 0;
|
|
590
|
+
let replaced = 0;
|
|
591
|
+
const { files } = collect(root);
|
|
592
|
+
for (const f of files) {
|
|
593
|
+
if (!f.endsWith('.md')) continue;
|
|
594
|
+
let content = fs.readFileSync(f, 'utf8');
|
|
595
|
+
const original = content;
|
|
596
|
+
for (const { from, to } of replacements) {
|
|
597
|
+
content = content.replace(from, to);
|
|
598
|
+
}
|
|
599
|
+
if (content !== original) {
|
|
600
|
+
fs.writeFileSync(f, content, 'utf8');
|
|
601
|
+
replaced++;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
return replaced;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
// ---------- 运行时文件位置迁移(旧命名空间 .comet/ → 新命名空间 .flow-comet/) ----------
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* lstat 探测:返回 stat 或 null(**不跟随符号链接**——isSymbolicLink() 判定依赖 lstat)。
|
|
611
|
+
* 只有「该路径确实不存在」一类错误返回 null:
|
|
612
|
+
* - ENOENT:路径缺失;
|
|
613
|
+
* - ENOTDIR:路径中某一段不是目录——该路径不可能存在为文件(跨平台等价于「不存在」:
|
|
614
|
+
* 同一形态在 Windows 报 ENOENT、在 POSIX 报 ENOTDIR)。
|
|
615
|
+
* 其余错误(EACCES / EPERM / EIO 等访问类故障)**原样抛出**——把访问故障当成「文件不存在」
|
|
616
|
+
* 会让迁移静默跳过:安装照常继续并报告「已跳过:旧位置不存在」,用户的旧状态却永远留在
|
|
617
|
+
* 旧位置(用户以为已迁移)——静默的数据丢失风险,必须中止并暴露。
|
|
618
|
+
*/
|
|
619
|
+
function lstatIfExists(target) {
|
|
620
|
+
try {
|
|
621
|
+
return fs.lstatSync(target);
|
|
622
|
+
} catch (err) {
|
|
623
|
+
if (err && typeof err === 'object' && (err.code === 'ENOENT' || err.code === 'ENOTDIR')) {
|
|
624
|
+
return null;
|
|
625
|
+
}
|
|
626
|
+
throw err;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/** 迁移备份文件名时间戳(ISO 串里的 `:` 与 `.` 在 Windows 文件名中非法——统一替换为 `-`)。 */
|
|
631
|
+
function migrationTimestamp() {
|
|
632
|
+
return new Date().toISOString().replace(/[:.]/g, '-');
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/** 逐字节读回并比较;读取失败一律视为不一致(调用方按校验失败处置)。 */
|
|
636
|
+
function fileEqualsBytes(filePath, bytes) {
|
|
637
|
+
try {
|
|
638
|
+
return fs.readFileSync(filePath).equals(bytes);
|
|
639
|
+
} catch {
|
|
640
|
+
return false;
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* 迁移前置校验:旧件不存在 → 返回 null(无需迁移);命中边界情形 → 抛错中止(不返回字节)。
|
|
646
|
+
* 三种边界一律不静默覆盖或丢弃:
|
|
647
|
+
* - 旧件是符号链接 → 不跟随(防穿越到项目外),报错并给人工处置指引
|
|
648
|
+
* - 新旧位置并存 → 不覆盖任何一方,报错并列出两处路径供人工判断
|
|
649
|
+
* - 内容损坏(validateJson 条目)→ 保留原件未改动,报错并给修复指引
|
|
650
|
+
*/
|
|
651
|
+
function readMigratableRuntimeFile(oldPath, newPath, entry) {
|
|
652
|
+
const oldStat = lstatIfExists(oldPath);
|
|
653
|
+
if (!oldStat) return null;
|
|
654
|
+
if (oldStat.isSymbolicLink()) {
|
|
655
|
+
let linkTarget = '(读取失败)';
|
|
656
|
+
try {
|
|
657
|
+
linkTarget = fs.readlinkSync(oldPath);
|
|
658
|
+
} catch {
|
|
659
|
+
/* 链接目标读取失败不改变处置(同样中止) */
|
|
660
|
+
}
|
|
661
|
+
throw new Error(
|
|
662
|
+
`${oldPath} 是符号链接(指向 ${linkTarget})——不跟随链接搬移(避免穿越到项目外)。` +
|
|
663
|
+
`请人工确认后处理:把真实文件复制到 ${newPath}(保留本链接),或删除该链接后重跑安装器。`
|
|
664
|
+
);
|
|
665
|
+
}
|
|
666
|
+
if (lstatIfExists(newPath)) {
|
|
667
|
+
throw new Error(
|
|
668
|
+
`新旧位置同时存在该运行时文件,不覆盖任何一方:\n - 旧位置: ${oldPath}\n - 新位置: ${newPath}\n` +
|
|
669
|
+
`请人工比对两处内容,保留正确的一份(删除或改名另一份)后重跑安装器。`
|
|
670
|
+
);
|
|
671
|
+
}
|
|
672
|
+
let bytes;
|
|
673
|
+
try {
|
|
674
|
+
bytes = fs.readFileSync(oldPath);
|
|
675
|
+
} catch (err) {
|
|
676
|
+
throw new Error(`${oldPath} 读取失败(${err.message})——未做任何改动,中止迁移。请检查文件权限后重跑安装器。`);
|
|
677
|
+
}
|
|
678
|
+
if (entry.validateJson) {
|
|
679
|
+
let parsed = null;
|
|
680
|
+
try {
|
|
681
|
+
// 校验容忍 UTF-8 BOM(与运行时判定一致:workflow-state.mjs 的 readJson 明确容忍 BOM,
|
|
682
|
+
// 外部写入如会话 Write 可能带 BOM)——否则运行时读得动的状态文件会被迁移判为损坏而
|
|
683
|
+
// 中止安装(同一份数据两处判定不一致)。strip 只作用于校验:备份与落盘始终写原始字节
|
|
684
|
+
// bytes,绝不改写用户数据(BOM strip 用字面形态——与 workflow-state.mjs 的 readJson 同形,
|
|
685
|
+
// 行为由场景 234 端到端锚定:若该字符被剥离会立即变红)。
|
|
686
|
+
parsed = JSON.parse(bytes.toString('utf8').replace(/^/, ''));
|
|
687
|
+
} catch (err) {
|
|
688
|
+
throw new Error(
|
|
689
|
+
`${oldPath} 内容不是合法 JSON(${err.message})——保留原件未改动,中止迁移。` +
|
|
690
|
+
`请修复该文件(或从备份恢复)后重跑安装器。`
|
|
691
|
+
);
|
|
692
|
+
}
|
|
693
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
694
|
+
throw new Error(`${oldPath} 顶层不是 JSON 对象——保留原件未改动,中止迁移。请修复该文件后重跑安装器。`);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
return bytes;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* 搬移落盘(写成功才删旧件):建目标目录 → 复制备份 → 逐字节校验备份 → 写新位置 →
|
|
702
|
+
* 回读校验新位置 → 删旧件。任一环节失败都抛错,且**旧件与备份至少保留一份**(不丢数据)。
|
|
703
|
+
*/
|
|
704
|
+
function writeRuntimeFileWithBackup(oldPath, newPath, backupPath, bytes) {
|
|
705
|
+
const targetDir = path.dirname(newPath);
|
|
706
|
+
const keepNote = `旧文件完整保留于 ${oldPath}`;
|
|
707
|
+
try {
|
|
708
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
709
|
+
} catch (err) {
|
|
710
|
+
throw new Error(
|
|
711
|
+
`无法建立迁移目标目录 ${targetDir}(${err.message})——${keepNote},未做任何改动。` +
|
|
712
|
+
`请确认该路径未被普通文件占用且可写后重跑安装器。`
|
|
713
|
+
);
|
|
714
|
+
}
|
|
715
|
+
try {
|
|
716
|
+
fs.copyFileSync(oldPath, backupPath);
|
|
717
|
+
} catch (err) {
|
|
718
|
+
throw new Error(
|
|
719
|
+
`生成迁移前备份失败(${backupPath}:${err.message})——${keepNote},未做任何改动。` +
|
|
720
|
+
`请解除目标目录的写入限制后重跑安装器。`
|
|
721
|
+
);
|
|
722
|
+
}
|
|
723
|
+
if (!fileEqualsBytes(backupPath, bytes)) {
|
|
724
|
+
throw new Error(`备份逐字节校验失败(${backupPath} 与源文件不一致)——${keepNote},中止迁移。请重跑安装器。`);
|
|
725
|
+
}
|
|
726
|
+
try {
|
|
727
|
+
fs.writeFileSync(newPath, bytes);
|
|
728
|
+
} catch (err) {
|
|
729
|
+
throw new Error(
|
|
730
|
+
`写入新位置失败(${newPath}:${err.message})——${keepNote},备份保留于 ${backupPath}。` +
|
|
731
|
+
`请解除该路径的写入限制后重跑安装器。`
|
|
732
|
+
);
|
|
733
|
+
}
|
|
734
|
+
if (!fileEqualsBytes(newPath, bytes)) {
|
|
735
|
+
throw new Error(
|
|
736
|
+
`新位置回读校验失败(${newPath} 与源文件不一致)——${keepNote},备份保留于 ${backupPath},中止迁移。请人工检查后重跑安装器。`
|
|
737
|
+
);
|
|
738
|
+
}
|
|
739
|
+
fs.rmSync(oldPath, { force: true });
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* 编排单个白名单文件的位置迁移(前置校验 → 备份 → 写 → 回读校验 → 删旧件);
|
|
744
|
+
* 无需迁移时把跳过说明记入 report(调用方统一打印)。
|
|
745
|
+
*/
|
|
746
|
+
function migrateRuntimeFile(target, entry, report) {
|
|
747
|
+
const oldRel = path.join(LEGACY_RUNTIME_DIR, entry.name);
|
|
748
|
+
const newRel = path.join(RUNTIME_DIR, entry.name);
|
|
749
|
+
const oldPath = path.join(target, oldRel);
|
|
750
|
+
const newPath = path.join(target, newRel);
|
|
751
|
+
const bytes = readMigratableRuntimeFile(oldPath, newPath, entry);
|
|
752
|
+
if (!bytes) {
|
|
753
|
+
report.skipped.push(`${oldRel} 不存在——无需迁移`);
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
const backupRel = path.join(RUNTIME_DIR, `${entry.name}.bak-${migrationTimestamp()}`);
|
|
757
|
+
writeRuntimeFileWithBackup(oldPath, newPath, path.join(target, backupRel), bytes);
|
|
758
|
+
report.migrated.push(`${oldRel} → ${newRel}(迁移前备份: ${backupRel})`);
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
/**
|
|
762
|
+
* 运行时文件位置迁移总入口:按 RUNTIME_FILE_MIGRATIONS 白名单逐个执行(白名单之外的内容
|
|
763
|
+
* 一律原位不动——目标项目 `.comet/` 三方共占),打印迁移/跳过报告;任一文件失败即抛错中止
|
|
764
|
+
* (旧件与备份保留,可重跑自愈)。
|
|
765
|
+
*/
|
|
766
|
+
function migrateRuntimeFiles(target) {
|
|
767
|
+
const report = { migrated: [], skipped: [] };
|
|
768
|
+
for (const entry of RUNTIME_FILE_MIGRATIONS) {
|
|
769
|
+
migrateRuntimeFile(target, entry, report);
|
|
770
|
+
}
|
|
771
|
+
console.log(`[prepare-env] 运行时文件迁移(${LEGACY_RUNTIME_DIR}/ → ${RUNTIME_DIR}/;白名单 ${RUNTIME_FILE_MIGRATIONS.length} 项):`);
|
|
772
|
+
for (const line of report.migrated) {
|
|
773
|
+
console.log(` - 已迁移: ${line}`);
|
|
774
|
+
}
|
|
775
|
+
for (const line of report.skipped) {
|
|
776
|
+
console.log(` - 已跳过: ${line}`);
|
|
777
|
+
}
|
|
778
|
+
if (report.migrated.length > 0) {
|
|
779
|
+
console.log(' (迁移前备份保留在新位置,确认无误后可自行清理)');
|
|
780
|
+
} else {
|
|
781
|
+
console.log(' (全部跳过 = 本项目的状态已在当前命名空间,安装器不再需要触碰旧目录)');
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
// ---------- .gitignore 保守纳管(目标项目;读-合并-写幂等,参照 injectSettingsHook 模式) ----------
|
|
786
|
+
|
|
787
|
+
/** 判断一行是否为 `.flow-comet/` 忽略条目(容忍首尾空白与可选的首尾斜杠变体)。 */
|
|
788
|
+
function isRuntimeDirIgnoreLine(line) {
|
|
789
|
+
return String(line).trim().replace(/^\/+/, '').replace(/\/+$/, '') === RUNTIME_DIR;
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
/**
|
|
793
|
+
* gitignore 保守纳管(读-合并-写幂等):既有条目一律保留原样——安装器无法可靠推断用户
|
|
794
|
+
* 写下 `.comet/` 条目的原始意图(实测载体用整目录形态,该目录中仍有 Comet 资产),
|
|
795
|
+
* 故只做「缺失时追加」,不改写用户其他内容(无关行/注释/既有行尾风格)。
|
|
796
|
+
* 源仓库自身跳过:`.flow-comet/` 在源仓库是权威源(需被版本控制跟踪),
|
|
797
|
+
* 整目录忽略会把权威源新增文件静默排除出版本控制。
|
|
798
|
+
*/
|
|
799
|
+
function manageGitignore(target) {
|
|
800
|
+
if (fs.existsSync(path.join(target, RUNTIME_DIR, 'skills', 'flow-comet', 'SKILL.md'))) {
|
|
801
|
+
console.log(`[prepare-env] .gitignore 纳管跳过: 当前目录是 flow-comet 源仓库(${RUNTIME_DIR}/ 为权威源,需保持被版本控制跟踪)`);
|
|
802
|
+
return;
|
|
803
|
+
}
|
|
804
|
+
const gitignorePath = path.join(target, '.gitignore');
|
|
805
|
+
let existing = '';
|
|
806
|
+
let fileExists = false;
|
|
807
|
+
try {
|
|
808
|
+
existing = fs.readFileSync(gitignorePath, 'utf8');
|
|
809
|
+
fileExists = true;
|
|
810
|
+
} catch {
|
|
811
|
+
/* 文件缺失 = 首次纳管 */
|
|
812
|
+
}
|
|
813
|
+
if (existing.split(/\r?\n/).some(isRuntimeDirIgnoreLine)) {
|
|
814
|
+
console.log(`[prepare-env] .gitignore 已含 ${RUNTIME_DIR}/ 条目——保持原样(幂等)`);
|
|
815
|
+
return;
|
|
816
|
+
}
|
|
817
|
+
const eol = existing.includes('\r\n') ? '\r\n' : '\n';
|
|
818
|
+
const appended = ['# flow-comet 运行时文件(不随仓库分发)', `${RUNTIME_DIR}/`].join(eol);
|
|
819
|
+
const separator = !fileExists || existing === '' || existing.endsWith('\n') ? '' : eol;
|
|
820
|
+
fs.writeFileSync(gitignorePath, existing + separator + appended + eol, 'utf8');
|
|
821
|
+
console.log(`[prepare-env] .gitignore 追加 ${RUNTIME_DIR}/ 条目(既有内容原样保留): ${gitignorePath}`);
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
// ---------- settings.local.json 注入(Claude Code 平台;参考 comet installClaudeCodeHooks) ----------
|
|
825
|
+
|
|
826
|
+
/** 判断 hook 命令是否为"已管理的 comet hook"(注入时替换,保证幂等)。
|
|
827
|
+
* 精确匹配:归一化命令(去前缀、统一分隔符)后必须包含
|
|
828
|
+
* 'comet-hook-guard.mjs' 作为脚本路径(而非任意位置子串),防误伤用户自定义 hook。
|
|
829
|
+
*/
|
|
830
|
+
function isManagedHookCommand(command) {
|
|
831
|
+
if (typeof command !== 'string' || command.trim() === '') {
|
|
832
|
+
return false;
|
|
833
|
+
}
|
|
834
|
+
const normalized = command.replace(/\\/g, '/').trim();
|
|
835
|
+
// 提取命令中的脚本路径(node <path> 或直接 <path>)——容忍外层引号(含空格路径防护形态):
|
|
836
|
+
// 引号由可选的 ["']? 吸收、不并入捕获组,basename 判等保持形态无关(旧相对形态 / 变量引用
|
|
837
|
+
// 形态 / 绝对路径形态 / 带引号形态均识别)。
|
|
838
|
+
const pathMatch = normalized.match(/(?:^|\s)["']?([^\s"']+\.mjs)["']?(?:\s|$)/);
|
|
839
|
+
if (!pathMatch) {
|
|
840
|
+
return false;
|
|
841
|
+
}
|
|
842
|
+
const scriptPath = pathMatch[1].split('/').pop();
|
|
843
|
+
return scriptPath === MANAGED_HOOK_COMMAND_MARKER;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
/**
|
|
847
|
+
* 合并 hook 组:已有组中过滤掉已管理的 comet hook 命令;新组按 matcher 合并进
|
|
848
|
+
* 同 matcher 的既有组(或新建组)。参考 comet mergeHookGroups。
|
|
849
|
+
*/
|
|
850
|
+
function mergeHookGroups(existingGroups, newGroups) {
|
|
851
|
+
const mergedGroups = (Array.isArray(existingGroups) ? existingGroups : []).map((group) => {
|
|
852
|
+
if (!group || typeof group !== 'object' || Array.isArray(group) || !Array.isArray(group.hooks)) {
|
|
853
|
+
return group;
|
|
854
|
+
}
|
|
855
|
+
const hadManagedHook = group.hooks.some((hook) => isManagedHookCommand(hook && hook.command));
|
|
856
|
+
const filtered = group.hooks.filter((hook) => !isManagedHookCommand(hook && hook.command));
|
|
857
|
+
// 只删除「含托管 hook 且过滤后为空」的组:matcher 演进(如 Write|Edit → Write|Edit|Bash)
|
|
858
|
+
// 会使旧托管组过滤为空,此时删除防配置污染(与 Codex purge 路径一致);
|
|
859
|
+
// 用户空组/纯用户组保留(不动用户配置——2026-08-16 修复:此前统一删空组会误删用户空 matcher 组)
|
|
860
|
+
if (hadManagedHook && filtered.length === 0) return null;
|
|
861
|
+
return { ...group, hooks: filtered };
|
|
862
|
+
}).filter(Boolean);
|
|
863
|
+
for (const newGroup of newGroups) {
|
|
864
|
+
const existingIndex = mergedGroups.findIndex(
|
|
865
|
+
(group) =>
|
|
866
|
+
group &&
|
|
867
|
+
typeof group === 'object' &&
|
|
868
|
+
!Array.isArray(group) &&
|
|
869
|
+
group.matcher === newGroup.matcher &&
|
|
870
|
+
Array.isArray(group.hooks)
|
|
871
|
+
);
|
|
872
|
+
if (existingIndex >= 0) {
|
|
873
|
+
const existing = mergedGroups[existingIndex];
|
|
874
|
+
mergedGroups[existingIndex] = { ...existing, hooks: [...existing.hooks, ...newGroup.hooks] };
|
|
875
|
+
} else {
|
|
876
|
+
mergedGroups.push(newGroup);
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
return mergedGroups;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
/**
|
|
883
|
+
* 注入 comet hook 到 settings.local.json(Claude Code 平台):
|
|
884
|
+
* 读现有 settings(缺失 → {})→ 保留全部既有字段(permissions 等)→
|
|
885
|
+
* 只更新 hooks.PreToolUse(过滤已管理 comet hook + 合并新 hook)→ 写回。
|
|
886
|
+
*
|
|
887
|
+
* fail-safe(T-FIX-13 审查修复):
|
|
888
|
+
* - settings 文件存在但 JSON 非法 → 抛错退出(不静默覆盖,避免用户内容丢失)
|
|
889
|
+
* - hooks.PreToolUse 非数组(手写变体/对象形式)→ 保留原值 + 警告(不注入,
|
|
890
|
+
* 避免破坏用户结构;提示手动配置)
|
|
891
|
+
*/
|
|
892
|
+
function injectSettingsHook(claudeDir) {
|
|
893
|
+
const settingsPath = path.join(claudeDir, 'settings.local.json');
|
|
894
|
+
let settings = {};
|
|
895
|
+
let fileExists = false;
|
|
896
|
+
try {
|
|
897
|
+
const raw = fs.readFileSync(settingsPath, 'utf8');
|
|
898
|
+
fileExists = true;
|
|
899
|
+
const parsed = JSON.parse(raw);
|
|
900
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
901
|
+
settings = parsed;
|
|
902
|
+
} else {
|
|
903
|
+
throw new Error('settings 顶层必须是 JSON 对象');
|
|
904
|
+
}
|
|
905
|
+
} catch (err) {
|
|
906
|
+
if (fileExists) {
|
|
907
|
+
// 文件存在但非法:fail-safe——不覆盖用户内容,报错退出
|
|
908
|
+
throw new Error(
|
|
909
|
+
`settings.local.json 已存在但内容非法(${err.message})——为保护用户配置,中止注入。` +
|
|
910
|
+
`请修复该文件后重试,或手动添加 hook 配置(见 README 方案 B)。`
|
|
911
|
+
);
|
|
912
|
+
}
|
|
913
|
+
// 文件缺失:视为空对象(首次安装)
|
|
914
|
+
settings = {};
|
|
915
|
+
}
|
|
916
|
+
const existingHooks = settings.hooks && typeof settings.hooks === 'object' && !Array.isArray(settings.hooks)
|
|
917
|
+
? settings.hooks
|
|
918
|
+
: {};
|
|
919
|
+
const newGroup = {
|
|
920
|
+
// R5: matcher 含 Bash——协调者禁令物理化(Bash 写源码命令同样被 hook 检测拦截;
|
|
921
|
+
// hook 对无写路径的 Bash 命令(如 git/查询)放行)
|
|
922
|
+
matcher: 'Write|Edit|Bash',
|
|
923
|
+
hooks: [
|
|
924
|
+
{
|
|
925
|
+
type: 'command',
|
|
926
|
+
// 项目根锚定(全平台统一形态):node ${CLAUDE_PROJECT_DIR}/.claude/skills/.../comet-hook-guard.mjs
|
|
927
|
+
// —— CC 以 /bin/bash -c 执行 hook 命令(含 Windows),POSIX 词形 + 正斜杠为唯一可用形态。
|
|
928
|
+
// 运行期由 CC 注入的变量展开;单一事实来源 = PROJECT_ROOT_ENV_VAR + projectRootGuardScriptRef。
|
|
929
|
+
command: `node ${projectRootGuardScriptRef('.claude')}`,
|
|
930
|
+
},
|
|
931
|
+
],
|
|
932
|
+
};
|
|
933
|
+
if (existingHooks.PreToolUse !== undefined && !Array.isArray(existingHooks.PreToolUse)) {
|
|
934
|
+
// PreToolUse 非数组(手写变体):保留原值 + 警告,不注入(避免破坏用户结构)
|
|
935
|
+
console.error(
|
|
936
|
+
'[prepare-env] 警告: settings.local.json 的 hooks.PreToolUse 不是数组(可能是手写变体)——' +
|
|
937
|
+
'为保护用户配置,未注入 comet hook。请手动添加(见 README 方案 B)。'
|
|
938
|
+
);
|
|
939
|
+
return settings;
|
|
940
|
+
}
|
|
941
|
+
const merged = mergeHookGroups(existingHooks.PreToolUse, [newGroup]);
|
|
942
|
+
settings.hooks = { ...existingHooks, PreToolUse: merged };
|
|
943
|
+
fs.mkdirSync(claudeDir, { recursive: true });
|
|
944
|
+
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n', 'utf8');
|
|
945
|
+
return settings;
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
// ---------- Codex 平台注入 + AGENTS.md 托管区(codex/dsh 共用) ----------
|
|
949
|
+
|
|
950
|
+
/**
|
|
951
|
+
* 注入 comet hook 到 .codex/hooks.json(Codex 平台):
|
|
952
|
+
* 读现有 hooks(缺失 → {})→ 过滤已管理 comet hook(命令含 comet-hook-guard.mjs)→
|
|
953
|
+
* 合并新组(matcher "*"——Codex PreToolUse 只拦截 Bash 工具,写路径经 PowerShell/Bash 命令,
|
|
954
|
+
* 必须匹配所有 Bash 调用;命令带 --platform codex 平台标记)→ 写回。
|
|
955
|
+
* 结构:顶层 "hooks" 包裹层(Codex 与 Claude Code 同构——{"hooks":{"PreToolUse":[...]}};
|
|
956
|
+
* 实测缺包裹层 hook 不加载)。
|
|
957
|
+
* fail-safe:文件存在但 JSON 非法 → 抛错退出(保护用户配置)。
|
|
958
|
+
*/
|
|
959
|
+
function injectCodexHook(codexDir, target) {
|
|
960
|
+
const hooksPath = path.join(codexDir, 'hooks.json');
|
|
961
|
+
let hooks = {};
|
|
962
|
+
let fileExists = false;
|
|
963
|
+
try {
|
|
964
|
+
const raw = fs.readFileSync(hooksPath, 'utf8');
|
|
965
|
+
fileExists = true;
|
|
966
|
+
const parsed = JSON.parse(raw);
|
|
967
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
968
|
+
hooks = parsed;
|
|
969
|
+
} else {
|
|
970
|
+
throw new Error('hooks.json 顶层必须是 JSON 对象');
|
|
971
|
+
}
|
|
972
|
+
} catch (err) {
|
|
973
|
+
if (fileExists) {
|
|
974
|
+
throw new Error(
|
|
975
|
+
`.codex/hooks.json 已存在但内容非法(${err.message})——为保护用户配置,中止注入。请修复该文件后重试。`
|
|
976
|
+
);
|
|
977
|
+
}
|
|
978
|
+
hooks = {};
|
|
979
|
+
}
|
|
980
|
+
const existingHooks = hooks.hooks && typeof hooks.hooks === 'object' && !Array.isArray(hooks.hooks)
|
|
981
|
+
? hooks.hooks
|
|
982
|
+
: {};
|
|
983
|
+
const existingGroups = Array.isArray(existingHooks.PreToolUse) ? existingHooks.PreToolUse : [];
|
|
984
|
+
// D6 查证结论(2026-08,openai/codex 官方仓库 main 分支):
|
|
985
|
+
// - hooks.json 的 command 经 shell 执行:Windows = %COMSPEC% 回退 cmd.exe /C;POSIX = $SHELL
|
|
986
|
+
// 回退 /bin/sh -lc(codex-rs/hooks/src/engine/command_runner.rs 的 build_command /
|
|
987
|
+
// default_shell_command);子进程环境 = 会话环境快照 + 条目级 env 映射,current_dir = 会话工作目录。
|
|
988
|
+
// - 无等价项目根变量:全仓 Rust 检索 CODEX_PROJECT_DIR / CLAUDE_PROJECT_DIR / project_dir
|
|
989
|
+
// 均 0 命中(GitHub code search,openai/codex);$PWD/%CD% 展开的是"会话启动目录"而非
|
|
990
|
+
// 项目根契约,子目录启动即漂移——不构成 CLAUDE_PROJECT_DIR 的等价锚点。
|
|
991
|
+
// → 按 D6 预授权退化分支:安装期把 target 绝对路径字面注入(path.join)。
|
|
992
|
+
// ⚠️ 迁移目录需重跑安装器:绝对路径随安装位置固化,项目移动/换 checkout 后必须重新运行
|
|
993
|
+
// 本安装器刷新 hooks.json(旧条目按脚本 basename 仍被 isManagedHookCommand 识别并在
|
|
994
|
+
// 重跑时被过滤替换为新位置——幂等自愈,不会产生重复条目)。
|
|
995
|
+
// 注:上游 HookHandlerConfig::Command 另有 command_windows 平台分字段(declarations.rs);
|
|
996
|
+
// 本安装器逐机注入,写入路径与宿主 OS 天然一致,无需分字段双写。
|
|
997
|
+
const guardAbsolutePath = path.join(target, '.agents', 'skills', 'flow-comet', 'scripts', MANAGED_HOOK_COMMAND_MARKER);
|
|
998
|
+
const newGroup = {
|
|
999
|
+
matcher: '*',
|
|
1000
|
+
hooks: [
|
|
1001
|
+
{
|
|
1002
|
+
type: 'command',
|
|
1003
|
+
command: `node ${guardAbsolutePath} before_tool --platform codex`,
|
|
1004
|
+
},
|
|
1005
|
+
],
|
|
1006
|
+
};
|
|
1007
|
+
const merged = mergeHookGroups(existingGroups, [newGroup]);
|
|
1008
|
+
hooks.hooks = { ...existingHooks, PreToolUse: merged };
|
|
1009
|
+
fs.mkdirSync(codexDir, { recursive: true });
|
|
1010
|
+
fs.writeFileSync(hooksPath, JSON.stringify(hooks, null, 2) + '\n', 'utf8');
|
|
1011
|
+
return hooks;
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
/**
|
|
1015
|
+
* 注入 Codex hook 启用配置到 .codex/config.toml(Codex 平台):
|
|
1016
|
+
* hooks 默认关闭——需 [features] hooks = true(部分版本 codex_hooks = true;双写兼容)。
|
|
1017
|
+
* 幂等:已有 [features] 段时合并缺失键,不覆盖用户其他配置。
|
|
1018
|
+
*/
|
|
1019
|
+
function injectCodexConfig(codexDir) {
|
|
1020
|
+
const configPath = path.join(codexDir, 'config.toml');
|
|
1021
|
+
let content = '';
|
|
1022
|
+
try {
|
|
1023
|
+
content = fs.readFileSync(configPath, 'utf8');
|
|
1024
|
+
} catch { /* 文件缺失 = 首次安装 */ }
|
|
1025
|
+
if (content.includes('[features]')) {
|
|
1026
|
+
// 已有 [features] 段:一次性补齐全部缺失键(分次 replace 会因插入点被前次消耗而失配;
|
|
1027
|
+
// CRLF 行尾与文件尾无换行均须匹配——(\[features\])(?:\r?\n|$) 覆盖)
|
|
1028
|
+
const missing = [];
|
|
1029
|
+
if (!/^hooks\s*=/m.test(content)) missing.push('hooks = true');
|
|
1030
|
+
if (!/^codex_hooks\s*=/m.test(content)) missing.push('codex_hooks = true');
|
|
1031
|
+
if (missing.length > 0) {
|
|
1032
|
+
content = content.replace(/(\[features\])(?:\r?\n|$)/, '$1\r\n' + missing.join('\r\n') + '\r\n');
|
|
1033
|
+
}
|
|
1034
|
+
} else {
|
|
1035
|
+
const trimmed = content.trim();
|
|
1036
|
+
content = (trimmed ? trimmed + '\n\n' : '') + '[features]\nhooks = true\ncodex_hooks = true\n';
|
|
1037
|
+
}
|
|
1038
|
+
fs.mkdirSync(codexDir, { recursive: true });
|
|
1039
|
+
fs.writeFileSync(configPath, content, 'utf8');
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
/**
|
|
1043
|
+
* 注入 rules 到 AGENTS.md 托管区(Codex/dsh 平台共用——AGENTS.md 是两平台指令
|
|
1044
|
+
* 唯一自动加载路径;.codex/rules/ 是命令批准规则目录,不混用)。
|
|
1045
|
+
* 读现有 AGENTS.md(缺失 → 空)→ 移除旧托管区(幂等)→ 托管区内联 orchestration 全文
|
|
1046
|
+
* → 保留托管区外用户内容 → 写回。托管区标记不变(任一平台卸载可清,另一平台重装恢复)。
|
|
1047
|
+
*/
|
|
1048
|
+
function injectManagedRules(target, stats) {
|
|
1049
|
+
const rulesSrc = path.join(BUNDLE_DRAFTS, 'rules');
|
|
1050
|
+
if (!fs.existsSync(rulesSrc)) return false;
|
|
1051
|
+
const rulesFile = path.join(rulesSrc, 'flow-comet-orchestration.md');
|
|
1052
|
+
if (!fs.existsSync(rulesFile)) return false;
|
|
1053
|
+
const rulesText = fs.readFileSync(rulesFile, 'utf8');
|
|
1054
|
+
const agentsPath = path.join(target, 'AGENTS.md');
|
|
1055
|
+
let existing = '';
|
|
1056
|
+
try {
|
|
1057
|
+
existing = fs.readFileSync(agentsPath, 'utf8');
|
|
1058
|
+
} catch { /* 文件缺失 = 首次安装 */ }
|
|
1059
|
+
const stripped = stripManagedBlock(existing, MANAGED_AGENTS_START, MANAGED_AGENTS_END);
|
|
1060
|
+
const managedBlock =
|
|
1061
|
+
MANAGED_AGENTS_START + '\n' +
|
|
1062
|
+
rulesText.trim() + '\n' +
|
|
1063
|
+
MANAGED_AGENTS_END + '\n';
|
|
1064
|
+
const merged = (stripped.trim() ? stripped.trim() + '\n\n' : '') + managedBlock;
|
|
1065
|
+
fs.writeFileSync(agentsPath, merged, 'utf8');
|
|
1066
|
+
stats.files++;
|
|
1067
|
+
return true;
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
/**
|
|
1071
|
+
* 移除 .codex/hooks.json 中的托管 hook 条目(purge 语义:移除而非注入——保留用户条目;
|
|
1072
|
+
* 重建流程会重新注入新托管条目)。文件非法 JSON 时不动(保护用户配置)。
|
|
1073
|
+
*/
|
|
1074
|
+
function removeManagedCodexHooks(codexDir) {
|
|
1075
|
+
const hooksPath = path.join(codexDir, 'hooks.json');
|
|
1076
|
+
if (!fs.existsSync(hooksPath)) return;
|
|
1077
|
+
let hooks;
|
|
1078
|
+
try {
|
|
1079
|
+
hooks = JSON.parse(fs.readFileSync(hooksPath, 'utf8'));
|
|
1080
|
+
} catch { return; } // 非法 JSON 不触碰(保护用户配置)
|
|
1081
|
+
const outer = hooks && typeof hooks === 'object' && !Array.isArray(hooks) ? hooks : {};
|
|
1082
|
+
const inner = outer.hooks && typeof outer.hooks === 'object' && !Array.isArray(outer.hooks) ? outer.hooks : {};
|
|
1083
|
+
if (!Array.isArray(inner.PreToolUse)) return;
|
|
1084
|
+
inner.PreToolUse = inner.PreToolUse
|
|
1085
|
+
.map((group) => (
|
|
1086
|
+
group && typeof group === 'object' && !Array.isArray(group) && Array.isArray(group.hooks)
|
|
1087
|
+
? { ...group, hooks: group.hooks.filter((hook) => !isManagedHookCommand(hook && hook.command)) }
|
|
1088
|
+
: group
|
|
1089
|
+
))
|
|
1090
|
+
.filter((group) => group && typeof group === 'object' && Array.isArray(group.hooks) && group.hooks.length > 0);
|
|
1091
|
+
outer.hooks = inner;
|
|
1092
|
+
fs.writeFileSync(hooksPath, JSON.stringify(outer, null, 2) + '\n', 'utf8');
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
// ---------- dsh 平台注入 ----------
|
|
1096
|
+
|
|
1097
|
+
/**
|
|
1098
|
+
* $DSH_HOME 解析(dsh 平台——与 dsh CLI 同款默认):显式 DSH_HOME 环境变量 > ~/.dsh。
|
|
1099
|
+
*/
|
|
1100
|
+
function resolveDshHome() {
|
|
1101
|
+
if (process.env.DSH_HOME) return path.resolve(process.env.DSH_HOME);
|
|
1102
|
+
return path.join(os.homedir(), '.dsh');
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
// ---------- loader 覆盖前版本比对(全局挂载的桥接 loader 生命周期治理) ----------
|
|
1106
|
+
|
|
1107
|
+
// 版本戳锚点正则(标记行格式契约:独立整行、行首无缩进、冒号后恰一个空格、行尾无其它字符;
|
|
1108
|
+
// 格式禁动——安装器与 bridge-check 双侧提取以此为准;技能包自包含,语义同值复刻不跨模块 import)。
|
|
1109
|
+
const BRIDGE_VERSION_RE = /^\/\/ BRIDGE_VERSION: ([0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?)$/m;
|
|
1110
|
+
|
|
1111
|
+
/**
|
|
1112
|
+
* 从 loader 文件提取版本戳(同一锚点正则)。返回 { ok:true, version };
|
|
1113
|
+
* 文件缺失/读取失败/无标记行 → { ok:false, reason }(调用方告警兜底,不抛错)。
|
|
1114
|
+
*/
|
|
1115
|
+
function extractBridgeVersion(loaderPath) {
|
|
1116
|
+
let text;
|
|
1117
|
+
try {
|
|
1118
|
+
text = fs.readFileSync(loaderPath, 'utf8');
|
|
1119
|
+
} catch {
|
|
1120
|
+
return { ok: false, reason: '文件读取失败' };
|
|
1121
|
+
}
|
|
1122
|
+
const match = BRIDGE_VERSION_RE.exec(text);
|
|
1123
|
+
if (match) return { ok: true, version: match[1] };
|
|
1124
|
+
// 标记行存在但形态非法(区别于「无标记行」)——仍走既有警告路径,原因精确可诊断
|
|
1125
|
+
const loose = /^\/\/ BRIDGE_VERSION: (.*)$/m.exec(text);
|
|
1126
|
+
return loose
|
|
1127
|
+
? { ok: false, reason: '版本戳形态非法(非语义化版本: ' + loose[1] + ')' }
|
|
1128
|
+
: { ok: false, reason: '未提取到版本戳标记行' };
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
/**
|
|
1132
|
+
* 语义化版本数值序比较(覆盖前判定方向):major.minor.patch 逐段按数值比较
|
|
1133
|
+
* (如 1.10.0 > 1.6.0——非字典序);核心版本相等时按语义化版本规则处理预发布后缀
|
|
1134
|
+
* (如 1.6.0-alpha.1 < 1.6.0)。返回 -1 / 0 / 1。
|
|
1135
|
+
*/
|
|
1136
|
+
function compareBridgeVersions(a, b) {
|
|
1137
|
+
const parse = (v) => {
|
|
1138
|
+
const text = String(v);
|
|
1139
|
+
const dash = text.indexOf('-');
|
|
1140
|
+
const core = (dash === -1 ? text : text.slice(0, dash)).split('.').map((seg) => parseInt(seg, 10));
|
|
1141
|
+
return { core, pre: dash === -1 ? null : text.slice(dash + 1) };
|
|
1142
|
+
};
|
|
1143
|
+
const av = parse(a);
|
|
1144
|
+
const bv = parse(b);
|
|
1145
|
+
const coreLen = Math.max(av.core.length, bv.core.length);
|
|
1146
|
+
for (let i = 0; i < coreLen; i++) {
|
|
1147
|
+
const x = av.core[i] ?? 0;
|
|
1148
|
+
const y = bv.core[i] ?? 0;
|
|
1149
|
+
if (x !== y) return x < y ? -1 : 1;
|
|
1150
|
+
}
|
|
1151
|
+
const ap = av.pre !== null;
|
|
1152
|
+
const bp = bv.pre !== null;
|
|
1153
|
+
if (ap !== bp) return ap ? -1 : 1; // 预发布 < 正式版
|
|
1154
|
+
if (!ap) return 0;
|
|
1155
|
+
const ai = av.pre.split('.');
|
|
1156
|
+
const bi = bv.pre.split('.');
|
|
1157
|
+
const preLen = Math.max(ai.length, bi.length);
|
|
1158
|
+
for (let i = 0; i < preLen; i++) {
|
|
1159
|
+
if (i >= ai.length) return -1; // 更短标识符列表是更长列表的前缀 → 更小
|
|
1160
|
+
if (i >= bi.length) return 1;
|
|
1161
|
+
const x = ai[i];
|
|
1162
|
+
const y = bi[i];
|
|
1163
|
+
if (x === y) continue;
|
|
1164
|
+
const xn = /^\d+$/.test(x) ? parseInt(x, 10) : null;
|
|
1165
|
+
const yn = /^\d+$/.test(y) ? parseInt(y, 10) : null;
|
|
1166
|
+
if (xn !== null && yn !== null) return xn < yn ? -1 : 1;
|
|
1167
|
+
if (xn !== null) return -1; // 数字标识符 < 字母标识符(语义化版本规则)
|
|
1168
|
+
if (yn !== null) return 1;
|
|
1169
|
+
return x < y ? -1 : 1;
|
|
1170
|
+
}
|
|
1171
|
+
return 0;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
/**
|
|
1175
|
+
* 覆盖前版本比对明示:从权威源 loader 与已装 loader 各提取版本戳,按数值序输出
|
|
1176
|
+
* 首次安装 / 升级 A→B / 降级 A→B / 版本一致 四类结论之一(人读、带 [prepare-env] 前缀)。
|
|
1177
|
+
* 任一侧提取失败(文件缺失/无标记行)→ 告警并跳过比对——不抛错、不中断安装;
|
|
1178
|
+
* 覆盖动作(copyFileSync)由调用方照常执行,幂等语义不变。
|
|
1179
|
+
*/
|
|
1180
|
+
function reportLoaderVersionTransition(loaderSrc, loaderDst) {
|
|
1181
|
+
if (!fs.existsSync(loaderDst)) {
|
|
1182
|
+
console.log('[prepare-env] 桥接 loader 首次安装($DSH_HOME/plugins 无已装 loader)');
|
|
1183
|
+
return;
|
|
1184
|
+
}
|
|
1185
|
+
const src = extractBridgeVersion(loaderSrc);
|
|
1186
|
+
const installed = extractBridgeVersion(loaderDst);
|
|
1187
|
+
if (!src.ok || !installed.ok) {
|
|
1188
|
+
const failedSide = !src.ok ? '权威源 loader' : '已装 loader';
|
|
1189
|
+
const reason = !src.ok ? src.reason : installed.reason;
|
|
1190
|
+
console.warn(
|
|
1191
|
+
`[prepare-env] 警告: ${failedSide} 版本戳提取失败(${reason})——跳过版本比对,继续覆盖安装`
|
|
1192
|
+
);
|
|
1193
|
+
return;
|
|
1194
|
+
}
|
|
1195
|
+
const a = installed.version;
|
|
1196
|
+
const b = src.version;
|
|
1197
|
+
const cmp = compareBridgeVersions(a, b);
|
|
1198
|
+
if (cmp < 0) {
|
|
1199
|
+
console.log(`[prepare-env] 桥接 loader 升级 ${a} → ${b}`);
|
|
1200
|
+
} else if (cmp > 0) {
|
|
1201
|
+
console.log(`[prepare-env] 桥接 loader 降级 ${a} → ${b}`);
|
|
1202
|
+
} else {
|
|
1203
|
+
console.log(`[prepare-env] 桥接 loader 版本一致(${a})`);
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
/**
|
|
1208
|
+
* 极简 YAML 形状校验(零依赖——用于 cordis.patch.yml 的 fail-safe 判定):
|
|
1209
|
+
* 文件须为顶层 YAML 列表形态(行形态:注释 / 空行 / 顶层 `- 条目` / 条目下缩进内容 / 文档分隔符),
|
|
1210
|
+
* 无法识别(二进制、顶层 map 形态等)→ 返回 false——调用方据此 fail-safe 不覆盖(保护用户配置)。
|
|
1211
|
+
*/
|
|
1212
|
+
function isSimpleYamlText(text) {
|
|
1213
|
+
return text.split(/\r?\n/).every((line) => {
|
|
1214
|
+
const t = line.trimEnd();
|
|
1215
|
+
if (t.trim() === '') return true; // 空行
|
|
1216
|
+
if (/^\s*#/.test(t)) return true; // 注释
|
|
1217
|
+
if (/^\s*---\s*$/.test(t)) return true; // 文档分隔符
|
|
1218
|
+
if (/^\s*-\s+\S/.test(t)) return true; // 顶层列表条目
|
|
1219
|
+
if (/^\s+\S/.test(t)) return true; // 条目下缩进内容(键值/嵌套列表;YAML 允许 1 空格缩进)
|
|
1220
|
+
return false;
|
|
1221
|
+
});
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
/**
|
|
1225
|
+
* 注入桥接 loader 托管块到 $DSH_HOME/cordis.patch.yml(dsh 平台 home patch——
|
|
1226
|
+
* 读-合并-写,L-008 非破坏):
|
|
1227
|
+
* 读现有 cordis.patch.yml(缺失 → 空)→ 移除旧托管块(幂等)→ 托管块内联 loader 的
|
|
1228
|
+
* file:// 引用(**insert 形态**:无 id 的顶层 `- insert:` 条目内嵌插件行
|
|
1229
|
+
* `- id: dsh-flow-comet-bridge` + `name: 'file:///<abs 路径>'`——dsh-app-boot 的
|
|
1230
|
+
* applyEntryPatches 语义是 id-targeted patch(id 必须已存在于配置树);无 id 的
|
|
1231
|
+
* `insert:` 顶层条目才执行 data.push 追加新插件行,是 home patch 新增插件的唯一正确形态;
|
|
1232
|
+
* 旧的 `- id: ... + name:` patch 形态对不存在的 id 报 entry not found 并跳过——
|
|
1233
|
+
* loader 从不加载 → 拦截整链静默失效)
|
|
1234
|
+
* → 保留托管块外既有块(dsh-skin 等)→ 写回。
|
|
1235
|
+
* fail-safe:文件存在但内容无法识别为 YAML → 抛错退出不覆盖(参照 injectSettingsHook 模式)。
|
|
1236
|
+
*/
|
|
1237
|
+
function injectDshCordisPatch(dshHome, loaderPath) {
|
|
1238
|
+
const patchPath = path.join(dshHome, 'cordis.patch.yml');
|
|
1239
|
+
let existing = '';
|
|
1240
|
+
let fileExists = false;
|
|
1241
|
+
try {
|
|
1242
|
+
existing = fs.readFileSync(patchPath, 'utf8');
|
|
1243
|
+
fileExists = true;
|
|
1244
|
+
} catch { /* 文件缺失 = 首次安装 */ }
|
|
1245
|
+
if (fileExists && !isSimpleYamlText(existing)) {
|
|
1246
|
+
throw new Error(
|
|
1247
|
+
`cordis.patch.yml 已存在但内容无法解析为 YAML(${patchPath})——为保护用户配置,中止注入。` +
|
|
1248
|
+
`请修复该文件(顶层 YAML 列表形态)后重试,或手动添加桥接 loader 引用。`
|
|
1249
|
+
);
|
|
1250
|
+
}
|
|
1251
|
+
const stripped = stripManagedBlock(existing, MANAGED_CORDIS_START, MANAGED_CORDIS_END);
|
|
1252
|
+
const fileUrl = pathToFileURL(loaderPath).href;
|
|
1253
|
+
const managedBlock =
|
|
1254
|
+
MANAGED_CORDIS_START + '\n' +
|
|
1255
|
+
'- insert:\n' +
|
|
1256
|
+
' - id: dsh-flow-comet-bridge\n' +
|
|
1257
|
+
` name: '${fileUrl.replace(/'/g, "''")}'\n` +
|
|
1258
|
+
MANAGED_CORDIS_END + '\n';
|
|
1259
|
+
const merged = (stripped.trim() ? stripped.trim() + '\n\n' : '') + managedBlock;
|
|
1260
|
+
fs.mkdirSync(dshHome, { recursive: true });
|
|
1261
|
+
fs.writeFileSync(patchPath, merged, 'utf8');
|
|
1262
|
+
return merged;
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
/**
|
|
1266
|
+
* 移除 $DSH_HOME/cordis.patch.yml 中的托管块(purge 语义:移除而非注入——保留 dsh-skin
|
|
1267
|
+
* 等既有块与用户内容)。文件缺失 → 跳过;内容无法识别为 YAML → 不动(fail-safe 保护
|
|
1268
|
+
* 用户配置)。移除后文件为空 → 删除文件本身。返回删除清单条目(供 purge 打印)。
|
|
1269
|
+
*/
|
|
1270
|
+
function removeDshCordisPatch(dshHome) {
|
|
1271
|
+
const patchPath = path.join(dshHome, 'cordis.patch.yml');
|
|
1272
|
+
const removed = [];
|
|
1273
|
+
if (!fs.existsSync(patchPath)) return removed;
|
|
1274
|
+
let content;
|
|
1275
|
+
try {
|
|
1276
|
+
content = fs.readFileSync(patchPath, 'utf8');
|
|
1277
|
+
} catch { return removed; }
|
|
1278
|
+
if (!content.includes(MANAGED_CORDIS_START)) return removed;
|
|
1279
|
+
if (!isSimpleYamlText(content)) {
|
|
1280
|
+
// 无法识别 → 不触碰(fail-safe 语义与注入一致——保护用户配置)
|
|
1281
|
+
return removed;
|
|
1282
|
+
}
|
|
1283
|
+
const stripped = stripManagedBlock(content, MANAGED_CORDIS_START, MANAGED_CORDIS_END);
|
|
1284
|
+
const trimmed = stripped.trim();
|
|
1285
|
+
if (trimmed) {
|
|
1286
|
+
fs.writeFileSync(patchPath, trimmed + '\n', 'utf8');
|
|
1287
|
+
} else {
|
|
1288
|
+
fs.rmSync(patchPath, { force: true });
|
|
1289
|
+
}
|
|
1290
|
+
removed.push(patchPath + '(托管块)');
|
|
1291
|
+
return removed;
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
function escapeRegExp(value) {
|
|
1295
|
+
return String(value).replace(/[|\\{}()[\]^$+?.]/g, '\\$&');
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
// 移除托管块(幂等替换边界):start 标记到 end 标记(含尾随换行)之间的全部内容。
|
|
1299
|
+
// 注入与清理共用——AGENTS.md 托管区 / cordis.patch.yml 托管块(L-008 读-合并-写)。
|
|
1300
|
+
function stripManagedBlock(content, startMarker, endMarker) {
|
|
1301
|
+
return content.replace(
|
|
1302
|
+
new RegExp(escapeRegExp(startMarker) + '[\\s\\S]*?' + escapeRegExp(endMarker) + '\\n?'),
|
|
1303
|
+
''
|
|
1304
|
+
);
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
// ---------- 版本标识(全部平台共用) ----------
|
|
1308
|
+
|
|
1309
|
+
/**
|
|
1310
|
+
* 版本标识:优先从源仓库 git describe 生成(多人协作精确检测——发布版 = 精确 tag,
|
|
1311
|
+
* 开发态 = "<tag>-<领先提交数>-g<hash>",提 issue 时维护者可据此判断包含哪些积累);
|
|
1312
|
+
* 无 git(纯手动复制兜底)时用权威源随技能包分发的 INSTALLED_VERSION(最近发布版本)。
|
|
1313
|
+
*/
|
|
1314
|
+
function resolveInstalledVersion() {
|
|
1315
|
+
let installedVersion = '';
|
|
1316
|
+
try {
|
|
1317
|
+
const desc = execFileSync('git', ['describe', '--tags'], { cwd: REPO_ROOT, encoding: 'utf8', timeout: 30000 }).trim();
|
|
1318
|
+
if (desc) installedVersion = desc.replace(/^v/, ''); // 剥 v 前缀,与 CHANGELOG 版本号一致
|
|
1319
|
+
} catch { /* 无 tag 或非 git 仓库:回退权威源文件 */ }
|
|
1320
|
+
if (!installedVersion) {
|
|
1321
|
+
const bundled = path.join(BUNDLE_DRAFTS, 'skills', 'flow-comet', 'INSTALLED_VERSION');
|
|
1322
|
+
if (fs.existsSync(bundled)) installedVersion = fs.readFileSync(bundled, 'utf8').trim();
|
|
1323
|
+
}
|
|
1324
|
+
return installedVersion;
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
// ---------- flow-kit 获取链路(ADR-008 / DESIGN D1~D6——平台无关,main 平台循环前调用一次) ----------
|
|
1328
|
+
|
|
1329
|
+
// 锁定点常量(D2):上游 github.com/rihebty/flow-kit 实测无 tag,commit 是唯一精确锚;
|
|
1330
|
+
// 升级锁定点 = 显式修改此常量并记 CHANGELOG,不做自动跟随(2026-08-23 ls-remote 实测 == 上游 HEAD)。
|
|
1331
|
+
const FLOW_KIT_UPSTREAM_URL = 'https://github.com/rihebty/flow-kit.git';
|
|
1332
|
+
const FLOW_KIT_LOCKED_COMMIT = '9b5dda7206ae841230f118348d660ad8d0ae2830';
|
|
1333
|
+
// git 网络调用统一超时(D5——沿用 resolveInstalledVersion 的 execFileSync timeout 先例风格)
|
|
1334
|
+
const FLOW_KIT_GIT_TIMEOUT_MS = 60000;
|
|
1335
|
+
// 摘要展示用短 sha 长度(git 短哈希惯例——单一来源,clone 成功/已有/指引三处引用)
|
|
1336
|
+
const FLOW_KIT_SHORT_SHA_LEN = 7;
|
|
1337
|
+
|
|
1338
|
+
/** 提取 git 调用失败信息首行(execFileSync 错误消息含多行 stderr——警告只留首行) */
|
|
1339
|
+
function gitErrorFirstLine(err) {
|
|
1340
|
+
return String(err && err.message ? err.message : err).split('\n')[0].trim();
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
/** 手动获取指引(clone/checkout 失败或目录非上游克隆时打印——含上游 URL 与目标路径;不抛错) */
|
|
1344
|
+
function printFlowKitManualGuide(target) {
|
|
1345
|
+
const flowKitDir = path.join(target, 'flow-kit');
|
|
1346
|
+
console.warn(`[prepare-env] 手动获取指引: git clone ${FLOW_KIT_UPSTREAM_URL} "${flowKitDir}"`);
|
|
1347
|
+
console.warn(`[prepare-env] 然后锁定快照: git -C "${flowKitDir}" checkout --detach ${FLOW_KIT_LOCKED_COMMIT.slice(0, FLOW_KIT_SHORT_SHA_LEN)}`);
|
|
1348
|
+
console.warn('[prepare-env] 未获取 flow-kit 时技能协议引用悬空、guard 段名基准退化为内置 fallback——其余安装职责不受影响。');
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
/**
|
|
1352
|
+
* 归属判定(D3):origin remote URL 匹配上游——本地 config 读取,无网络。
|
|
1353
|
+
* 兼容 https / ssh 形态与可选 .git 后缀;读取失败视为不匹配(保守落入"非上游克隆"分支,
|
|
1354
|
+
* 绝不改动用户目录)。返回 'match' | 'mismatch' | 'unreadable'。
|
|
1355
|
+
*/
|
|
1356
|
+
function flowKitOriginStatus(flowKitDir) {
|
|
1357
|
+
try {
|
|
1358
|
+
const url = execFileSync(
|
|
1359
|
+
'git',
|
|
1360
|
+
['-C', flowKitDir, 'config', '--get', 'remote.origin.url'],
|
|
1361
|
+
{ encoding: 'utf8', timeout: FLOW_KIT_GIT_TIMEOUT_MS }
|
|
1362
|
+
).trim().replace(/\.git$/i, '').replace(/\/+$/, '');
|
|
1363
|
+
return /github\.com[:/]rihebty\/flow-kit$/i.test(url) ? 'match' : 'mismatch';
|
|
1364
|
+
} catch {
|
|
1365
|
+
return 'unreadable';
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
/**
|
|
1370
|
+
* 确保 <target>/flow-kit/ 就位(ADR-008 / DESIGN D1~D6——L-008 非破坏哲学:已存在路径一律只读不动):
|
|
1371
|
+
* 路径 1 目标不存在 → execFileSync('git',…) 数组参数形态 clone 上游后 detached checkout 到锁定 commit;
|
|
1372
|
+
* 路径 2 已存在且 .git 存在且 origin 匹配上游 → 只读 rev-parse HEAD 并输出与锁定点差异影响,绝不改动;
|
|
1373
|
+
* 路径 3 已存在但不满足归属判定 → 摘要"非上游克隆,已跳过"+ 手动指引,不改动;
|
|
1374
|
+
* 路径 4 clone/checkout 任一失败 → 警告 + 手动获取指引,**不抛错**(D5——外网故障不得绑架安装器
|
|
1375
|
+
* 其余职责,进程照常 exit 0);各平台 purge 清单永不包含 flow-kit(D6,见 PLATFORMS 表头注释)。
|
|
1376
|
+
*/
|
|
1377
|
+
function ensureFlowKit(target) {
|
|
1378
|
+
const flowKitDir = path.join(target, 'flow-kit');
|
|
1379
|
+
const shortLock = FLOW_KIT_LOCKED_COMMIT.slice(0, FLOW_KIT_SHORT_SHA_LEN);
|
|
1380
|
+
if (!fs.existsSync(flowKitDir)) {
|
|
1381
|
+
// 路径 1:目标缺失 → clone + detached checkout 到锁定 commit(D1/D2)
|
|
1382
|
+
try {
|
|
1383
|
+
execFileSync('git', ['clone', FLOW_KIT_UPSTREAM_URL, flowKitDir], {
|
|
1384
|
+
encoding: 'utf8',
|
|
1385
|
+
timeout: FLOW_KIT_GIT_TIMEOUT_MS,
|
|
1386
|
+
});
|
|
1387
|
+
execFileSync('git', ['-C', flowKitDir, 'checkout', '--detach', FLOW_KIT_LOCKED_COMMIT], {
|
|
1388
|
+
encoding: 'utf8',
|
|
1389
|
+
timeout: FLOW_KIT_GIT_TIMEOUT_MS,
|
|
1390
|
+
});
|
|
1391
|
+
console.log(`[prepare-env] 已获取 flow-kit(锁定 ${shortLock})`);
|
|
1392
|
+
} catch (err) {
|
|
1393
|
+
// 路径 4:clone/checkout 任一失败 → WARN + 手动指引,不抛错
|
|
1394
|
+
console.warn(`[prepare-env] 警告: flow-kit 自动获取失败(${gitErrorFirstLine(err)})`);
|
|
1395
|
+
printFlowKitManualGuide(target);
|
|
1396
|
+
}
|
|
1397
|
+
return;
|
|
1398
|
+
}
|
|
1399
|
+
// 目录已存在 → 归属判定(D3:.git 存在 且 origin remote 匹配上游)
|
|
1400
|
+
const hasDotGit = fs.existsSync(path.join(flowKitDir, '.git'));
|
|
1401
|
+
const originStatus = hasDotGit ? flowKitOriginStatus(flowKitDir) : 'mismatch';
|
|
1402
|
+
if (originStatus !== 'match') {
|
|
1403
|
+
// 路径 3:同名非克隆目录(或 remote 无法确认归属)→ 跳过 + 指引,绝不改动
|
|
1404
|
+
if (originStatus === 'unreadable') {
|
|
1405
|
+
console.warn('[prepare-env] 警告: flow-kit 目录存在且含 .git,但 origin remote 读取失败——无法确认归属');
|
|
1406
|
+
}
|
|
1407
|
+
console.log('[prepare-env] 目录存在但非上游克隆,已跳过');
|
|
1408
|
+
printFlowKitManualGuide(target);
|
|
1409
|
+
return;
|
|
1410
|
+
}
|
|
1411
|
+
// 路径 2:上游克隆 → 只读检测并输出差异影响(D4——绝不 fetch/checkout 改动用户克隆)
|
|
1412
|
+
try {
|
|
1413
|
+
const head = execFileSync('git', ['-C', flowKitDir, 'rev-parse', 'HEAD'], {
|
|
1414
|
+
encoding: 'utf8',
|
|
1415
|
+
timeout: FLOW_KIT_GIT_TIMEOUT_MS,
|
|
1416
|
+
}).trim();
|
|
1417
|
+
const shortHead = head.slice(0, FLOW_KIT_SHORT_SHA_LEN);
|
|
1418
|
+
console.log(`[prepare-env] 已有 flow-kit(HEAD=${shortHead},推荐锁定点=${shortLock})`);
|
|
1419
|
+
if (head === FLOW_KIT_LOCKED_COMMIT) {
|
|
1420
|
+
console.log('[prepare-env] 当前 HEAD 与推荐锁定点一致。');
|
|
1421
|
+
} else {
|
|
1422
|
+
console.warn(
|
|
1423
|
+
`[prepare-env] 差异影响: 当前 HEAD 与推荐锁定点不一致——guard 段名基准与协议引用可能偏离安装器锁定内容` +
|
|
1424
|
+
`(安装器绝不改动已有克隆;如需对齐请手动执行 git -C "${flowKitDir}" fetch 后 checkout --detach ${shortLock})`
|
|
1425
|
+
);
|
|
1426
|
+
}
|
|
1427
|
+
} catch (err) {
|
|
1428
|
+
// HEAD 只读失败同样不改动、不中断安装(保守降级为警告)
|
|
1429
|
+
console.warn(`[prepare-env] 警告: flow-kit HEAD 读取失败(${gitErrorFirstLine(err)}),已跳过比对`);
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
// ---------- 主流程 ----------
|
|
1434
|
+
|
|
1435
|
+
async function main() {
|
|
1436
|
+
const { target, purge, yes, platform: platformArg } = parseArgs(process.argv);
|
|
1437
|
+
|
|
1438
|
+
// --purge 的 --yes 二次确认(防误传导致整删)**先于一切写操作**:被拒绝的命令不得产生
|
|
1439
|
+
// 任何副作用——确认校验若排在迁移之后,命令最终被拒绝但迁移已经执行、目标已被修改。
|
|
1440
|
+
if (purge && !yes) {
|
|
1441
|
+
throw new Error('--purge 是破坏性操作,需显式 --yes 确认:node scripts/prepare-env.mjs --target <dir> --purge --yes');
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
const platforms = await resolvePlatform(target, platformArg);
|
|
1445
|
+
const skillsSrc = path.join(BUNDLE_DRAFTS, 'skills');
|
|
1446
|
+
if (!fs.existsSync(skillsSrc)) {
|
|
1447
|
+
throw new Error(`权威源 skills 目录不存在: ${skillsSrc}`);
|
|
1448
|
+
}
|
|
1449
|
+
const platformsLabel = platforms.map((p) => p.label).join(' + ');
|
|
1450
|
+
|
|
1451
|
+
// 执行顺序第一段:运行时文件位置迁移(先于任何部署写入——失败即中止,保持可重跑的一致态)
|
|
1452
|
+
migrateRuntimeFiles(target);
|
|
1453
|
+
|
|
1454
|
+
// --purge(已在上方确认 --yes):逐平台删除生成物后重新生成
|
|
1455
|
+
if (purge) {
|
|
1456
|
+
console.error(`[prepare-env] 警告: --purge 将删除 ${platformsLabel} 平台的以下生成物(不可恢复):`);
|
|
1457
|
+
for (const platform of platforms) {
|
|
1458
|
+
for (const entry of platform.purge(target)) {
|
|
1459
|
+
console.error(` - ${entry}`);
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
console.error('[prepare-env] 已删除,开始重新生成。');
|
|
1463
|
+
}
|
|
1464
|
+
|
|
1465
|
+
// flow-kit 获取链路(ADR-008 / D1~D6——平台无关,平台循环之前调用一次;
|
|
1466
|
+
// 内部四条路径均不抛错、不阻断后续平台安装职责)
|
|
1467
|
+
if (!purge) ensureFlowKit(target);
|
|
1468
|
+
|
|
1469
|
+
for (const platform of platforms) {
|
|
1470
|
+
const skillRoot = platform.skillRoot(target);
|
|
1471
|
+
const stats = { dirs: 0, files: 0, skills: [] };
|
|
1472
|
+
|
|
1473
|
+
// 覆盖前打印将覆盖的生成物清单(默认非破坏:只覆盖生成物)
|
|
1474
|
+
console.log(`[prepare-env] 平台: ${platform.label} — 将覆盖以下生成物(其他内容保留):`);
|
|
1475
|
+
console.log(` - ${skillRoot}`);
|
|
1476
|
+
for (const line of platform.overwriteDescription(target)) {
|
|
1477
|
+
console.log(` - ${line}`);
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
// 1. hook 注入(平台化:描述符驱动——CC=settings.local.json;Codex=hooks.json+config.toml)
|
|
1481
|
+
platform.installHooks(target, stats);
|
|
1482
|
+
|
|
1483
|
+
// 2. rules 注入(平台化:描述符驱动——CC=.claude/rules/ 复制;Codex=AGENTS.md 托管区)
|
|
1484
|
+
platform.installRules(target, stats);
|
|
1485
|
+
|
|
1486
|
+
// 3. skills/(复制全部 flow-comet* 目录 + 平台路径替换)
|
|
1487
|
+
const skillNames = fs
|
|
1488
|
+
.readdirSync(skillsSrc, { withFileTypes: true })
|
|
1489
|
+
.filter((e) => e.isDirectory() && /^flow-comet/.test(e.name))
|
|
1490
|
+
.map((e) => e.name)
|
|
1491
|
+
.sort();
|
|
1492
|
+
if (skillNames.length === 0) {
|
|
1493
|
+
throw new Error(`权威源中未找到任何 flow-comet* skill 目录: ${skillsSrc}`);
|
|
1494
|
+
}
|
|
1495
|
+
for (const name of skillNames) {
|
|
1496
|
+
copyTree(path.join(skillsSrc, name), path.join(skillRoot, name), stats);
|
|
1497
|
+
stats.skills.push(name);
|
|
1498
|
+
}
|
|
1499
|
+
const replacedFiles = applyPathReplacements(skillRoot, platform.pathReplacements);
|
|
1500
|
+
if (replacedFiles > 0) {
|
|
1501
|
+
console.log(`[prepare-env] 平台路径替换: ${replacedFiles} 个 .md 文件(${platform.label} 命令路径)`);
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
// 4. 版本标识(写入平台技能根,随技能包分发)
|
|
1505
|
+
const installedVersion = resolveInstalledVersion();
|
|
1506
|
+
fs.writeFileSync(path.join(skillRoot, 'flow-comet', 'INSTALLED_VERSION'), installedVersion + '\n', 'utf8');
|
|
1507
|
+
stats.files++;
|
|
1508
|
+
|
|
1509
|
+
// 摘要(逐平台)
|
|
1510
|
+
console.log(`[prepare-env] 已准备环境: ${target}(${platform.label})`);
|
|
1511
|
+
console.log(
|
|
1512
|
+
`[prepare-env] 目录 ${stats.dirs} 个、文件 ${stats.files} 个、skills ${stats.skills.length} 个`
|
|
1513
|
+
);
|
|
1514
|
+
console.log(`[prepare-env] skills: ${stats.skills.join(', ')}`);
|
|
1515
|
+
}
|
|
1516
|
+
|
|
1517
|
+
// 执行顺序第三段:.gitignore 保守纳管(部署成功后执行——失败即中止,可重跑自愈)
|
|
1518
|
+
manageGitignore(target);
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
try {
|
|
1522
|
+
await main();
|
|
1523
|
+
} catch (err) {
|
|
1524
|
+
console.error(`[prepare-env] 错误: ${err.message}`);
|
|
1525
|
+
process.exit(1);
|
|
1526
|
+
}
|