dsh-git-push 2.9.4 → 2.9.9
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/README.md +21 -25
- package/lib/app/apply.js +8 -1
- package/lib/app/audit-api.js +4 -1
- package/lib/app/command-registry.js +17 -11
- package/lib/app/handlers/meta.js +38 -0
- package/lib/app/handlers/repo-actions.js +3 -1
- package/lib/app/handlers/settings.js +0 -0
- package/lib/app/http-handlers.js +3 -1
- package/lib/app/schema.js +0 -0
- package/lib/app/settings-bridge.js +0 -0
- package/lib/app/tool-call.js +26 -21
- package/lib/ast/control-flow.js +12 -3
- package/lib/ast/size.js +5 -2
- package/lib/audit/audit-file.js +4 -2
- package/lib/audit/file-context.js +89 -0
- package/lib/audit/finding.js +16 -2
- package/lib/audit/index.js +2 -2
- package/lib/checks/button-bind.js +25 -2
- package/lib/commit-push.js +20 -2
- package/lib/core/exec.js +6 -4
- package/lib/exempt/tool.js +198 -0
- package/lib/git/clone-history-mode.js +24 -5
- package/lib/git/push.js +28 -13
- package/lib/git/transport.js +49 -17
- package/lib/git/visibility.js +93 -9
- package/lib/npm/publish.js +27 -0
- package/lib/npm/registry.js +5 -2
- package/lib/self/index.js +1 -1
- package/package.json +2 -4
- package/skills/dev/dsh-git-push.md +1 -2
- package/skills/guide/guide-cli-scripts.md +3 -3
- package/skills/guide/guide-inject-ui.md +4 -4
- package/assets/minihost.sh +0 -35
- package/assets/preview.html +0 -36703
- package/lib/fs/edit-after-read.js +0 -142
- package/scripts/frontend-preview-check.mjs +0 -124
- package/scripts/preview-gen.mjs +0 -489
- package/scripts/preview-local-runner.mjs +0 -55
- package/scripts/preview-server-direct.mjs +0 -116
- package/scripts/preview-server.mjs +0 -305
- package/scripts/preview-slot-data.mjs +0 -123
- package/scripts/sync-plugin-install.mjs +0 -286
- package/scripts/sync-plugin.mjs +0 -284
|
@@ -1,286 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 插件部署形态与 HMR 自检(sync-plugin 的安装位置逻辑,2026-10-09 拆出)
|
|
3
|
-
*
|
|
4
|
-
* 背景(来源:开发者文档《DSH 插件免重启热重载 —— 打通方法与两个必要条件》):
|
|
5
|
-
* 服务端插件代码要**免重启热重载**,必须同时满足两条,缺一不可:
|
|
6
|
-
* ① hmr 插件要显式配 `root`(base bundle 默认 `root: []` = 「module roots are opt-in」,一个模块目录都不 watch);
|
|
7
|
-
* ② 插件本体要放在 `<profile>/local-plugins/`,**不能只放 node_modules**
|
|
8
|
-
* (hmr 的 ignored 默认含 `node_modules/**`,chokidar 遇到该目录整棵剪枝——
|
|
9
|
-
* 想「只放行 node_modules 下某个插件」的否定写法也无效)。
|
|
10
|
-
* 另外 `config.base` 是相对 **hmr 自己的包位置**解析的(不是相对 profile),
|
|
11
|
-
* 所以只写 `root: ['local-plugins']` 会解析到 `node_modules/@deepseek-ai/dsh-hmr/local-plugins` —— 必须同时给 base。
|
|
12
|
-
*
|
|
13
|
-
* 本模块只做「看清楚 + 给方案 + 按需执行」:
|
|
14
|
-
* inspectProfiles 列 profile 与插件双副本形态
|
|
15
|
-
* readHmrConfig 读该 profile 的 cordis.patch.yml 里 id: hmr 的 base/root
|
|
16
|
-
* judgeHmr 判定「这个插件改代码是否需要重启」
|
|
17
|
-
* planInstall 目标形态与现状的差异清单(默认只报告)
|
|
18
|
-
* applyInstall 按 on/off 执行安装形态动作(用改动本体的 sync 步骤落真实文件)
|
|
19
|
-
* depHints package.json 依赖声明是否还指向 node_modules / 绝对 link:
|
|
20
|
-
*/
|
|
21
|
-
import { existsSync, readdirSync, lstatSync, unlinkSync, mkdirSync, readFileSync } from 'node:fs';
|
|
22
|
-
import { join, resolve, sep } from 'node:path';
|
|
23
|
-
|
|
24
|
-
/** DSH 数据根(含 .dsh 的那一层)。兼容两种入参形态:
|
|
25
|
-
* A) DSH_HOME 本尊:`…/0.2.0-rc.2` → profiles 在 `<h>/.dsh/profiles`
|
|
26
|
-
* B) 已含 .dsh 的路径:`…/0.2.0-rc.2/.dsh` → profiles 在 `<h>/profiles`
|
|
27
|
-
* 两种都探测不到时按 A 返回(保持既有调用方行为),由上层报「探测为空」。 */
|
|
28
|
-
export function dshRootOf(home = process.env.DSH_HOME || '') {
|
|
29
|
-
const h = String(home || '').trim();
|
|
30
|
-
if (!h) return '';
|
|
31
|
-
if (existsSync(join(h, '.dsh', 'profiles'))) return join(h, '.dsh');
|
|
32
|
-
if (existsSync(join(h, 'profiles'))) return h;
|
|
33
|
-
return join(h, '.dsh');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** profiles 目录。 */
|
|
37
|
-
export function profilesDirOf(home = process.env.DSH_HOME || '') {
|
|
38
|
-
const root = dshRootOf(home);
|
|
39
|
-
return root ? join(root, 'profiles') : '';
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** 列各 profile 及其中 local-plugins 的概况(供「多 profile 要显式指定」的提示)。 */
|
|
43
|
-
export function inspectProfiles(home = process.env.DSH_HOME || '') {
|
|
44
|
-
const dir = profilesDirOf(home);
|
|
45
|
-
const out = [];
|
|
46
|
-
if (!dir || !existsSync(dir)) return out;
|
|
47
|
-
for (const e of readdirSync(dir, { withFileTypes: true })) {
|
|
48
|
-
if (!e.isDirectory()) continue;
|
|
49
|
-
const pd = join(dir, e.name);
|
|
50
|
-
const localDir = join(pd, 'local-plugins');
|
|
51
|
-
let localPlugins = [];
|
|
52
|
-
try { localPlugins = readdirSync(localDir, { withFileTypes: true }).filter((x) => x.isDirectory() || x.isSymbolicLink()).map((x) => x.name); } catch { localPlugins = []; }
|
|
53
|
-
out.push({ name: e.name, dir: pd, localDir, localPlugins });
|
|
54
|
-
}
|
|
55
|
-
return out;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* 读 profile 的 hmr 配置(cordis.patch.yml 的 `- id: hmr` 块)。
|
|
60
|
-
* 文本扫描而非引 YAML 依赖:只需要 base / root 两项,且该文件由插件管理器生成、结构稳定。
|
|
61
|
-
*/
|
|
62
|
-
export function readHmrConfig(profileDir, home = process.env.DSH_HOME || '') {
|
|
63
|
-
const baseDir = profileDir || join(profilesDirOf(home), 'web');
|
|
64
|
-
const f = join(baseDir, 'cordis.patch.yml');
|
|
65
|
-
if (!existsSync(f)) return { found: false, profileDir: baseDir, base: '', root: [], reason: '该 profile 没有 cordis.patch.yml' };
|
|
66
|
-
let text = '';
|
|
67
|
-
try { text = readFileSync(f, 'utf8'); } catch { return { found: false, profileDir: baseDir, base: '', root: [], reason: 'cordis.patch.yml 读失败' }; }
|
|
68
|
-
const i = text.search(/^-\s*id:\s*hmr\s*$/m);
|
|
69
|
-
if (i < 0) {
|
|
70
|
-
return { found: false, profileDir: baseDir, base: '', root: [], reason: '未配置 id: hmr —— hmr 默认 root:[] 只重载 profile 配置,不 watch 任何模块目录' };
|
|
71
|
-
}
|
|
72
|
-
const rest = text.slice(i);
|
|
73
|
-
const nextIdx = rest.slice(1).search(/^-\s/m);
|
|
74
|
-
const block = nextIdx < 0 ? rest : rest.slice(0, nextIdx + 1);
|
|
75
|
-
const base = ((block.match(/^[ \t]*base:[ \t]*(\S+)[ \t]*$/m) || [])[1] || '');
|
|
76
|
-
// ⚠️ 这里不能用 \s*(\s 含换行,会把 root: 后的换行也吃掉,导致再要求 \n 时匹配失败)——只吃行内空白。
|
|
77
|
-
const rootBlock = ((block.match(/^[ \t]*root:[ \t]*\r?\n((?:[ \t]+-[ \t]*\S+[ \t]*\r?\n?)+)/m) || [])[1] || '');
|
|
78
|
-
const root = [...rootBlock.matchAll(/-[ \t]*(\S+)/g)].map((m) => m[1]);
|
|
79
|
-
return { found: true, profileDir: baseDir, base, root, reason: '' };
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* 判定「该插件改代码是否需要重启」。
|
|
84
|
-
* @param {{hmr: object, profileDir: string, pluginName: string}} p
|
|
85
|
-
* @returns {{hot: boolean, reason: string, dir?: string}}
|
|
86
|
-
*/
|
|
87
|
-
export function judgeHmr({ hmr, profileDir, pluginName }) {
|
|
88
|
-
if (!pluginName) return { hot: false, reason: '未给出插件名' };
|
|
89
|
-
if (!hmr || !hmr.found) return { hot: false, reason: (hmr && hmr.reason) || '未读到 hmr 配置(默认不 watch 模块目录)' };
|
|
90
|
-
const base = hmr.base || profileDir || '';
|
|
91
|
-
if (!hmr.root.length) {
|
|
92
|
-
return { hot: false, reason: 'hmr.root 为空 —— module roots are opt-in:配了 base 也必须显式列出要 watch 的目录' };
|
|
93
|
-
}
|
|
94
|
-
const roots = hmr.root.map((r) => resolve(base, r));
|
|
95
|
-
const localDir = join(base, 'local-plugins', pluginName);
|
|
96
|
-
const nmDir = join(base, 'node_modules', pluginName);
|
|
97
|
-
const inRoot = (p) => roots.some((r) => p === r || p.startsWith(r + sep));
|
|
98
|
-
// 判据必须**先确认真实存在**:只看路径前缀会把「名字恰好落在 root 下、其实没装」也判成可热重载(单测抓到过)。
|
|
99
|
-
if (existsSync(localDir) && inRoot(localDir)) {
|
|
100
|
-
return { hot: true, reason: `命中所配 root(${hmr.root.join(' / ')})且不经 node_modules ⇒ 改代码可免重启热重载`, dir: localDir };
|
|
101
|
-
}
|
|
102
|
-
if (existsSync(nmDir)) {
|
|
103
|
-
return { hot: false, reason: `插件只落在 node_modules(${nmDir})—— hmr 的默认 ignored 会整棵剪枝该目录(否定写法同样无效),需按目标形态挪到 local-plugins` };
|
|
104
|
-
}
|
|
105
|
-
if (!existsSync(localDir)) {
|
|
106
|
-
return { hot: false, reason: `未在 ${localDir} 找到该插件(看起来没装到该 profile)` };
|
|
107
|
-
}
|
|
108
|
-
return { hot: false, reason: `插件在 local-plugins,但该目录不在所配 root 覆盖范围内(root=${hmr.root.join(',')})` };
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/** lstat 包装:不存在返回 null(符号链接本身用 lstat 判断)。 */
|
|
112
|
-
function lstatOrNull(p) {
|
|
113
|
-
try { return lstatSync(p); } catch { return null; }
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/** 读文本,失败返回 null(用于副本漂移判定)。 */
|
|
117
|
-
function readTextOrNull(p) {
|
|
118
|
-
try { return readFileSync(p, 'utf8'); } catch { return null; }
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** 递归列目录内文件(相对路径;跳过 .git/node_modules/.trash 等非发布内容)。 */
|
|
122
|
-
function listFilesRecursive(dir, prefix = '', out = new Set()) {
|
|
123
|
-
try {
|
|
124
|
-
for (const e of readdirSync(join(dir, prefix), { withFileTypes: true })) {
|
|
125
|
-
const rel = prefix ? `${prefix}/${e.name}` : e.name;
|
|
126
|
-
if (['.git', 'node_modules', '.trash', '.DS_Store'].includes(e.name)) continue;
|
|
127
|
-
if (e.isDirectory()) listFilesRecursive(dir, rel, out);
|
|
128
|
-
else out.add(rel);
|
|
129
|
-
}
|
|
130
|
-
} catch { /* 读不到按空处理,由调用方据结果判断 */ }
|
|
131
|
-
return out;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* 判定 node_modules 那份副本是否与真源不一致(漂移)。**两类都要查**:
|
|
136
|
-
*
|
|
137
|
-
* ① **内容漂移**:已有文件内容与真源不同(旧副本顶掉真源 ⇒ 宿主跑旧代码)。
|
|
138
|
-
* 实测事故:GitHub tarball 旧版 2.6.9 顶掉 2.8.6 ⇒ 宿主自报 2.6.9、新工具整批消失。
|
|
139
|
-
* ② **文件清单漂移**:真源有、副本**缺** ✗ —— 硬链接孪生**不会自动带上新建文件**。
|
|
140
|
-
* 实测事故(2026-10-15 19:2x):新增 `lib/ast/treesitter.js` 只落 local-plugins,
|
|
141
|
-
* 而 node_modules 那份的 `lib/audit/orchestrate.js` 已经 import 它 ⇒ 副本 import 失败
|
|
142
|
-
* ⇒ 宿主日志 `dsh-git-push (dsh-git-push): failed to import`、**该插件全部工具消失** ✗✗。
|
|
143
|
-
* 本检查此前只比对内容 ⇒ 完全没发现这一类 ✗。
|
|
144
|
-
*
|
|
145
|
-
* 为什么不能按「是不是目录」报警:pnpm 的 `file:` 目录依赖落在 node_modules 时,
|
|
146
|
-
* 实测是**与 local-plugins 同 inode 的硬链接副本**(stat 同 inode、链接数 2),属正常安装产物 ✓。
|
|
147
|
-
*
|
|
148
|
-
* @param {string} localDir 真源(local-plugins/<插件>)
|
|
149
|
-
* @param {string} nmDir node_modules/<插件>
|
|
150
|
-
* @returns {string} '' = 一致(含硬链接同文件);否则是差异描述
|
|
151
|
-
*/
|
|
152
|
-
export function copyDrift(localDir, nmDir) {
|
|
153
|
-
// ② 文件清单漂移(先查:缺文件比内容不同更致命 ⇒ 直接 import 失败)。
|
|
154
|
-
// 只比**加载关键子集**:lib/ 与 assets/ 是 import 与运行必需的;
|
|
155
|
-
// scripts/ skills/ docs/ 等不参与加载,pnpm 对 file: 依赖的拷贝口径也可能不同 ⇒
|
|
156
|
-
// 全量比对会产生上百条噪音,把真信号(如缺 lib/ast/treesitter.js)淹掉 ✗
|
|
157
|
-
const localFiles = listFilesRecursive(localDir);
|
|
158
|
-
const nmFiles = listFilesRecursive(nmDir);
|
|
159
|
-
const CRITICAL = /^(lib\/|assets\/|package\.json$|cordis\.patch\.yml$)/;
|
|
160
|
-
if (localFiles.size && nmFiles.size) {
|
|
161
|
-
const missing = [...localFiles].filter((f) => CRITICAL.test(f) && !nmFiles.has(f));
|
|
162
|
-
if (missing.length) return `副本缺少 ${missing.length} 个加载必需文件(如 ${missing[0]})`;
|
|
163
|
-
}
|
|
164
|
-
// ① 内容漂移:判据取「版本三处 + 入口文件」这几个标记位,任一处不同即认为副本陈旧/被替换
|
|
165
|
-
for (const rel of ['package.json', 'lib/self/index.js', 'lib/app/command-registry.js']) {
|
|
166
|
-
const a = readTextOrNull(join(localDir, rel));
|
|
167
|
-
const b = readTextOrNull(join(nmDir, rel));
|
|
168
|
-
if (a === null) continue; // 真源里没有这个文件(插件结构变体)⇒ 跳过该标记
|
|
169
|
-
if (b === null) return `${rel} 在副本里缺失`;
|
|
170
|
-
if (a !== b) return `${rel} 内容不同`;
|
|
171
|
-
}
|
|
172
|
-
return '';
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* 目标形态与现状的差异清单。默认只报告,不执行。
|
|
177
|
-
*
|
|
178
|
-
* 【2026-10-15 起】真源**只认** `<profile>/local-plugins/<插件>`(真实目录):
|
|
179
|
-
* `node_modules/<插件>` 的解析入口**不再由本脚本管理**(不建链、不改链)——它由
|
|
180
|
-
* DSH/pnpm 安装流程按 profile `package.json` 的 `file:local-plugins/<插件>` 声明自动生成,
|
|
181
|
-
* 安装位置就是 local-plugins。
|
|
182
|
-
*
|
|
183
|
-
* 为什么仍要报告 node_modules:实测事故(本机 2026-10-15)——node_modules 下留了一份
|
|
184
|
-
* **真实目录副本**(旧版 2.6.9,来自更早的 tarball 安装),它顶替 local-plugins 里的 2.8.6
|
|
185
|
-
* 成为宿主实际加载源,宿主 `git_api status` 报 2.6.9、`npm_publish` 等新工具整批消失。
|
|
186
|
-
* 更硬的证据:删掉该条目后,从 profile 根 `require.resolve('dsh-git-push')` 直接 MODULE_NOT_FOUND
|
|
187
|
-
* ⇒ **没有入口 = 解析不到 = 插件不加载**;入口必须存在,但该由安装流程生成,而非本脚本伪造。
|
|
188
|
-
* @returns {Array<{kind: string, path: string, desc: string}>}
|
|
189
|
-
*/
|
|
190
|
-
export function planInstall({ profileDir, pluginName }) {
|
|
191
|
-
const actions = [];
|
|
192
|
-
if (!profileDir || !pluginName) return actions;
|
|
193
|
-
const local = join(profileDir, 'local-plugins', pluginName);
|
|
194
|
-
const nm = join(profileDir, 'node_modules', pluginName);
|
|
195
|
-
const ls = lstatOrNull(local);
|
|
196
|
-
if (ls && ls.isSymbolicLink()) {
|
|
197
|
-
actions.push({ kind: 'delink-local', path: local, desc: 'local-plugins 下是软链:真实加载源应是**真实目录**(执行时会先删链再落一份真实副本)' });
|
|
198
|
-
} else if (!ls) {
|
|
199
|
-
actions.push({ kind: 'create-local', path: local, desc: 'local-plugins 下缺该插件:需先同步一份真实目录(真实加载源在这里)' });
|
|
200
|
-
}
|
|
201
|
-
const ns = lstatOrNull(nm);
|
|
202
|
-
if (ns && !ns.isSymbolicLink()) {
|
|
203
|
-
// 真实目录在 pnpm `file:` 目录依赖下是**正常产物**(实测与真源同 inode 的硬链接)⇒ 不能只看形态报警,
|
|
204
|
-
// 要比**内容**:只有副本与真源不一致(陈旧/被替换)才会顶掉真源、让宿主跑旧代码 ✓
|
|
205
|
-
const drift = copyDrift(local, nm);
|
|
206
|
-
if (drift) {
|
|
207
|
-
actions.push({ kind: 'nm-drift', path: nm, desc: `node_modules 下的副本与真源**内容不一致**(${drift}):它会顶掉 local-plugins 成为实际加载源(实测旧副本 2.6.9 顶掉 2.8.6、新工具整批消失)⇒ 应重装该依赖或删除此副本` });
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
return actions;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* 执行安装形态动作(仅在调用方传入 on=true 时写盘)。
|
|
215
|
-
* delink-local:删软链(真实内容由后续同步步骤落进来)
|
|
216
|
-
* create-local:建目录(同上)
|
|
217
|
-
* nm-real-dir:只报告(删副本属破坏性动作,交人工或安装流程;本工具不动既有数据)
|
|
218
|
-
*
|
|
219
|
-
* 注:本工具**不建 node_modules 软链**(真源只认 local-plugins;解析入口由 DSH/pnpm 安装流程生成)。
|
|
220
|
-
* @returns {{done: string[], skipped: string[]}}
|
|
221
|
-
*/
|
|
222
|
-
export function applyInstall(actions, { on = false } = {}) {
|
|
223
|
-
const done = [];
|
|
224
|
-
const skipped = [];
|
|
225
|
-
for (const a of actions) {
|
|
226
|
-
// 只有「改 local-plugins 真实目录」是他的活;node_modules 相关一律只报告(不建链、不删数据)
|
|
227
|
-
if (!on || (a.kind !== 'delink-local' && a.kind !== 'create-local')) { skipped.push(a.kind); continue; }
|
|
228
|
-
try {
|
|
229
|
-
if (a.kind === 'delink-local') {
|
|
230
|
-
unlinkSync(a.path); // 只删软链本身,不触及它指向的任何内容
|
|
231
|
-
mkdirSync(a.path, { recursive: true });
|
|
232
|
-
} else if (a.kind === 'create-local') {
|
|
233
|
-
mkdirSync(a.path, { recursive: true });
|
|
234
|
-
}
|
|
235
|
-
done.push(a.kind);
|
|
236
|
-
} catch (e) {
|
|
237
|
-
skipped.push(`${a.kind}(${String(e && e.message).slice(0, 60)})`);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
return { done, skipped };
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* 依赖声明提示:profile 的 package.json 里该插件若写成 `link:<绝对路径>` 或 `file:node_modules/...`,
|
|
245
|
-
* 应改为 `file:local-plugins/<插件>`(否则 profile 解析到的不是真实加载源)。
|
|
246
|
-
* @returns {string[]} 提示行(空数组 = 无需改)
|
|
247
|
-
*/
|
|
248
|
-
export function depHints(profileDir, pluginName) {
|
|
249
|
-
const out = [];
|
|
250
|
-
if (!profileDir || !pluginName) return out;
|
|
251
|
-
const f = join(profileDir, 'package.json');
|
|
252
|
-
if (!existsSync(f)) return out;
|
|
253
|
-
let deps = {};
|
|
254
|
-
try { deps = { ...(JSON.parse(readFileSync(f, 'utf8')).dependencies || {}) }; } catch { return out; }
|
|
255
|
-
const val = deps[pluginName];
|
|
256
|
-
if (typeof val !== 'string') return out;
|
|
257
|
-
const want = `file:local-plugins/${pluginName}`;
|
|
258
|
-
if (val !== want) {
|
|
259
|
-
out.push(`依赖声明应改为 "${pluginName}": "${want}"(当前 "${val}")`);
|
|
260
|
-
}
|
|
261
|
-
return out;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* 从多 profile 里选出「该插件的正确部署 profile」。
|
|
266
|
-
*
|
|
267
|
-
* 为什么需要:自动探测原先取首个命中,而本机多个 profile 都可能残留同名副本(旧副本/测试 profile),
|
|
268
|
-
* 选中错的 profile 会同步到错的地方、并把「未配 hmr」等结论也判错(dry-run 实测踩到过)。
|
|
269
|
-
* 判据按可靠性排序:① 插件在 local-plugins(真实加载源)② 只在 node_modules(历史 link 产物)。
|
|
270
|
-
*
|
|
271
|
-
* @returns {{dir: string, profile?: string, reason: string, ambiguous?: boolean, candidates?: string[]}}
|
|
272
|
-
*/
|
|
273
|
-
export function pickProfile(profiles = [], pluginName = '') {
|
|
274
|
-
if (!pluginName) return { dir: '', reason: '未给出插件名' };
|
|
275
|
-
const withLocal = profiles.filter((p) => existsSync(join(p.localDir, pluginName)));
|
|
276
|
-
if (withLocal.length === 1) return { dir: withLocal[0].dir, profile: withLocal[0].name, reason: '命中 local-plugins(真实加载源)' };
|
|
277
|
-
if (withLocal.length > 1) {
|
|
278
|
-
return { dir: '', reason: '多个 profile 的 local-plugins 下都有该插件,请用 --profile 指定', ambiguous: true, candidates: withLocal.map((p) => p.name) };
|
|
279
|
-
}
|
|
280
|
-
const withNm = profiles.filter((p) => existsSync(join(p.dir, 'node_modules', pluginName)));
|
|
281
|
-
if (withNm.length === 1) return { dir: withNm[0].dir, profile: withNm[0].name, reason: '只在 node_modules 命中(历史 link 产物,建议按目标形态迁到 local-plugins)' };
|
|
282
|
-
if (withNm.length > 1) {
|
|
283
|
-
return { dir: '', reason: '多个 profile 的 node_modules 下都有该插件,请用 --profile 指定', ambiguous: true, candidates: withNm.map((p) => p.name) };
|
|
284
|
-
}
|
|
285
|
-
return { dir: '', reason: `没有任何 profile 装了这个插件(共 ${profiles.length} 个 profile)` };
|
|
286
|
-
}
|
package/scripts/sync-plugin.mjs
DELETED
|
@@ -1,284 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 双副本同步脚本(1.0.0)
|
|
3
|
-
*
|
|
4
|
-
* 源 = 本仓库(脚本所在插件仓库根)
|
|
5
|
-
* 目标 = DSH 插件的**真实安装位置**:`.dsh/profiles/<profile>/local-plugins/<插件名>`
|
|
6
|
-
* (`node_modules/<插件名>` 只是**解析入口**,由 DSH/pnpm 安装流程按 profile
|
|
7
|
-
* `package.json` 的 `file:local-plugins/<插件>` 声明生成 —— 本脚本不建链、不改链)
|
|
8
|
-
*
|
|
9
|
-
* 默认 **dryRun**(只打印将要同步的差异,不写入);`--write` 才真同步。
|
|
10
|
-
* 同步内容:lib/** skills/** scripts/** cli.mjs package.json cordis.patch.yml README.md
|
|
11
|
-
* 排除:.git node_modules docs/WORKBOARD*(开发看板不随插件发布)test/**
|
|
12
|
-
*
|
|
13
|
-
* 用法:
|
|
14
|
-
* node scripts/sync-plugin.mjs # dry-run(默认)
|
|
15
|
-
* node scripts/sync-plugin.mjs --write # 真同步
|
|
16
|
-
* node scripts/sync-plugin.mjs --target <目录> # 指定目标(默认自动探测)
|
|
17
|
-
* node scripts/sync-plugin.mjs --source <目录> # 指定源插件仓库(默认本仓库);
|
|
18
|
-
* # 目标未显式给时按源目录名自动探测双副本
|
|
19
|
-
*/
|
|
20
|
-
import { existsSync, readdirSync, statSync, readFileSync } from 'node:fs';
|
|
21
|
-
import { mkdir, readFile, chmod } from 'node:fs/promises';
|
|
22
|
-
// 目标目录常在工作区(CIFS 网络挂载):copyFileSync 在 CIFS 内会 EPERM
|
|
23
|
-
// (尝试 SMB 服务端复制),必须用带读写回退的 copyFileCompat。
|
|
24
|
-
import { copyFileCompat } from '../lib/fsx.js';
|
|
25
|
-
import { readdir, stat, access } from 'node:fs/promises';
|
|
26
|
-
import { join, relative, dirname, basename } from 'node:path';
|
|
27
|
-
import { fileURLToPath } from 'node:url';
|
|
28
|
-
// 安装形态与免重启热重载自检(2026-10-09 拆出,见 scripts/sync-plugin-install.mjs 顶部说明)
|
|
29
|
-
import { dshRootOf, profilesDirOf, inspectProfiles, readHmrConfig, judgeHmr, planInstall, applyInstall, depHints, pickProfile } from './sync-plugin-install.mjs';
|
|
30
|
-
|
|
31
|
-
export const SOURCE_ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
|
|
32
|
-
|
|
33
|
-
/** 随插件发布的顶层条目(通用候选:按源目录存在性过滤,兼容任意 DSH 插件)。
|
|
34
|
-
* 候选含 lib/skills/scripts/assets/tools/cli.mjs/package.json/cordis.patch.yml/
|
|
35
|
-
* README.md/.auditignore/.gitignore——源里没有的条目自动跳过。 */
|
|
36
|
-
export const SYNC_ENTRIES = ['lib', 'skills', 'scripts', 'assets', 'tools', 'cli.mjs', 'package.json', 'cordis.patch.yml', 'README.md', 'LICENSE', '.auditignore', '.gitignore'];
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* npm 打包的**硬性排除项**(npm-packlist 默认忽略集,与项目自定义无关)。
|
|
40
|
-
*
|
|
41
|
-
* 【设计】同步清单的唯一权威 = 源项目 package.json 的 `files` 字段(npm 发布语义):
|
|
42
|
-
* 「要发布什么,就写进 files」;同步工具**不维护任何自定义排除表**。
|
|
43
|
-
*
|
|
44
|
-
* 为什么删掉旧的自定义排除(2026-10-07 事故):旧实现在 `files` 白名单之外另有一套
|
|
45
|
-
* 子串排除 `['.git','node_modules','WORKBOARD','test','.tmp','.bak','.trash']`,
|
|
46
|
-
* 判定写成 `rel.split('/').includes(x) || rel.includes(x)`——`rel.includes('test')`
|
|
47
|
-
* 把随插件发布的正式模块 `lib/shared/test-hooks.js` 当成 test 目录排掉了。后果:
|
|
48
|
-
* 同步报「成功」、安装副本却少一个文件,两个版本之间 lib/ 永远对不齐(只有逐文件
|
|
49
|
-
* diff 副本才发现,宿主运行看不出)。教训:**发布内容只由 package.json 决定**,
|
|
50
|
-
* `files` 白名单与自定义排除表是两套真相,必然分叉——排除只保留 npm 自己的硬规则。
|
|
51
|
-
*/
|
|
52
|
-
export const NPM_ALWAYS_IGNORE_SEGMENTS = ['.git', 'node_modules', '.DS_Store', '.npmrc', 'package-lock.json'];
|
|
53
|
-
/** npm 硬性忽略的后缀型条目(编辑器交换/备份文件)。 */
|
|
54
|
-
export const NPM_ALWAYS_IGNORE_SUFFIX = ['.orig', '.swp'];
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 该相对路径是否命中 npm 硬性排除项(目录遍历与单测共用同一判定,避免规则再次分叉)。
|
|
58
|
-
* @param {string} rel 相对插件根的路径(POSIX 分隔符)
|
|
59
|
-
* @returns {boolean} true = npm 不会打包它,同步也不复制
|
|
60
|
-
*/
|
|
61
|
-
export function isNpmIgnored(rel) {
|
|
62
|
-
const segments = String(rel).split('/');
|
|
63
|
-
return NPM_ALWAYS_IGNORE_SEGMENTS.some((x) => segments.includes(x))
|
|
64
|
-
|| NPM_ALWAYS_IGNORE_SUFFIX.some((x) => String(rel).endsWith(x));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** 异步探测路径是否存在(node:fs/promises 不提供 exists)。 */
|
|
68
|
-
async function exists(p) {
|
|
69
|
-
try { await access(p); return true; } catch { return false; }
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* 随插件发布的顶层条目 = 源项目 package.json 的 `files` 白名单(每个插件自行声明
|
|
74
|
-
* 发布内容,与 npm 打包语义一致);另强制包含 package.json 本身(部署副本加载必需)。
|
|
75
|
-
* files 缺失或读取失败时兜底保守最小集 ['lib', 'cordis.patch.yml']。
|
|
76
|
-
* @param {string} root 源仓库根
|
|
77
|
-
* @returns {string[]} 应同步的顶层条目
|
|
78
|
-
*/
|
|
79
|
-
export async function packageFilesOf(root) {
|
|
80
|
-
let files = [];
|
|
81
|
-
try {
|
|
82
|
-
const pkg = JSON.parse(await readFile(join(root, 'package.json'), 'utf8'));
|
|
83
|
-
if (Array.isArray(pkg.files) && pkg.files.length > 0) files = pkg.files;
|
|
84
|
-
} catch {
|
|
85
|
-
// 读失败 → 走兜底最小集
|
|
86
|
-
}
|
|
87
|
-
if (files.length === 0) {
|
|
88
|
-
// npm 语义:未声明 `files` = 发布「除默认忽略项以外的一切」;安装副本**不照做**
|
|
89
|
-
// (那会把 test/、docs/、开发归档一起灌进宿主副本),退化为候选白名单并提示补声明。
|
|
90
|
-
console.warn('[sync-plugin] ⚠️ package.json 未声明 files —— 按保守白名单同步;建议按 npm 规范补上 files 字段');
|
|
91
|
-
files = SYNC_ENTRIES;
|
|
92
|
-
}
|
|
93
|
-
// package.json 永远随包(npm 语义:始终包含)——部署副本靠它识别插件与版本。
|
|
94
|
-
return Array.from(new Set([...files, 'package.json']));
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* 递归列出源目录下应同步的文件(相对路径)。
|
|
99
|
-
* @param {string} root 源根
|
|
100
|
-
* @returns {string[]} 相对路径列表
|
|
101
|
-
*/
|
|
102
|
-
export async function listSyncFiles(root = SOURCE_ROOT) {
|
|
103
|
-
const out = [];
|
|
104
|
-
// 递归遍历目录:改异步以免在大目录上逐项阻塞事件循环
|
|
105
|
-
// (readdir 带 withFileTypes,目录项类型由一次调用带回,无需再逐个 stat)
|
|
106
|
-
const walk = async (dir) => {
|
|
107
|
-
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
108
|
-
const full = join(dir, entry.name);
|
|
109
|
-
const rel = relative(root, full);
|
|
110
|
-
if (isNpmIgnored(rel)) continue;
|
|
111
|
-
if (entry.isDirectory()) await walk(full);
|
|
112
|
-
else out.push(rel);
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
for (const entryName of await packageFilesOf(root)) {
|
|
116
|
-
const full = join(root, entryName);
|
|
117
|
-
if (!(await exists(full))) continue;
|
|
118
|
-
if ((await stat(full)).isDirectory()) await walk(full);
|
|
119
|
-
else out.push(entryName);
|
|
120
|
-
}
|
|
121
|
-
return out.sort();
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* 探测 DSH 插件的部署位置。
|
|
126
|
-
*
|
|
127
|
-
* **真实安装位置 = `<profile>/local-plugins/<插件名>`**(profile 的 package.json 写的是
|
|
128
|
-
* `"<插件名>": "file:local-plugins/<插件名>"`)—— 同步目标就是它。
|
|
129
|
-
*
|
|
130
|
-
* `<profile>/node_modules/<插件名>` 只作**解析入口**探测(部分解析路径经此;实测删掉条目后
|
|
131
|
-
* 从 profile 根 require.resolve 直接 MODULE_NOT_FOUND ⇒ 入口缺失 = 插件加载不到):
|
|
132
|
-
* 该入口由 DSH/pnpm 安装流程生成与维护,**本脚本不再创建/修补它**。
|
|
133
|
-
*
|
|
134
|
-
* 历史教训(2026-10-15):node_modules 下残留真实目录副本(旧版)会**顶替** local-plugins
|
|
135
|
-
* 成为实际加载源,宿主跑旧代码、新工具整批消失 —— 故 planInstall 会对这种形态报警。
|
|
136
|
-
* @param {string} [home] DSH_HOME(默认从环境变量推断)
|
|
137
|
-
* @param {string} [pluginName] 插件名
|
|
138
|
-
* @returns {string[]} 命中的目标目录(local-plugins 在前)
|
|
139
|
-
*/
|
|
140
|
-
export function detectTargets(home = process.env.DSH_HOME || '', pluginName = '', profileName = '') {
|
|
141
|
-
const hits = [];
|
|
142
|
-
if (!home || !pluginName) return hits;
|
|
143
|
-
// 2026-10-09 入参归一:DSH 自己的 DSH_HOME **含** `.dsh`,而本函数原先一律按「不含 .dsh」拼接
|
|
144
|
-
// ⇒ 直接传 $DSH_HOME 会拼成 `…/.dsh/.dsh/profiles`、探测为空(开发者文档 §5.1 记录的坑)。
|
|
145
|
-
// 现按实存目录判定两种形态;多 profile 时可用 profileName 只看指定 profile(§5.2:自动取首个会选错)。
|
|
146
|
-
const profiles = profilesDirOf(home);
|
|
147
|
-
if (!profiles || !existsSync(profiles)) return hits;
|
|
148
|
-
for (const profile of readdirSync(profiles, { withFileTypes: true })) {
|
|
149
|
-
if (!profile.isDirectory()) continue;
|
|
150
|
-
if (profileName && profile.name !== profileName) continue;
|
|
151
|
-
const base = join(profiles, profile.name);
|
|
152
|
-
// 真实加载源优先(package.json 的 file: 指向这里)
|
|
153
|
-
const local = join(base, 'local-plugins', pluginName);
|
|
154
|
-
if (existsSync(local)) hits.push(local);
|
|
155
|
-
// npm link 产物(保留:部分解析路径走这里)
|
|
156
|
-
const nm = join(base, 'node_modules', pluginName);
|
|
157
|
-
if (existsSync(nm)) hits.push(nm);
|
|
158
|
-
}
|
|
159
|
-
return hits;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* 同步(dry-run 或真写入)。
|
|
164
|
-
* @param {object} p { source, target, write }
|
|
165
|
-
* @returns {{ok: boolean, written: number, skipped: number, files: string[], error?: string}}
|
|
166
|
-
*/
|
|
167
|
-
/**
|
|
168
|
-
* 两文件内容是否相同;目标不存在返回 false。
|
|
169
|
-
* 读失败(权限/编码)按「不相同」处理 —— 交给后续复制去覆盖并上报真实错误,
|
|
170
|
-
* 避免在这里吞掉问题。
|
|
171
|
-
*/
|
|
172
|
-
async function fileContentEqual(from, to) {
|
|
173
|
-
try {
|
|
174
|
-
// 异步函数内同步 existsSync 改 fs.promises.access(消除阻塞事件循环)
|
|
175
|
-
try { await access(to); } catch { return false; }
|
|
176
|
-
const [a, b] = await Promise.all([readFile(from, 'utf8'), readFile(to, 'utf8')]);
|
|
177
|
-
return a === b;
|
|
178
|
-
} catch {
|
|
179
|
-
return false;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
export async function syncPlugin({ source = SOURCE_ROOT, target = '', write = false } = {}) {
|
|
184
|
-
if (!target) return { ok: false, written: 0, skipped: 0, files: [], error: '未指定目标目录(用 --target 或配置 DSH_HOME)' };
|
|
185
|
-
const files = await listSyncFiles(source);
|
|
186
|
-
let written = 0;
|
|
187
|
-
let skipped = 0;
|
|
188
|
-
const failures = [];
|
|
189
|
-
for (const rel of files) {
|
|
190
|
-
const from = join(source, rel);
|
|
191
|
-
const to = join(target, rel);
|
|
192
|
-
// 内容不同才写(幂等)
|
|
193
|
-
// 逐文件比较内容(幂等):改 await 后单次遍历不再阻塞事件循环
|
|
194
|
-
const same = await fileContentEqual(from, to);
|
|
195
|
-
if (same) { skipped++; continue; }
|
|
196
|
-
if (write) {
|
|
197
|
-
await mkdir(dirname(to), { recursive: true });
|
|
198
|
-
const r = copyFileCompat(from, to);
|
|
199
|
-
if (!r.ok) {
|
|
200
|
-
// 单个文件失败不再抛出中断整个同步:记录后继续,最后统一上报。
|
|
201
|
-
failures.push({ file: rel, error: r.error });
|
|
202
|
-
continue;
|
|
203
|
-
}
|
|
204
|
-
// cli.mjs 是 bin(git-sluice)入口,需可执行权限(shebang 执行)。
|
|
205
|
-
// copyFile 不带原文件 mode,同步后恢复 +x(幂等 chmod;失败不阻塞)。
|
|
206
|
-
if (rel === 'cli.mjs' || rel.endsWith('/cli.mjs')) {
|
|
207
|
-
try { await chmod(to, 0o755); } catch { /* 权限恢复失败不强拦 */ }
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
written++;
|
|
211
|
-
}
|
|
212
|
-
return { ok: failures.length === 0, written, skipped, files, failures };
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/** CLI 入口。支持 --source <目录>(缺省=本仓库)、--target <目录>(缺省=自动探测)、
|
|
216
|
-
* --profile <名>(多 profile 时显式指定)、--write(真写)、--fix(按目标形态修正安装位置,含 --write 语义)。 */
|
|
217
|
-
export async function main(argv = process.argv.slice(2)) {
|
|
218
|
-
const fix = argv.includes('--fix');
|
|
219
|
-
const write = argv.includes('--write') || fix;
|
|
220
|
-
const si = argv.indexOf('--source');
|
|
221
|
-
const source = si >= 0 ? argv[si + 1] : SOURCE_ROOT;
|
|
222
|
-
const ti = argv.indexOf('--target');
|
|
223
|
-
const pi = argv.indexOf('--profile');
|
|
224
|
-
const profileName = pi >= 0 ? argv[pi + 1] : '';
|
|
225
|
-
const home = process.env.DSH_HOME || '';
|
|
226
|
-
const pluginName = basename(source);
|
|
227
|
-
// 2026-10-09:按 profile 探测(多 profile 时自动取首个会选错——开发者文档 §5.2 已记录该坑)
|
|
228
|
-
const profiles = inspectProfiles(home);
|
|
229
|
-
const picked = profileName
|
|
230
|
-
? { dir: (profiles.find((p) => p.name === profileName) || {}).dir || '', profile: profileName, reason: '按 --profile 指定' }
|
|
231
|
-
: pickProfile(profiles, pluginName);
|
|
232
|
-
if (!ti && picked.ambiguous) {
|
|
233
|
-
console.log(`"${pluginName}" 在多个 profile 都有副本:${(picked.candidates || []).join(' / ')}`);
|
|
234
|
-
console.log('请显式指定:--profile <名>(或 --target <目录>)');
|
|
235
|
-
return 1;
|
|
236
|
-
}
|
|
237
|
-
const hits = detectTargets(home, pluginName, profileName || picked.profile || '');
|
|
238
|
-
const target = ti >= 0 ? argv[ti + 1] : (hits[0] || '');
|
|
239
|
-
const pd = picked.dir || (profiles.find((p) => p.dir === dirname(dirname(target))) || {}).dir || '';
|
|
240
|
-
if (!ti && !target) {
|
|
241
|
-
console.log(`未探测到 "${pluginName}" 的部署副本:${picked.reason}`);
|
|
242
|
-
if (profiles.length) console.log(` 共 ${profiles.length} 个 profile:${profiles.map((p) => p.name).join(' / ')}`);
|
|
243
|
-
console.log('请显式指定:--profile <名>(或 --target <目录>)');
|
|
244
|
-
return 1;
|
|
245
|
-
}
|
|
246
|
-
const r = await syncPlugin({ source, target, write });
|
|
247
|
-
if (!r.ok) {
|
|
248
|
-
console.log(`同步未执行:${r.error}`);
|
|
249
|
-
console.log(`可用目标(自动探测):${hits.join(' | ') || '(无)'}`);
|
|
250
|
-
return 1;
|
|
251
|
-
}
|
|
252
|
-
console.log(`双副本同步${write ? '' : '(dry-run,加 --write 才写)'}`);
|
|
253
|
-
console.log(` 源:${source}`);
|
|
254
|
-
console.log(` 目标:${target}`);
|
|
255
|
-
console.log(` 待写 ${r.written} 个文件,已一致 ${r.skipped} 个`);
|
|
256
|
-
for (const f of r.files.slice(0, 10)) console.log(` - ${f}`);
|
|
257
|
-
if (r.files.length > 10) console.log(` … 共 ${r.files.length} 个`);
|
|
258
|
-
// ── 安装形态 + 免重启热重载自检 + 依赖声明(2026-10-09:把开发者文档 §3.1/§3.2/§5 的检查内置)──
|
|
259
|
-
if (pd) {
|
|
260
|
-
const hmr = readHmrConfig(pd);
|
|
261
|
-
const verdict = judgeHmr({ hmr, profileDir: pd, pluginName });
|
|
262
|
-
console.log(` profile:${pd}`);
|
|
263
|
-
console.log(` 免重启热重载:${verdict.hot ? '✅ 可(改代码后同步即生效)' : `❌ 不可 —— ${verdict.reason}`}`);
|
|
264
|
-
const actions = planInstall({ profileDir: pd, pluginName });
|
|
265
|
-
if (actions.length) {
|
|
266
|
-
console.log(' 安装形态待办(真源 = local-plugins 真实目录;node_modules 入口由 DSH 安装流程生成,本脚本不建链):');
|
|
267
|
-
for (const a of actions) console.log(` · [${a.kind}] ${a.desc}`);
|
|
268
|
-
if (fix) {
|
|
269
|
-
const res = applyInstall(actions, { on: true });
|
|
270
|
-
console.log(` 已执行:${res.done.join(', ') || '(无)'}${res.skipped.length ? `|跳过:${res.skipped.join(', ')}` : ''}`);
|
|
271
|
-
} else {
|
|
272
|
-
console.log(' (加 --fix 才执行:解 local-plugins 软链 / 建真实目录;node_modules 相关只报告不动盘)');
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
for (const h of depHints(pd, pluginName)) console.log(` ⚠️ ${h}`);
|
|
276
|
-
}
|
|
277
|
-
return 0;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
// 顶层 await:main 已异步,未 await 的话 rejection 会成为 unhandled rejection
|
|
281
|
-
// (进程静默退出,退出码不对),故显式 await 并回传退出码。
|
|
282
|
-
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
|
|
283
|
-
process.exitCode = await main();
|
|
284
|
-
}
|