dsh-plugin-mesh 0.3.33 → 0.3.35
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/AGENTS.md +23 -1
- package/README.md +23 -0
- package/lib/_selftest-capabilities.mjs +46 -3
- package/lib/capabilities.js +78 -25
- package/lib/index.js +2 -2
- package/package.json +6 -3
- package/scripts/_selftest-release-plan.mjs +253 -0
- package/scripts/pack-release-tarball.sh +47 -0
- package/scripts/release-plan.mjs +412 -0
- package/scripts/release-plugin.sh +8 -14
package/AGENTS.md
CHANGED
|
@@ -130,6 +130,20 @@ private multicast group so it cannot pollute a real LAN.
|
|
|
130
130
|
|
|
131
131
|
## 6. Releasing
|
|
132
132
|
|
|
133
|
+
**Normal path: merge to `main` and stop touching it.** `.github/workflows/release.yml`
|
|
134
|
+
tests, decides the version from the commit message, bumps `package.json` +
|
|
135
|
+
`lib/index.js`, publishes to npm with provenance, tags `plugin-v<version>`, and
|
|
136
|
+
creates the GitHub release with the source tarball. Docs-only commits
|
|
137
|
+
(`docs:`/`chore:`/`ci:`/`test:`/`style:`/`build:`) deliberately do **not** release.
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
npm run release:plan # what would the pipeline do? (no network, no writes)
|
|
141
|
+
npm run release:plan -- --since HEAD~5 --npm-latest 0.3.33
|
|
142
|
+
npm run release:plan -- --bump patch --apply # bump locally instead of by hand
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
If you must release from this machine (offline, or the pipeline is broken):
|
|
146
|
+
|
|
133
147
|
```bash
|
|
134
148
|
npm run check && npm test
|
|
135
149
|
npm run release-plugin -- --publish # tarball -> GitHub release -> LAN mirror
|
|
@@ -141,11 +155,19 @@ If the dsh host is on this machine, `scripts/restart-dsh.sh --install [delay]`
|
|
|
141
155
|
restarts it from launchd so the restart survives killing the app.
|
|
142
156
|
|
|
143
157
|
Notes:
|
|
158
|
+
- **Never hand-edit the version.** Two places must agree (`package.json` and the
|
|
159
|
+
`VERSION` constant in `lib/index.js`); `scripts/release-plan.mjs --apply` writes
|
|
160
|
+
both, and `check-version.mjs` rejects a mismatch.
|
|
161
|
+
- The bump rule lives in `scripts/release-plan.mjs` and is unit-tested
|
|
162
|
+
(`scripts/_selftest-release-plan.mjs`). Change the rule *and* the test together.
|
|
144
163
|
- `scripts/check-tarball.mjs` validates the **published artifact**, not the source
|
|
145
164
|
tree, because `files` in `package.json` decides what users actually get.
|
|
146
165
|
- npm's "staged publishing" makes a new version appear in metadata before its
|
|
147
|
-
tarball is downloadable. Do not declare success until the tarball returns 200
|
|
166
|
+
tarball is downloadable. Do not declare success until the tarball returns 200 —
|
|
167
|
+
the workflow already polls for this; if you publish by hand, poll manually.
|
|
148
168
|
- Keep `llms.txt` and `docs/llms.txt` identical: `npm run check` verifies it.
|
|
169
|
+
- Anything non-obvious about the pipeline (state machine, retries, rollback,
|
|
170
|
+
branch-protection caveat) belongs in `docs/RELEASE.md`, not in this file.
|
|
149
171
|
|
|
150
172
|
## 7. Things that will waste your time if you do not know them
|
|
151
173
|
|
package/README.md
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
> 🤖 **给 agent / 英文读者**:[`llms.txt`](llms.txt)(机器可读的事实:工具、配置项与默认值、端口)
|
|
7
7
|
> · [`AGENTS.md`](AGENTS.md)(改这个仓库前的硬约束与坑)· [`README.en.md`](README.en.md)(英文快速上手)· [`DESIGN.md`](DESIGN.md)(设计原理与复盘)
|
|
8
8
|
>
|
|
9
|
+
> 🛠 **想参与开发**:[`CONTRIBUTING.md`](CONTRIBUTING.md)(提交规范、会被挡下的写法)—— 合并进 `main` 就会自动测试、发 npm 包和 GitHub Release,你不需要碰版本号。
|
|
10
|
+
>
|
|
9
11
|
> 本 README 是完整中文文档,含设计取舍与踩坑记录,比较长。
|
|
10
12
|
|
|
11
13
|
DSH 的 Mesh 插件:**启动后自动发现同一网段的其他 DSH 实例**,在侧栏提供一个
|
|
@@ -842,6 +844,27 @@ node lib/_selftest-discovery.mjs alpha 45900 9000 # 两个进程同时跑可
|
|
|
842
844
|
```
|
|
843
845
|
|
|
844
846
|
|
|
847
|
+
## 参与开发与发版
|
|
848
|
+
|
|
849
|
+
**合并进 `main` 就是发版**:CI 会跑全量测试、按提交信息决定版本号(`feat:` → minor,
|
|
850
|
+
`fix:` → patch,`docs:`/`chore:` 不发版)、发 npm 包(带 provenance 签名)、打
|
|
851
|
+
`plugin-v<版本>` tag,并建带源码包的 GitHub Release。贡献者不需要手改版本号、打 tag
|
|
852
|
+
或跑 `npm publish` —— 手改反而会和流水线打架(`npm run check` 会在两处版本号不一致时
|
|
853
|
+
直接拦下)。
|
|
854
|
+
|
|
855
|
+
| 想知道 | 看 |
|
|
856
|
+
|---|---|
|
|
857
|
+
| 提交信息怎么写、PR 会被哪些门禁检查 | [`CONTRIBUTING.md`](CONTRIBUTING.md) |
|
|
858
|
+
| 流水线怎么运作、失败了怎么重跑、怎么撤回 | [`docs/RELEASE.md`](docs/RELEASE.md) |
|
|
859
|
+
| 改代码前必须知道的硬约束与坑 | [`AGENTS.md`](AGENTS.md) |
|
|
860
|
+
|
|
861
|
+
本地预演"如果现在合并会发生什么"(不联网、不改文件):
|
|
862
|
+
|
|
863
|
+
```bash
|
|
864
|
+
npm run release:plan
|
|
865
|
+
```
|
|
866
|
+
|
|
867
|
+
|
|
845
868
|
## 文件结构
|
|
846
869
|
|
|
847
870
|
```
|
|
@@ -30,6 +30,8 @@ import {
|
|
|
30
30
|
__isEmptyButFailed,
|
|
31
31
|
__pathLookupUnreliable,
|
|
32
32
|
__extraPathDirs,
|
|
33
|
+
__normPathForCompare,
|
|
34
|
+
__probeGpu,
|
|
33
35
|
__storeAliasDir,
|
|
34
36
|
__hasUncertainProbe,
|
|
35
37
|
__probeFailCount,
|
|
@@ -159,13 +161,27 @@ test("骗过 sanitize 的假型号:确认净化层确实不拦(所以必须
|
|
|
159
161
|
assert.equal(seen.gpu.name, "ERROR: nvidia-smi failed", "净化层只看名字非空 → 上游不挡就会一路报出去");
|
|
160
162
|
});
|
|
161
163
|
|
|
162
|
-
await testAsync("
|
|
164
|
+
await testAsync("真实采集:有显卡的机器必须报出像样的 GPU 名字(无显卡的虚拟机要能说清)", async () => {
|
|
163
165
|
const caps = await collectCapabilities();
|
|
164
166
|
if (process.platform !== "darwin" && process.platform !== "win32") {
|
|
165
167
|
console.log(" 跳过断言(本平台不一定有 GPU)");
|
|
166
168
|
return;
|
|
167
169
|
}
|
|
168
|
-
|
|
170
|
+
if (!caps.gpu) {
|
|
171
|
+
// ⚠️ 探测输出里没有 Chipset Model 时,`null` 是**正确**结论(headless Mac /
|
|
172
|
+
// 虚拟机的 system_profiler 只有 Display 段)。GitHub 的 macOS runner 就是这种
|
|
173
|
+
// 机器 —— 原来这里无条件断言"必须有 GPU",于是 runner 上永远红、本机永远绿,
|
|
174
|
+
// 而红的原因跟代码质量毫无关系。
|
|
175
|
+
//
|
|
176
|
+
// 但也不能一律放过:**输出里有型号字段却解析成 null** 就是解析器回归,必须红。
|
|
177
|
+
const { output } = await __probeGpu();
|
|
178
|
+
assert.ok(
|
|
179
|
+
!/Chipset Model:/i.test(output),
|
|
180
|
+
`探测输出里有 Chipset Model,却解析不出型号(解析器回归):${String(output).slice(0, 300)}`,
|
|
181
|
+
);
|
|
182
|
+
console.log(" 本机探测输出里没有显卡型号(虚拟机/无显示设备)→ 这是「确实没有」,不是缺陷");
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
169
185
|
assert.ok(caps.gpu.name, "GPU 名字不该为空");
|
|
170
186
|
assert.ok(
|
|
171
187
|
!caps.gpu.name.includes("[object"),
|
|
@@ -287,8 +303,35 @@ test("原 PATH 的每一项都还在(补全不能把已有路径挤掉)", ()
|
|
|
287
303
|
});
|
|
288
304
|
|
|
289
305
|
test("没有重复项", () => {
|
|
306
|
+
// 报出**是哪几个**重复:只报"不该有重复"的话,下次在别人机器上红了还是得猜。
|
|
290
307
|
const parts = augmentedPath().split(":").filter(Boolean);
|
|
291
|
-
|
|
308
|
+
const seen = new Set();
|
|
309
|
+
const dup = [];
|
|
310
|
+
for (const d of parts) {
|
|
311
|
+
if (seen.has(d)) dup.push(d);
|
|
312
|
+
else seen.add(d);
|
|
313
|
+
}
|
|
314
|
+
assert.deepEqual(dup, [], `PATH 里不该有重复目录,重复的是:${dup.join(" , ")}`);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test("同一个目录换了写法(末尾斜杠)也只能出现一次", () => {
|
|
318
|
+
// GitHub 的 macOS/Ubuntu runner 上就是靠这条红的:PATH 里那项带着尾斜杠,
|
|
319
|
+
// 而补全候选是不带尾斜杠的同一个目录 → 精确比对判不出来 → 插了两遍。
|
|
320
|
+
const existing = __extraPathDirs(process.platform).find((d) => d.startsWith("/"));
|
|
321
|
+
if (!existing) return; // 没有可补的目录(极简容器),这条无从复现
|
|
322
|
+
const spelled = `${existing}/`;
|
|
323
|
+
const out = augmentedPath(spelled, process.platform);
|
|
324
|
+
const norm = (d) => __normPathForCompare(d, process.platform);
|
|
325
|
+
const keys = out.split(":").filter(Boolean).map(norm);
|
|
326
|
+
assert.equal(new Set(keys).size, keys.length, `按目录判重后仍重复:${out}`);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
test("判重归一化:尾斜杠、根目录、Windows 大小写", () => {
|
|
330
|
+
assert.equal(__normPathForCompare("/usr/local/bin/", "darwin"), "/usr/local/bin");
|
|
331
|
+
assert.equal(__normPathForCompare("/usr/local/bin///", "linux"), "/usr/local/bin");
|
|
332
|
+
assert.equal(__normPathForCompare("/", "darwin"), "/", "根目录不能被削成空串");
|
|
333
|
+
assert.equal(__normPathForCompare("C:\\Tools\\", "win32"), "c:\\tools", "Windows 目录名大小写不敏感");
|
|
334
|
+
assert.notEqual(__normPathForCompare("/A", "linux"), __normPathForCompare("/a", "linux"), "POSIX 不能小写化");
|
|
292
335
|
});
|
|
293
336
|
|
|
294
337
|
console.log("\n── Windows 上的 PATH 补全(真机是 darwin,靠注入平台验)──");
|
package/lib/capabilities.js
CHANGED
|
@@ -145,14 +145,43 @@ function isDir(d) {
|
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
+
/**
|
|
149
|
+
* 归一化一个 PATH 项,**只用于判重**,不改写真正写进 PATH 的字符串
|
|
150
|
+
* (原样保留:装到别人机器上的东西不该被我悄悄换个写法)。
|
|
151
|
+
*
|
|
152
|
+
* 为什么需要它:`/usr/local/bin` 与 `/usr/local/bin/` 是同一个目录,但字符串不等。
|
|
153
|
+
* 只做精确比对时,"补全"会把同一个目录再插一遍。这不是理论问题 —— 自测里那条
|
|
154
|
+
* "PATH 里不该有重复目录"在 GitHub 的 runner 上首跑就红了,本机不红(本机 PATH
|
|
155
|
+
* 的写法恰好一致)。判重必须按**目录**判,不能按字符串判。
|
|
156
|
+
*/
|
|
157
|
+
function normPathForCompare(d, p = platform()) {
|
|
158
|
+
let s = String(d).trim();
|
|
159
|
+
// 根目录去掉尾分隔符就没意义了("/" → ""),原样返回
|
|
160
|
+
const isRoot = s === "/" || /^[A-Za-z]:[\\/]?$/.test(s);
|
|
161
|
+
if (!isRoot) s = s.replace(/[\\/]+$/, "");
|
|
162
|
+
return p === "win32" ? s.toLowerCase() : s;
|
|
163
|
+
}
|
|
164
|
+
|
|
148
165
|
/** 补全后的 PATH 字符串。 */
|
|
149
166
|
export function augmentedPath(current = String(process.env.PATH ?? ""), p = platform()) {
|
|
150
167
|
// 分隔符必须跟平台走:Windows 是 `;`,POSIX 是 `:`。写死 `:` 会让 Windows 上
|
|
151
168
|
// 整个 PATH 变成一个"目录名",比不补还糟。
|
|
152
169
|
const sep = p === "win32" ? ";" : delimiter;
|
|
153
|
-
const
|
|
154
|
-
const
|
|
155
|
-
|
|
170
|
+
const parts = String(current).split(sep).filter(Boolean);
|
|
171
|
+
const present = new Set(parts);
|
|
172
|
+
const seen = new Set(parts.map((d) => normPathForCompare(d, p)));
|
|
173
|
+
const extra = [];
|
|
174
|
+
for (const d of extraPathDirs(p)) {
|
|
175
|
+
const key = normPathForCompare(d, p);
|
|
176
|
+
if (!key || seen.has(key)) continue; // 已经在 PATH 里(哪怕写法不同)就不再插
|
|
177
|
+
seen.add(key);
|
|
178
|
+
extra.push(d);
|
|
179
|
+
}
|
|
180
|
+
// 注意:**无条件重建**,不能"没有可补的就 return current"。
|
|
181
|
+
// 那条捷径会把调用方 PATH 里本来就有的重复项原样带出去 —— 当所有补全候选都
|
|
182
|
+
// 已经在 PATH 里时(GitHub 的 runner 上正是如此),"没有重复项"这条自测就会红,
|
|
183
|
+
// 而且红得莫名其妙:我们明明一个目录都没加。
|
|
184
|
+
return [...extra, ...present].join(sep);
|
|
156
185
|
}
|
|
157
186
|
/** 探测用的环境变量:补全 PATH,保留其余。 */
|
|
158
187
|
function probeEnv() {
|
|
@@ -386,6 +415,10 @@ export const __isEmptyButFailed = isEmptyButFailed;
|
|
|
386
415
|
/** 只为自测导出:让测试能注入平台/PATH 验证"PATH 残缺"判据,不必真改本机 PATH。 */
|
|
387
416
|
export const __extraPathDirs = extraPathDirs;
|
|
388
417
|
export const __pathLookupUnreliable = pathLookupUnreliable;
|
|
418
|
+
/** 只为自测导出:目录判重的归一化规则("换了写法"这种失败本机极难自然复现)。 */
|
|
419
|
+
export const __normPathForCompare = normPathForCompare;
|
|
420
|
+
/** 只为自测导出:原始 GPU 探测输出 —— 用来区分"这台机器没有显卡"和"解析器坏了"。 */
|
|
421
|
+
export const __probeGpu = probeGpu;
|
|
389
422
|
/** 只为自测导出:Store 别名目录的排除规则(Mac 上没有该目录,无法端到端复现)。 */
|
|
390
423
|
export const __storeAliasDir = STORE_ALIAS_DIR;
|
|
391
424
|
/** 只为自测导出:让自测能按判据反推 runtimesIncomplete 的期望值,而不是写死。 */
|
|
@@ -475,33 +508,53 @@ export function parseWindowsGpu(text) {
|
|
|
475
508
|
* ⚠️ 每个分支都必须读 `r.output`(runProbe 返回的是 `{output,error}`)。
|
|
476
509
|
* 直接把 `r` 交给解析函数就是上面说的那个 bug。
|
|
477
510
|
*/
|
|
511
|
+
/** 每个平台"怎么问 + 用哪个解析器"集中一张表:自测与生产共用同一条命令。 */
|
|
512
|
+
function gpuProbeSpec(p = platform()) {
|
|
513
|
+
switch (p) {
|
|
514
|
+
case "darwin":
|
|
515
|
+
return { cmd: "system_profiler", args: ["SPDisplaysDataType"], parse: parseMacGpu };
|
|
516
|
+
case "linux":
|
|
517
|
+
case "android":
|
|
518
|
+
return {
|
|
519
|
+
cmd: "nvidia-smi",
|
|
520
|
+
args: ["--query-gpu=name,memory.total", "--format=csv,noheader,nounits"],
|
|
521
|
+
parse: parseNvidiaGpu,
|
|
522
|
+
};
|
|
523
|
+
case "win32":
|
|
524
|
+
return {
|
|
525
|
+
cmd: "powershell",
|
|
526
|
+
args: ["-NoProfile", "-Command", "(Get-CimInstance Win32_VideoController).Name -join \"`n\""],
|
|
527
|
+
parse: parseWindowsGpu,
|
|
528
|
+
};
|
|
529
|
+
default:
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* 原始输出 + 解析结果一起返回。
|
|
536
|
+
*
|
|
537
|
+
* 为什么要把 output 也带出来:虚拟机/headless Mac 的 `system_profiler` 输出里
|
|
538
|
+
* 只有 Display 段、没有 Chipset Model,这时解析出 null 是**正确**结果;而"输出里
|
|
539
|
+
* 明明有 Chipset Model 却解析成 null"才是解析器回归。自测必须能区分这两者,
|
|
540
|
+
* 否则在有显卡的机器上永远绿、在 VM 上永远红(GitHub runner 上就是这么红的)。
|
|
541
|
+
*/
|
|
542
|
+
async function probeGpu() {
|
|
543
|
+
const spec = gpuProbeSpec();
|
|
544
|
+
if (!spec) return { output: "", gpu: null };
|
|
545
|
+
const r = await runProbe(spec.cmd, spec.args, GPU_TIMEOUT_MS);
|
|
546
|
+
const output = r.output ?? "";
|
|
547
|
+
if (!output) return { output: "", gpu: null };
|
|
548
|
+
return { output, gpu: spec.parse(output) };
|
|
549
|
+
}
|
|
550
|
+
|
|
478
551
|
async function collectGpu() {
|
|
479
|
-
const p = platform();
|
|
480
552
|
try {
|
|
481
|
-
|
|
482
|
-
const r = await runProbe("system_profiler", ["SPDisplaysDataType"], GPU_TIMEOUT_MS);
|
|
483
|
-
return r.output ? parseMacGpu(r.output) : null;
|
|
484
|
-
}
|
|
485
|
-
if (p === "linux" || p === "android") {
|
|
486
|
-
const r = await runProbe(
|
|
487
|
-
"nvidia-smi",
|
|
488
|
-
["--query-gpu=name,memory.total", "--format=csv,noheader,nounits"],
|
|
489
|
-
GPU_TIMEOUT_MS,
|
|
490
|
-
);
|
|
491
|
-
return r.output ? parseNvidiaGpu(r.output) : null;
|
|
492
|
-
}
|
|
493
|
-
if (p === "win32") {
|
|
494
|
-
const r = await runProbe(
|
|
495
|
-
"powershell",
|
|
496
|
-
["-NoProfile", "-Command", "(Get-CimInstance Win32_VideoController).Name -join \"`n\""],
|
|
497
|
-
GPU_TIMEOUT_MS,
|
|
498
|
-
);
|
|
499
|
-
return r.output ? parseWindowsGpu(r.output) : null;
|
|
500
|
-
}
|
|
553
|
+
return (await probeGpu()).gpu;
|
|
501
554
|
} catch {
|
|
502
555
|
/* 探测本身就不可靠,失败即"不知道" */
|
|
556
|
+
return null;
|
|
503
557
|
}
|
|
504
|
-
return null;
|
|
505
558
|
}
|
|
506
559
|
|
|
507
560
|
/**
|
package/lib/index.js
CHANGED
|
@@ -45,7 +45,7 @@ import { tailnetFetch, tailnetOrigin } from "./tsnet/bridge.js";
|
|
|
45
45
|
export const name = "dsh-plugin-mesh";
|
|
46
46
|
export const inject = ["tools", "webServer"];
|
|
47
47
|
|
|
48
|
-
const VERSION = "0.3.
|
|
48
|
+
const VERSION = "0.3.35";
|
|
49
49
|
const RULE_ID = "mesh-inbound";
|
|
50
50
|
const RULE_KIND = "mesh";
|
|
51
51
|
const MAX_BODY = 256 * 1024;
|
|
@@ -1571,7 +1571,7 @@ function registerTools(ctx, { discovery, config, inbound, outbound, log, warn })
|
|
|
1571
1571
|
output: { schema: { type: "string" }, render: (_a, v) => text(v) },
|
|
1572
1572
|
async execute() {
|
|
1573
1573
|
const before = discovery.peers().length;
|
|
1574
|
-
const r = await runScan({ discovery, config, warn, reason: "agent 触发扫描" });
|
|
1574
|
+
const r = await runScan({ discovery, config, log, warn, reason: "agent 触发扫描" });
|
|
1575
1575
|
const after = discovery.peers();
|
|
1576
1576
|
const lines = after.map((p) => `${p.online ? "●" : "○"} ${p.machine} ${p.host}:${p.port}`).join("\n");
|
|
1577
1577
|
return (
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-mesh",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.35",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "DSH Mesh:局域网自动发现同网段的 DSH 实例、显示每台机器上报的能力,并双向派发任务;可用内置的用户态 Tailscale 节点跨互联网组网。Discovers other DSH instances on the LAN, shows the capabilities each one reports, and dispatches tasks between machines in both directions, with an optional userspace Tailscale node for links across networks.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -58,10 +58,13 @@
|
|
|
58
58
|
"selftest:prompt": "node lib/_selftest-prompt.mjs",
|
|
59
59
|
"selftest:caps": "node lib/_selftest-capabilities.mjs",
|
|
60
60
|
"selftest:newsession": "node lib/_selftest-newsession.mjs",
|
|
61
|
-
"
|
|
62
|
-
"
|
|
61
|
+
"selftest:release": "node scripts/_selftest-release-plan.mjs",
|
|
62
|
+
"test": "npm run check && npm run selftest && npm run selftest:prompt && npm run selftest:pairing && npm run selftest:client && npm run selftest:caps && npm run selftest:release",
|
|
63
|
+
"check": "node scripts/check-version.mjs && node scripts/check-tdz.mjs && node --check lib/index.js && node --check lib/discovery.js && node --check lib/conversation.js && node --check lib/identity.js && node --check lib/capabilities.js && node --check lib/prompt.js && node --check lib/pairing.js && node --check lib/scan.js && node --check lib/tsnet/download.js && node --check lib/tsnet/manager.js && node --check lib/tsnet/bridge.js && node --check client/client.js && node --check scripts/release-plan.mjs && npm run check:loadable && npm run check:docs",
|
|
63
64
|
"build:sidecar": "bash scripts/build-sidecar.sh",
|
|
64
65
|
"verify:mirror": "node scripts/verify-lan-mirror.mjs",
|
|
66
|
+
"release:plan": "node scripts/release-plan.mjs",
|
|
67
|
+
"release-plugin": "bash scripts/release-plugin.sh",
|
|
65
68
|
"check:loadable": "node scripts/check-loadable.mjs --allow-missing-runtime-dep",
|
|
66
69
|
"check:deploy": "node scripts/check-loadable.mjs",
|
|
67
70
|
"check:scope": "python3 tools/scopecheck.py lib/index.js",
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* release-plan 的自测。
|
|
3
|
+
*
|
|
4
|
+
* 为什么这段逻辑值得单独测:它现在**真的会替人做发版决定** —— 一次误判就是往
|
|
5
|
+
* npm 上推一个错误版本号,而 npm 的版本号不能覆盖、只能 deprecate。所以这里把
|
|
6
|
+
* 那些真实发生过的半态穷举一遍(tag 有了包没发、包发了版本没改、只改了文档、
|
|
7
|
+
* squash 与 merge commit 两种历史形状),而不是等它在 CI 里第一次运行时才发现。
|
|
8
|
+
*
|
|
9
|
+
* 跑法:node scripts/_selftest-release-plan.mjs (已挂在 npm test 里)
|
|
10
|
+
*/
|
|
11
|
+
import assert from "node:assert/strict";
|
|
12
|
+
import {
|
|
13
|
+
TAG_PREFIX,
|
|
14
|
+
analyzeCommits,
|
|
15
|
+
applyBump,
|
|
16
|
+
bumpFromCommit,
|
|
17
|
+
compareSemver,
|
|
18
|
+
parseSemver,
|
|
19
|
+
planRelease,
|
|
20
|
+
tagVersion,
|
|
21
|
+
} from "./release-plan.mjs";
|
|
22
|
+
|
|
23
|
+
let pass = 0;
|
|
24
|
+
let fail = 0;
|
|
25
|
+
|
|
26
|
+
function test(name, fn) {
|
|
27
|
+
try {
|
|
28
|
+
fn();
|
|
29
|
+
pass += 1;
|
|
30
|
+
console.log(` ✓ ${name}`);
|
|
31
|
+
} catch (err) {
|
|
32
|
+
fail += 1;
|
|
33
|
+
console.log(` ✗ ${name}`);
|
|
34
|
+
console.log(` ${err.message.split("\n")[0]}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
console.log("\n── semver ──");
|
|
39
|
+
|
|
40
|
+
test("解析与比较", () => {
|
|
41
|
+
assert.deepEqual(parseSemver("0.3.33"), { major: 0, minor: 3, patch: 33 });
|
|
42
|
+
assert.equal(compareSemver("0.3.33", "0.3.34"), -1);
|
|
43
|
+
assert.equal(compareSemver("0.4.0", "0.3.99"), 1);
|
|
44
|
+
assert.equal(compareSemver("1.0.0", "1.0.0"), 0);
|
|
45
|
+
assert.equal(parseSemver("0.3"), null, "两段的「版本号」不是 semver,必须拒绝而不是猜");
|
|
46
|
+
assert.throws(() => compareSemver("abc", "1.0.0"), /不可比较/);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("bump:0.x 阶段 breaking 只能升到 0.(y+1).0,不能替用户跳到 1.0.0", () => {
|
|
50
|
+
assert.equal(applyBump("0.3.33", "patch"), "0.3.34");
|
|
51
|
+
assert.equal(applyBump("0.3.33", "minor"), "0.4.0");
|
|
52
|
+
assert.equal(applyBump("0.3.33", "major"), "0.4.0", "0.x 的 major 必须降级成 minor");
|
|
53
|
+
assert.equal(applyBump("1.2.3", "major"), "2.0.0", "到了 1.x 之后 major 才是 major");
|
|
54
|
+
assert.throws(() => applyBump("1.0.0", "none"), /未知 bump/);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
console.log("\n── 提交信息 → bump 类型 ──");
|
|
58
|
+
|
|
59
|
+
test("常规类型", () => {
|
|
60
|
+
assert.equal(bumpFromCommit({ subject: "feat: 加个按钮" }), "minor");
|
|
61
|
+
assert.equal(bumpFromCommit({ subject: "fix(ui): 配对码没显示" }), "patch");
|
|
62
|
+
assert.equal(bumpFromCommit({ subject: "perf: 少扫一遍" }), "patch");
|
|
63
|
+
assert.equal(bumpFromCommit({ subject: "refactor: 拆函数" }), "patch");
|
|
64
|
+
assert.equal(bumpFromCommit({ subject: "revert: 回滚 xx" }), "patch");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("不该发版的类型(改文档不等于用户要装新版)", () => {
|
|
68
|
+
for (const s of ["docs: 补说明", "chore: 收拾", "ci: 加个 action", "test: 补用例", "style: 空格", "build: 版本"]) {
|
|
69
|
+
assert.equal(bumpFromCommit({ subject: s }), "none", s);
|
|
70
|
+
}
|
|
71
|
+
assert.equal(bumpFromCommit({ subject: "随手改了点什么" }), "none");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("breaking:`!` 与正文里的 BREAKING CHANGE 都算", () => {
|
|
75
|
+
assert.equal(bumpFromCommit({ subject: "feat!: 换协议" }), "major");
|
|
76
|
+
assert.equal(bumpFromCommit({ subject: "feat(cli)!: 换参数" }), "major");
|
|
77
|
+
assert.equal(
|
|
78
|
+
bumpFromCommit({ subject: "feat: 换协议", body: "说明\n\nBREAKING CHANGE: 旧客户端连不上" }),
|
|
79
|
+
"major",
|
|
80
|
+
);
|
|
81
|
+
assert.equal(
|
|
82
|
+
bumpFromCommit({ subject: "feat: 换协议", body: "BREAKING-CHANGE: 连字符写法" }),
|
|
83
|
+
"major",
|
|
84
|
+
"连字符写法也是规范的一部分",
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("GitHub「Merge pull request」提交:类型藏在正文里,不能因此静默不发版", () => {
|
|
89
|
+
const merge = {
|
|
90
|
+
subject: "Merge pull request #12 from someone/feat-x",
|
|
91
|
+
body: "feat: 支持跨网段派单\n\n细节说明",
|
|
92
|
+
};
|
|
93
|
+
assert.equal(bumpFromCommit(merge), "minor");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("正文里的列表式提交行也认(- feat: xxx)", () => {
|
|
97
|
+
assert.equal(bumpFromCommit({ subject: "Merge pull request #3", body: "- fix: 修个 null" }), "patch");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("一批提交取最高级别,并给出理由", () => {
|
|
101
|
+
const r = analyzeCommits([
|
|
102
|
+
{ subject: "docs: a" },
|
|
103
|
+
{ subject: "fix: b" },
|
|
104
|
+
{ subject: "feat: c" },
|
|
105
|
+
{ subject: "chore: d" },
|
|
106
|
+
]);
|
|
107
|
+
assert.equal(r.bump, "minor");
|
|
108
|
+
assert.deepEqual(r.hits.map((h) => h.subject), ["feat: c"]);
|
|
109
|
+
|
|
110
|
+
const major = analyzeCommits([{ subject: "fix: a" }, { subject: "feat!: b" }]);
|
|
111
|
+
assert.equal(major.bump, "major");
|
|
112
|
+
assert.equal(major.hits.length, 1);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
console.log("\n── tag 解析 ──");
|
|
116
|
+
|
|
117
|
+
test("只认 plugin-v 前缀,别把 sidecar 的 tag 当插件版本", () => {
|
|
118
|
+
assert.equal(tagVersion(`${TAG_PREFIX}0.3.33`), "0.3.33");
|
|
119
|
+
assert.equal(tagVersion("v0.3.33"), null);
|
|
120
|
+
assert.equal(tagVersion(""), null);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
console.log("\n── 状态机:真实半态 ──");
|
|
124
|
+
|
|
125
|
+
test("正常推进:tag=npm=仓库=0.3.33,有 feat → 0.4.0", () => {
|
|
126
|
+
const p = planRelease({
|
|
127
|
+
pkgVersion: "0.3.33",
|
|
128
|
+
tag: "plugin-v0.3.33",
|
|
129
|
+
npmLatest: "0.3.33",
|
|
130
|
+
commits: [{ subject: "feat: 新面板" }],
|
|
131
|
+
});
|
|
132
|
+
assert.equal(p.mode, "bump");
|
|
133
|
+
assert.equal(p.version, "0.4.0");
|
|
134
|
+
assert.equal(p.tag, "plugin-v0.4.0");
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("只有文档改动 → 不发版(每改一句话就发一版是最贵的注意力税)", () => {
|
|
138
|
+
const p = planRelease({
|
|
139
|
+
pkgVersion: "0.3.33",
|
|
140
|
+
tag: "plugin-v0.3.33",
|
|
141
|
+
npmLatest: "0.3.33",
|
|
142
|
+
commits: [{ subject: "docs: 补一段" }, { subject: "chore: 格式化" }],
|
|
143
|
+
});
|
|
144
|
+
assert.equal(p.mode, "skip");
|
|
145
|
+
assert.equal(p.release, false);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("重试路径:版本号已改(0.3.34)、tag 还停在 0.3.33、npm 也停在 0.3.33 → 直接发 0.3.34", () => {
|
|
149
|
+
const p = planRelease({
|
|
150
|
+
pkgVersion: "0.3.34",
|
|
151
|
+
tag: "plugin-v0.3.33",
|
|
152
|
+
npmLatest: "0.3.33",
|
|
153
|
+
commits: [{ subject: "fix: 上次发版挂了" }],
|
|
154
|
+
});
|
|
155
|
+
assert.equal(p.mode, "publish-existing");
|
|
156
|
+
assert.equal(p.version, "0.3.34");
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("重试路径(tag 已推、包没发):tag=仓库=0.3.34、npm=0.3.33 → 发 0.3.34,不再 bump", () => {
|
|
160
|
+
const p = planRelease({
|
|
161
|
+
pkgVersion: "0.3.34",
|
|
162
|
+
tag: "plugin-v0.3.34",
|
|
163
|
+
npmLatest: "0.3.33",
|
|
164
|
+
commits: [{ subject: "fix: x" }],
|
|
165
|
+
});
|
|
166
|
+
assert.equal(p.mode, "publish-existing");
|
|
167
|
+
assert.equal(p.version, "0.3.34");
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("npm 比仓库新 → 以 npm 为基准继续 bump,而不是停手(否则会死锁)", () => {
|
|
171
|
+
// 现实来源:上次的版本号提交被分支保护挡住、或者有人在别的机器上手发过一次。
|
|
172
|
+
// 若按"仓库版本"算,算出来的号 npm 上已经有了 → publish 被拒 → 下一轮又算同一个号。
|
|
173
|
+
const bumped = planRelease({
|
|
174
|
+
pkgVersion: "0.3.33",
|
|
175
|
+
tag: "plugin-v0.3.33",
|
|
176
|
+
npmLatest: "0.3.34",
|
|
177
|
+
commits: [{ subject: "fix: x" }],
|
|
178
|
+
});
|
|
179
|
+
assert.equal(bumped.base, "0.3.34", "基准必须取 npm 上的版本");
|
|
180
|
+
assert.equal(bumped.mode, "bump");
|
|
181
|
+
assert.equal(bumped.version, "0.3.35", "不能算出一个已经存在的版本号");
|
|
182
|
+
assert.match(bumped.warn.join(" "), /落后/);
|
|
183
|
+
|
|
184
|
+
// 但"没有值得发的东西"时仍然不发 —— 漂移不该成为发空版的理由。
|
|
185
|
+
const idle = planRelease({
|
|
186
|
+
pkgVersion: "0.3.33",
|
|
187
|
+
tag: "plugin-v0.3.33",
|
|
188
|
+
npmLatest: "0.3.34",
|
|
189
|
+
commits: [{ subject: "docs: 只改了说明" }],
|
|
190
|
+
});
|
|
191
|
+
assert.equal(idle.mode, "skip");
|
|
192
|
+
assert.match(idle.warn.join(" "), /落后/);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test("没有 npm latest 时按 tag 推断(本地演练常见)", () => {
|
|
196
|
+
// tag 是"这一版真的发过"的证据:tag 与仓库同版本 → 当成已发布,正常往上 bump。
|
|
197
|
+
const published = planRelease({
|
|
198
|
+
pkgVersion: "0.3.33",
|
|
199
|
+
tag: "plugin-v0.3.33",
|
|
200
|
+
npmLatest: "",
|
|
201
|
+
commits: [{ subject: "fix: x" }],
|
|
202
|
+
});
|
|
203
|
+
assert.equal(published.mode, "bump");
|
|
204
|
+
assert.equal(published.version, "0.3.34");
|
|
205
|
+
|
|
206
|
+
// tag 落后于仓库 → 版本号改过了但没发,直接发,不再叠一层。
|
|
207
|
+
const pending = planRelease({
|
|
208
|
+
pkgVersion: "0.3.34",
|
|
209
|
+
tag: "plugin-v0.3.33",
|
|
210
|
+
npmLatest: "",
|
|
211
|
+
commits: [{ subject: "fix: x" }],
|
|
212
|
+
});
|
|
213
|
+
assert.equal(pending.mode, "publish-existing");
|
|
214
|
+
assert.equal(pending.version, "0.3.34");
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test("手动指定 --bump:跳过提交分析也要真的往上升", () => {
|
|
218
|
+
const p = planRelease({
|
|
219
|
+
pkgVersion: "0.3.33",
|
|
220
|
+
tag: "plugin-v0.3.33",
|
|
221
|
+
npmLatest: "0.3.33",
|
|
222
|
+
forcedBump: "patch",
|
|
223
|
+
commits: [],
|
|
224
|
+
});
|
|
225
|
+
assert.equal(p.mode, "bump");
|
|
226
|
+
assert.equal(p.version, "0.3.34");
|
|
227
|
+
|
|
228
|
+
const none = planRelease({
|
|
229
|
+
pkgVersion: "0.3.33",
|
|
230
|
+
tag: "plugin-v0.3.33",
|
|
231
|
+
npmLatest: "0.3.33",
|
|
232
|
+
forcedBump: "none",
|
|
233
|
+
commits: [{ subject: "feat: x" }],
|
|
234
|
+
});
|
|
235
|
+
assert.equal(none.mode, "skip");
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
test("已发布却没给提交列表 → 直接报错,不猜", () => {
|
|
239
|
+
assert.throws(
|
|
240
|
+
() => planRelease({ pkgVersion: "0.3.33", tag: "plugin-v0.3.33", npmLatest: "0.3.33", commits: null }),
|
|
241
|
+
/必须提供提交列表/,
|
|
242
|
+
);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test("非法 bump 类型立刻炸", () => {
|
|
246
|
+
assert.throws(
|
|
247
|
+
() => planRelease({ pkgVersion: "0.3.33", tag: "", npmLatest: "", forcedBump: "huge", commits: [] }),
|
|
248
|
+
/--bump/,
|
|
249
|
+
);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
console.log(`\n全部${fail === 0 ? "通过" : "结束"}:${pass} 通过 / ${fail} 失败`);
|
|
253
|
+
if (fail > 0) process.exit(1);
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# 打「插件源码包」:给不能 git clone 的机器准备的 GitHub Release 资产。
|
|
3
|
+
#
|
|
4
|
+
# 为什么单独抽成脚本:这条打包命令现在有两个调用方 —— 本机手工发版
|
|
5
|
+
# (scripts/release-plugin.sh)和 CI 自动发版(.github/workflows/release.yml)。
|
|
6
|
+
# 复制两份的必然结局是漂移:某天 files 白名单加了个目录,手发的那份带上了、
|
|
7
|
+
# CI 那份没带,而两边都不会报错(这项目在"看着接上了其实没接"上栽过好几次)。
|
|
8
|
+
#
|
|
9
|
+
# stdout 只输出机器可读的三行,日志走 stderr:
|
|
10
|
+
# asset=/abs/path/dsh-plugin-mesh-0.3.33.tar.gz
|
|
11
|
+
# size=101K
|
|
12
|
+
# sha256=...
|
|
13
|
+
#
|
|
14
|
+
# 用法:
|
|
15
|
+
# scripts/pack-release-tarball.sh [输出目录] # 默认 dist/
|
|
16
|
+
set -euo pipefail
|
|
17
|
+
|
|
18
|
+
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
19
|
+
VERSION="$(node -p "require('$REPO_ROOT/package.json').version")"
|
|
20
|
+
ASSET="dsh-plugin-mesh-$VERSION.tar.gz"
|
|
21
|
+
OUT_DIR="${1:-$REPO_ROOT/dist}"
|
|
22
|
+
|
|
23
|
+
mkdir -p "$OUT_DIR"
|
|
24
|
+
OUT="$OUT_DIR/$ASSET"
|
|
25
|
+
rm -f "$OUT"
|
|
26
|
+
|
|
27
|
+
{
|
|
28
|
+
echo "==> 打包插件 v$VERSION"
|
|
29
|
+
# sidecar 的 Go 源码要带上:用户可据此审计下载回来的二进制是用什么编出来的。
|
|
30
|
+
} >&2
|
|
31
|
+
|
|
32
|
+
tar --exclude='.git' \
|
|
33
|
+
--exclude='dist' \
|
|
34
|
+
--exclude='node_modules' \
|
|
35
|
+
--exclude='.DS_Store' \
|
|
36
|
+
-czf "$OUT" \
|
|
37
|
+
-C "$REPO_ROOT" \
|
|
38
|
+
lib client sidecar scripts cordis.patch.yml package.json README.md LICENSE install.sh
|
|
39
|
+
|
|
40
|
+
SIZE="$(du -h "$OUT" | cut -f1)"
|
|
41
|
+
SHA="$(shasum -a 256 "$OUT" 2>/dev/null | cut -d' ' -f1 || sha256sum "$OUT" | cut -d' ' -f1)"
|
|
42
|
+
|
|
43
|
+
echo " 产物: $OUT ($SIZE, $(tar -tzf "$OUT" | wc -l | tr -d ' ') 个条目)" >&2
|
|
44
|
+
|
|
45
|
+
echo "asset=$OUT"
|
|
46
|
+
echo "size=$SIZE"
|
|
47
|
+
echo "sha256=$SHA"
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 决定「这次该不该发版、发哪个版本号」,可选地把版本号写回文件。
|
|
3
|
+
*
|
|
4
|
+
* 为什么要有它:发版的决定权以前在人的脑子里 —— 改完代码自己想"这算 patch 还是
|
|
5
|
+
* minor",然后手改 package.json、手改 lib/index.js 的 VERSION、手打 tag、手点
|
|
6
|
+
* release、手跑 `npm publish`。五个动作里漏一个就是**半态**:tag 有了包没发、包
|
|
7
|
+
* 发了版本号对不上、release 资产是上一版。这个项目已经在"看着接上了其实没接"
|
|
8
|
+
* 上栽过好几次,所以这里把"该发什么版本"变成一条可跑、可测、可在 CI 里复现的
|
|
9
|
+
* 命令,而不是一段记忆。
|
|
10
|
+
*
|
|
11
|
+
* 它只做三件事,不做任何网络请求:
|
|
12
|
+
* 1. 认上一版在哪(git tag `plugin-v*` + npm 上的 latest,后者由调用方传入)
|
|
13
|
+
* 2. 读 `上一版..HEAD` 的提交,按 Conventional Commits 推 bump 类型
|
|
14
|
+
* 3. 说清楚下一步:bump(要发新版本)/ publish-existing(版本号已经改好、只差
|
|
15
|
+
* 打 tag 和发)/ skip(没有值得发的东西)
|
|
16
|
+
*
|
|
17
|
+
* 用法:
|
|
18
|
+
* node scripts/release-plan.mjs # 只看计划,不改文件
|
|
19
|
+
* node scripts/release-plan.mjs --since origin/main # 指定比较起点
|
|
20
|
+
* node scripts/release-plan.mjs --npm-latest 0.3.33 # 告诉它 npm 上是哪一版
|
|
21
|
+
* node scripts/release-plan.mjs --apply # 真的把版本号写进文件
|
|
22
|
+
* node scripts/release-plan.mjs --bump minor --apply # 强行指定 bump 类型
|
|
23
|
+
* node scripts/release-plan.mjs --github-output "$GITHUB_OUTPUT"
|
|
24
|
+
*
|
|
25
|
+
* --github-output 会把结论写成 `key=value` 追加进该文件,供 workflow 的
|
|
26
|
+
* `steps.plan.outputs.*` 读取;人看的说明走 stdout。
|
|
27
|
+
*/
|
|
28
|
+
import { execFileSync } from "node:child_process";
|
|
29
|
+
import { appendFileSync, readFileSync, writeFileSync } from "node:fs";
|
|
30
|
+
import { dirname, join } from "node:path";
|
|
31
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
32
|
+
|
|
33
|
+
export const TAG_PREFIX = "plugin-v";
|
|
34
|
+
|
|
35
|
+
/** 0.x 阶段把 breaking 当 minor:直接跳 1.0.0 是**替用户做决定**,不该由提交信息触发。 */
|
|
36
|
+
export const ZERO_MAJOR_AS_MINOR = true;
|
|
37
|
+
|
|
38
|
+
const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
39
|
+
|
|
40
|
+
// ── 小工具 ────────────────────────────────────────────────────────────────
|
|
41
|
+
|
|
42
|
+
export function parseSemver(v) {
|
|
43
|
+
const m = /^(\d+)\.(\d+)\.(\d+)(?:[-+].*)?$/.exec(String(v ?? "").trim());
|
|
44
|
+
if (!m) return null;
|
|
45
|
+
return { major: Number(m[1]), minor: Number(m[2]), patch: Number(m[3]) };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 返回 -1 / 0 / 1;任一侧解析不出来就抛 —— 版本号问题必须当场炸,不能猜。 */
|
|
49
|
+
export function compareSemver(a, b) {
|
|
50
|
+
const x = parseSemver(a);
|
|
51
|
+
const y = parseSemver(b);
|
|
52
|
+
if (!x || !y) throw new Error(`版本号不可比较:${JSON.stringify(a)} vs ${JSON.stringify(b)}`);
|
|
53
|
+
for (const k of ["major", "minor", "patch"]) {
|
|
54
|
+
if (x[k] !== y[k]) return x[k] < y[k] ? -1 : 1;
|
|
55
|
+
}
|
|
56
|
+
return 0;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function applyBump(version, bump) {
|
|
60
|
+
const v = parseSemver(version);
|
|
61
|
+
if (!v) throw new Error(`当前版本号不合法:${version}`);
|
|
62
|
+
switch (bump) {
|
|
63
|
+
case "major":
|
|
64
|
+
return ZERO_MAJOR_AS_MINOR && v.major === 0
|
|
65
|
+
? `0.${v.minor + 1}.0`
|
|
66
|
+
: `${v.major + 1}.0.0`;
|
|
67
|
+
case "minor":
|
|
68
|
+
return `${v.major}.${v.minor + 1}.0`;
|
|
69
|
+
case "patch":
|
|
70
|
+
return `${v.major}.${v.minor}.${v.patch + 1}`;
|
|
71
|
+
default:
|
|
72
|
+
throw new Error(`未知 bump 类型:${bump}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ── 提交信息解析 ──────────────────────────────────────────────────────────
|
|
77
|
+
|
|
78
|
+
/** 一行是不是 Conventional Commit;允许前面带列表符号(合并提交的正文会长这样)。 */
|
|
79
|
+
const HEADER = /^\s*(?:[-*+]\s*)?(feat|fix|perf|refactor|revert|docs|chore|ci|test|style|build)(?:\([^)]*\))?(!)?\s*:\s*/;
|
|
80
|
+
|
|
81
|
+
const RANK = { none: 0, patch: 1, minor: 2, major: 3 };
|
|
82
|
+
/** 只有这三种会让人真的想装新版本;其余(docs/chore/ci/test/style/build)不触发发版。 */
|
|
83
|
+
const TYPE_TO_BUMP = { feat: "minor", fix: "patch", perf: "patch", refactor: "patch", revert: "patch" };
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 从一条提交里读出 bump 类型。
|
|
87
|
+
*
|
|
88
|
+
* 为什么要连正文一起看:GitHub 的「Merge pull request」合并提交,标题是
|
|
89
|
+
* `Merge pull request #12 from a/b`(不含类型),真正的 `feat: ...` 在正文第一行。
|
|
90
|
+
* 只读标题会让这种仓库**永远不发版**,而且静默 —— 最坏的一类 bug。
|
|
91
|
+
*/
|
|
92
|
+
export function bumpFromCommit({ subject = "", body = "" } = {}) {
|
|
93
|
+
let bump = "none";
|
|
94
|
+
const hasBreaking = /(^|\n)BREAKING[ -]CHANGE:/.test(`${subject}\n${body}`);
|
|
95
|
+
|
|
96
|
+
const take = (line) => {
|
|
97
|
+
const m = HEADER.exec(line);
|
|
98
|
+
if (!m) return;
|
|
99
|
+
const type = m[1];
|
|
100
|
+
const bang = Boolean(m[2]);
|
|
101
|
+
if (bang || hasBreaking) {
|
|
102
|
+
bump = RANK[bump] >= RANK.major ? bump : "major";
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const t = TYPE_TO_BUMP[type];
|
|
106
|
+
if (t && RANK[t] > RANK[bump]) bump = t;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
take(subject);
|
|
110
|
+
for (const line of body.split("\n")) take(line);
|
|
111
|
+
return bump;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** 一批提交 → 总体 bump 类型 + 触发它的那几条(用于日志里解释"为什么是这个版本号")。 */
|
|
115
|
+
export function analyzeCommits(commits) {
|
|
116
|
+
let bump = "none";
|
|
117
|
+
const hits = [];
|
|
118
|
+
for (const c of commits) {
|
|
119
|
+
const b = bumpFromCommit(c);
|
|
120
|
+
if (b !== "none" && RANK[b] > RANK[bump]) bump = b;
|
|
121
|
+
if (b !== "none") hits.push({ bump: b, subject: c.subject });
|
|
122
|
+
}
|
|
123
|
+
const top = hits.filter((h) => h.bump === bump);
|
|
124
|
+
return { bump, hits: top };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// ── git ───────────────────────────────────────────────────────────────────
|
|
128
|
+
|
|
129
|
+
function git(args) {
|
|
130
|
+
return execFileSync("git", args, { cwd: ROOT, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }).trim();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function gitOrNull(args) {
|
|
134
|
+
try {
|
|
135
|
+
return git(args);
|
|
136
|
+
} catch {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function lastTag() {
|
|
142
|
+
// ⚠ 必须 --match:仓库里还可能有 sidecar 用的其他 tag 命名(v0.3.x 等),
|
|
143
|
+
// 混进来会把"插件版本"算到别的流水线上。
|
|
144
|
+
return gitOrNull(["describe", "--tags", "--abbrev=0", "--match", `${TAG_PREFIX}*`]) || "";
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function tagVersion(tag) {
|
|
148
|
+
if (!tag || !tag.startsWith(TAG_PREFIX)) return null;
|
|
149
|
+
return tag.slice(TAG_PREFIX.length);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** 读一段区间的提交;%x1f 分隔字段、%x1e 分隔记录 —— 正文里换行不会破坏结构。 */
|
|
153
|
+
export function readCommits(range) {
|
|
154
|
+
const out = gitOrNull(["log", "--format=%s%x1f%b%x1e", range]);
|
|
155
|
+
if (!out) return [];
|
|
156
|
+
return out
|
|
157
|
+
.split("\x1e")
|
|
158
|
+
.map((r) => r.replace(/^\n+/, ""))
|
|
159
|
+
.filter(Boolean)
|
|
160
|
+
.map((r) => {
|
|
161
|
+
const [subject = "", body = ""] = r.split("\x1f");
|
|
162
|
+
return { subject: subject.trim(), body };
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ── 计划 ──────────────────────────────────────────────────────────────────
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 纯函数:给状态,返回计划。抽出来是为了能在 selftest 里穷举半态
|
|
170
|
+
* (tag 有了包没发、包发了版本没改、只在文档上改了……),不用真去改仓库。
|
|
171
|
+
*/
|
|
172
|
+
export function planRelease({ pkgVersion, tag = "", npmLatest = "", since = "", forcedBump = "", commits = null }) {
|
|
173
|
+
let base = pkgVersion;
|
|
174
|
+
const tagVer = tagVersion(tag);
|
|
175
|
+
const notes = [];
|
|
176
|
+
const warn = [];
|
|
177
|
+
|
|
178
|
+
if (forcedBump && !["major", "minor", "patch", "none"].includes(forcedBump)) {
|
|
179
|
+
throw new Error(`--bump 只能是 major/minor/patch/none,收到 ${forcedBump}`);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// 基准 = 仓库版本与 npm latest 中**较大**的那个。
|
|
183
|
+
//
|
|
184
|
+
// 为什么不是"仓库版本为准":上一次的版本号提交如果被分支保护挡住、或者有人在本机
|
|
185
|
+
// 手发过一次,仓库就会永远落后 npm 一格。这时若还拿仓库版本当基准,算出来的
|
|
186
|
+
// 版本号是"npm 上已经有的那个",publish 会被拒,而下一轮又算出同一个号 —— 死锁,
|
|
187
|
+
// 而且每轮都红一次。取最大值既解开死锁,也让漂移在下一次发版时自动追平。
|
|
188
|
+
if (npmLatest) {
|
|
189
|
+
const rel = compareSemver(npmLatest, pkgVersion);
|
|
190
|
+
if (rel > 0) {
|
|
191
|
+
base = npmLatest;
|
|
192
|
+
warn.push(
|
|
193
|
+
`仓库版本 ${pkgVersion} 落后于 npm ${npmLatest}(上次的版本号提交没推上去?)—— 以 npm 为基准,这次会顺带追平`,
|
|
194
|
+
);
|
|
195
|
+
} else if (rel < 0) {
|
|
196
|
+
notes.push(`仓库 ${pkgVersion} 比 npm ${npmLatest} 新 → 直接发 ${pkgVersion}`);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// npm 上有这一版 = 发过了。拿不到 npm(离线演练)时才退回看 tag。
|
|
201
|
+
const published = npmLatest ? compareSemver(npmLatest, base) === 0 : tagVer === base;
|
|
202
|
+
|
|
203
|
+
// base 还没进 npm → 版本号早就改好了,只差发。**这一步是失败重试的入口**:
|
|
204
|
+
// 上一次 publish 挂了(可信发布没配好、网络抖动),重跑时不该再 bump 一次。
|
|
205
|
+
if (!published) {
|
|
206
|
+
if (npmLatest) notes.push(`npm latest=${npmLatest},基准=${base} → 直接发 ${base}(不 bump)`);
|
|
207
|
+
else notes.push(`没有 npm latest,按 tag ${tag || "(无)"} 推断 ${base} 尚未发布`);
|
|
208
|
+
return {
|
|
209
|
+
mode: "publish-existing",
|
|
210
|
+
release: true,
|
|
211
|
+
version: base,
|
|
212
|
+
tag: `${TAG_PREFIX}${base}`,
|
|
213
|
+
base,
|
|
214
|
+
bump: "none",
|
|
215
|
+
commits: commits?.length ?? 0,
|
|
216
|
+
reason: `版本号 ${base} 还没发布,直接发它`,
|
|
217
|
+
notes,
|
|
218
|
+
warn,
|
|
219
|
+
hits: [],
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// 已发布:看有没有值得发的新提交。
|
|
224
|
+
if (forcedBump === "none") {
|
|
225
|
+
return {
|
|
226
|
+
mode: "skip", release: false, version: base, tag: `${TAG_PREFIX}${base}`, base,
|
|
227
|
+
bump: "none", commits: commits?.length ?? 0, reason: "手动指定 none", notes, warn, hits: [],
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (commits === null) {
|
|
232
|
+
throw new Error("已发布状态下必须提供提交列表(commits),否则无法判断该不该发新版");
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const analyzed = forcedBump
|
|
236
|
+
? { bump: forcedBump, hits: [] }
|
|
237
|
+
: analyzeCommits(commits);
|
|
238
|
+
const bump = analyzed.bump;
|
|
239
|
+
|
|
240
|
+
if (bump === "none") {
|
|
241
|
+
return {
|
|
242
|
+
mode: "skip",
|
|
243
|
+
release: false,
|
|
244
|
+
version: base,
|
|
245
|
+
tag: `${TAG_PREFIX}${base}`,
|
|
246
|
+
base,
|
|
247
|
+
bump: "none",
|
|
248
|
+
commits: commits.length,
|
|
249
|
+
reason: `${commits.length} 条提交里没有 feat/fix/perf/refactor/revert —— 只有文档/杂务,不值得发版`,
|
|
250
|
+
notes,
|
|
251
|
+
warn,
|
|
252
|
+
hits: [],
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const version = applyBump(base, bump);
|
|
257
|
+
return {
|
|
258
|
+
mode: "bump",
|
|
259
|
+
release: true,
|
|
260
|
+
version,
|
|
261
|
+
tag: `${TAG_PREFIX}${version}`,
|
|
262
|
+
base,
|
|
263
|
+
bump,
|
|
264
|
+
commits: commits.length,
|
|
265
|
+
reason: `${bump}:${analyzed.hits.map((h) => h.subject).slice(0, 3).join(" / ") || "手动指定"}`,
|
|
266
|
+
notes,
|
|
267
|
+
warn,
|
|
268
|
+
hits: analyzed.hits,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// ── 写回文件 ──────────────────────────────────────────────────────────────
|
|
273
|
+
|
|
274
|
+
/** 版本号写在两个地方,必须一起改 —— 这正是 check-version.mjs 存在的理由。 */
|
|
275
|
+
export function writeVersion(version, root = ROOT) {
|
|
276
|
+
const pkgPath = join(root, "package.json");
|
|
277
|
+
const indexPath = join(root, "lib/index.js");
|
|
278
|
+
|
|
279
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
280
|
+
const before = pkg.version;
|
|
281
|
+
pkg.version = version;
|
|
282
|
+
writeFileSync(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`);
|
|
283
|
+
|
|
284
|
+
const src = readFileSync(indexPath, "utf8");
|
|
285
|
+
const m = /^const VERSION = "[^"]+";$/m;
|
|
286
|
+
if (!m.test(src)) throw new Error("lib/index.js 里找不到 `const VERSION = \"...\";` —— 不能瞎改");
|
|
287
|
+
writeFileSync(indexPath, src.replace(m, `const VERSION = "${version}";`));
|
|
288
|
+
|
|
289
|
+
const after = JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
290
|
+
const verify = /^const VERSION = "([^"]+)";$/m.exec(readFileSync(indexPath, "utf8"))?.[1];
|
|
291
|
+
if (after.version !== version || verify !== version) {
|
|
292
|
+
throw new Error(`写回后自检失败:package.json=${after.version} lib/index.js=${verify}`);
|
|
293
|
+
}
|
|
294
|
+
return { from: before, to: version };
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// ── CLI ───────────────────────────────────────────────────────────────────
|
|
298
|
+
|
|
299
|
+
function parseArgs(argv) {
|
|
300
|
+
const out = { apply: false, since: "", npmLatest: "", forcedBump: "", githubOutput: "" };
|
|
301
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
302
|
+
const a = argv[i];
|
|
303
|
+
if (a === "--apply") out.apply = true;
|
|
304
|
+
else if (a === "--since") out.since = argv[++i] ?? "";
|
|
305
|
+
else if (a.startsWith("--since=")) out.since = a.slice(8);
|
|
306
|
+
else if (a === "--npm-latest") out.npmLatest = argv[++i] ?? "";
|
|
307
|
+
else if (a.startsWith("--npm-latest=")) out.npmLatest = a.slice(13);
|
|
308
|
+
else if (a === "--bump") out.forcedBump = argv[++i] ?? "";
|
|
309
|
+
else if (a.startsWith("--bump=")) out.forcedBump = a.slice(7);
|
|
310
|
+
else if (a === "--github-output") out.githubOutput = argv[++i] ?? "";
|
|
311
|
+
else if (a.startsWith("--github-output=")) out.githubOutput = a.slice(16);
|
|
312
|
+
else if (a === "-h" || a === "--help") out.help = true;
|
|
313
|
+
else throw new Error(`不认识的参数:${a}`);
|
|
314
|
+
}
|
|
315
|
+
return out;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function main(argv) {
|
|
319
|
+
const args = parseArgs(argv);
|
|
320
|
+
if (args.help) {
|
|
321
|
+
console.log("用法: node scripts/release-plan.mjs [--since REF] [--npm-latest VER] [--bump TYPE] [--apply] [--github-output FILE]");
|
|
322
|
+
return 0;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const pkg = JSON.parse(readFileSync(join(ROOT, "package.json"), "utf8"));
|
|
326
|
+
const base = pkg.version;
|
|
327
|
+
const tag = lastTag();
|
|
328
|
+
|
|
329
|
+
// 比较起点:--since 优先;否则上一个 plugin-v tag;都没有就**不发** ——
|
|
330
|
+
// 没有起点时"从第一个提交算起"会把整个历史当成新功能,静默发一个大版本。
|
|
331
|
+
let since = args.since;
|
|
332
|
+
if (!since) {
|
|
333
|
+
if (!tag) {
|
|
334
|
+
const plan = {
|
|
335
|
+
mode: "skip", release: false, version: base, tag: `${TAG_PREFIX}${base}`, base, bump: "none", lastTag: "",
|
|
336
|
+
commits: 0, reason: "仓库里没有 plugin-v* tag,也没有 --since,无法确定哪些提交是新的",
|
|
337
|
+
notes: [], warn: ["首次发布请显式指定:--since <ref> 或 --bump <type>"], hits: [],
|
|
338
|
+
};
|
|
339
|
+
report(plan, args);
|
|
340
|
+
return 0;
|
|
341
|
+
}
|
|
342
|
+
since = tag;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const commits = readCommits(`${since}..HEAD`);
|
|
346
|
+
const plan = planRelease({
|
|
347
|
+
pkgVersion: base,
|
|
348
|
+
tag,
|
|
349
|
+
npmLatest: args.npmLatest,
|
|
350
|
+
since,
|
|
351
|
+
forcedBump: args.forcedBump,
|
|
352
|
+
commits,
|
|
353
|
+
});
|
|
354
|
+
plan.since = since;
|
|
355
|
+
plan.lastTag = tag;
|
|
356
|
+
report(plan, args);
|
|
357
|
+
|
|
358
|
+
if (plan.release && args.apply) {
|
|
359
|
+
if (plan.mode === "bump") {
|
|
360
|
+
const r = writeVersion(plan.version);
|
|
361
|
+
console.log(`✓ 已写回版本号:${r.from} → ${r.to}(package.json + lib/index.js)`);
|
|
362
|
+
} else {
|
|
363
|
+
console.log("✓ 版本号已经是目标值,无需写回");
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (args.githubOutput) {
|
|
368
|
+
const kv = {
|
|
369
|
+
release: String(plan.release),
|
|
370
|
+
mode: plan.mode,
|
|
371
|
+
version: plan.version,
|
|
372
|
+
tag: plan.tag,
|
|
373
|
+
base: plan.base,
|
|
374
|
+
bump: plan.bump,
|
|
375
|
+
commits: String(plan.commits),
|
|
376
|
+
since: plan.since ?? since,
|
|
377
|
+
};
|
|
378
|
+
appendFileSync(args.githubOutput, `${Object.entries(kv).map(([k, v]) => `${k}=${v}`).join("\n")}\n`);
|
|
379
|
+
}
|
|
380
|
+
return 0;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function report(plan, args) {
|
|
384
|
+
console.log(`▶ release-plan(仓库 ${plan.base},上次 tag ${plan.lastTag || "(无)"})`);
|
|
385
|
+
if (args.since) console.log(` 比较起点:${args.since}`);
|
|
386
|
+
if (args.npmLatest) console.log(` npm latest:${args.npmLatest}`);
|
|
387
|
+
console.log(` 提交数:${plan.commits}`);
|
|
388
|
+
for (const n of plan.notes) console.log(` · ${n}`);
|
|
389
|
+
for (const w of plan.warn) console.log(` ⚠ ${w}`);
|
|
390
|
+
|
|
391
|
+
if (!plan.release) {
|
|
392
|
+
console.log(`\n结论:不发版 —— ${plan.reason}`);
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
if (plan.mode === "publish-existing") {
|
|
396
|
+
console.log(`\n结论:发布 ${plan.version}(版本号已就位,${plan.reason})`);
|
|
397
|
+
console.log(` 演示:git tag ${plan.tag} && npm publish`);
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
console.log(`\n结论:${plan.bump} bump → ${plan.base} → ${plan.version}(tag ${plan.tag})`);
|
|
401
|
+
console.log(` 理由:${plan.reason}`);
|
|
402
|
+
if (!args.apply) console.log(" (只算不改;加 --apply 才会把版本号写进 package.json 和 lib/index.js)");
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
406
|
+
try {
|
|
407
|
+
process.exitCode = main(process.argv.slice(2));
|
|
408
|
+
} catch (err) {
|
|
409
|
+
console.error(`✗ ${err.message}`);
|
|
410
|
+
process.exitCode = 1;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
@@ -25,20 +25,14 @@ DIST="$REPO_ROOT/dist"
|
|
|
25
25
|
PUBLISH=0
|
|
26
26
|
[ "${1:-}" = "--publish" ] && PUBLISH=1
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
-czf "$DIST/$ASSET" \
|
|
37
|
-
-C "$REPO_ROOT" \
|
|
38
|
-
lib client sidecar scripts cordis.patch.yml package.json README.md LICENSE install.sh
|
|
39
|
-
|
|
40
|
-
echo " 产物: $DIST/$ASSET ($(du -h "$DIST/$ASSET" | cut -f1), $(tar -tzf "$DIST/$ASSET" | wc -l | tr -d ' ') 个条目)"
|
|
41
|
-
sha256sum "$DIST/$ASSET" 2>/dev/null || shasum -a 256 "$DIST/$ASSET"
|
|
28
|
+
# 打包逻辑与 CI 共用同一份(scripts/pack-release-tarball.sh):
|
|
29
|
+
# 手发和自动发必须打出**逐条相同**的文件清单,否则"本机发得出去、CI 发不出去"
|
|
30
|
+
# 这种不对称会一直藏着,直到某台对端的升级命令 404。
|
|
31
|
+
PACK_OUT="$(bash "$REPO_ROOT/scripts/pack-release-tarball.sh" "$DIST")"
|
|
32
|
+
ASSET_PATH="$(printf '%s\n' "$PACK_OUT" | sed -n 's/^asset=//p')"
|
|
33
|
+
[ -f "$ASSET_PATH" ] || { echo "打包失败:没有产物" >&2; exit 1; }
|
|
34
|
+
ASSET="$(basename "$ASSET_PATH")"
|
|
35
|
+
echo " sha256: $(printf '%s\n' "$PACK_OUT" | sed -n 's/^sha256=//p')"
|
|
42
36
|
|
|
43
37
|
if [ "$PUBLISH" -eq 0 ]; then
|
|
44
38
|
echo
|