release-run.mjs
8.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env node
/**
* 统一发版入口(release:run)—— ops 只跑这一条命令,且【零参数、永远不变】。
*
* pnpm release:run ← ops 固定命令(推荐,全程不变)
* pnpm release:run --version 1.3.0 ← 也可显式覆盖,但 ops 不必用
*
* 设计约束(来自团队约定):
* - ops 永远只执行同一条固定命令,不发版前判断「用哪种模式」、也不填版本号;
* - 版本号由本命令自动解析,OPS 不接触:
* * 有 releases/*.json 清单 → 取版本号最大的那份(清单模式,不比对 git);
* * 无清单、有 git tag → 取最新 tag 为版本,git 分析只发改动的;
* * 无清单、无 tag(首次部署)→ 全量模式,扫描全部子系统 + 主壳一次打齐。
* - 「用不用清单」由开发者侧事实决定——releases/<version>.json 是否存在;
* - 三层兜底,ops 一条零参数命令在任何仓库状态下都能发版,永不卡住。
*
* 本脚本只做「模式分发 + 版本解析」,不重复实现发版逻辑:解析后 spawn 对应的
* scripts/release/release-spec.mjs 或 scripts/release/release-affected.mjs,并把参数原样透传下去。
* 选哪条路、版本是多少,是【命令内部】唯一做的判断,OPS 完全不决策。
*/
import fs from 'node:fs';
import path from 'node:path';
import { spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { listSpecFiles, cmpVersion } from './release-spec.mjs';
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
const SPECS_DIR = path.join(ROOT, 'releases');
/** 取 --version(兼容 --version=1.3.0 与 --version 1.3.0 两种写法),无则 undefined。 */
function pickVersion(argv) {
for (let i = 0; i < argv.length; i += 1) {
const t = argv[i];
if (t === '--version') return argv[i + 1];
if (t.startsWith('--version=')) return t.slice('--version='.length);
}
return undefined;
}
function hasLatest(argv) {
return argv.includes('--latest');
}
/**
* 把开发者给的参数翻译后透传给子脚本:
* - 清单模式下把 --version <x> 翻译成 release-spec 认识的 --spec <x>;
* - 自动模式下 --version 原样保留(release:affected 需要它);
* - 丢弃 pnpm 透传的 -- 分隔符(子脚本也会跳过,这里顺手清掉避免歧义);
* - 其余参数(--base / --public-base / --out / --upload-cmd / --force / --skip-build /
* --dry-run / --verify)两种模式都能吃,原样透传。
*/
/**
* 预览类参数只有全量模式(release-all)认识;清单/自动模式没有预览阶段,
* 透传过去会因「未知参数」直接报错。这里剥离并提示,避免一条参数卡死发版。
*/
const PREVIEW_ONLY_ARGS = new Set(['--no-serve', '--no-open', '--port', '--no-verify']);
/** 这些参数各带一个取值,剥离时要连值一起跳过。 */
const ARGS_WITH_VALUE = new Set(['--port']);
function forwardArgs(argv, mode) {
const out = [];
for (let i = 0; i < argv.length; i += 1) {
const t = argv[i];
if (t === '--') continue;
if (PREVIEW_ONLY_ARGS.has(t) && mode !== 'all') {
console.log(`[release:run] 提示: ${t} 仅全量模式有效,本次(${mode} 模式)已忽略`);
if (ARGS_WITH_VALUE.has(t)) i += 1;
continue;
}
if (t === '--version' || t.startsWith('--version=')) {
if (mode === 'spec') {
if (t.startsWith('--version=')) out.push(`--spec=${t.slice('--version='.length)}`);
else {
out.push('--spec', argv[i + 1]);
i += 1;
}
} else {
out.push(t);
}
continue;
}
out.push(t);
}
return out;
}
function printHelp() {
console.log(`
release:run —— 统一发版入口(ops 只跑这一条、零参数、永远不变)
用法:
pnpm release:run ← ops 固定命令(推荐,全程不变)
pnpm release:run --version <ver> ← 也可显式指定版本覆盖自动解析(ops 不必用)
版本号自动解析(ops 不接触):
- releases/ 下有清单 → 取版本号最大的那份(清单模式,不比对 git)
- 无清单、有 git tag → 取最新 tag 为版本,git 分析只发改动的(自动模式)
- 无清单、无 tag(首次部署) → 全量模式:扫描全部子系统 + 主壳一次打齐
模式自动判定(三层兜底,ops 永不卡住):
- releases/<ver>.json 存在 → 清单模式(release:spec,不比对 git)
- 不存在但有 tag → 自动模式(release:affected,git 分析只发改动的)
- 都没有 → 全量模式(release:all,首次部署兜底)
透传选项(两种模式通用,ops 也一般不改):
--version <ver> 版本号(可选覆盖;即清单文件名 / 自动模式的 git tag 版本)。
--base <ref> 自动模式的 git 基线,默认 origin/master。
--public-base <u> 产物公网基址(透传)。
--out <dir> 产物目录,默认 dist。镜像部署根、每次发布重建,整棵树覆盖进部署目录即生效。
--state <dir> manifest 累积状态目录,默认 .release-state(或 MFE_STATE_DIR)。
--upload-cmd <c> 上传钩子({dir} 即产物根,一个变量覆盖部署根);也可用 MFE_UPLOAD_CMD。
--force 覆盖已存在版本目录 / 覆盖已有清单。
--skip-build 复用已有 dist,不重新构建。
--verify 清单模式下做 git 交叉校验(不符仅告警不阻断)。
--dry-run 只打印将执行的动作,不构建、不落盘、不上传。
--help 显示本帮助
开发者侧:发版前跑 pnpm release:gen --version <ver> 生成清单(或手写 releases/<ver>.json),
ops 再跑固定的 pnpm release:run 即可。忘了 gen 也没关系——命令会自动降级到 git 分析模式。
`);
}
/** 无清单时,从最新 git tag 推断版本号(去掉前缀 v/V);拿不到返回 null(由调用方决定兜底方式)。 */
function tryVersionFromGit() {
const res = spawnSync('git', ['describe', '--tags', '--abbrev=0'], { cwd: ROOT });
if (res.status === 0 && res.stdout && res.stdout.trim()) {
return res.stdout.trim().replace(/^v/i, '');
}
return null;
}
function main() {
const argv = process.argv.slice(2);
if (argv.includes('--help')) {
printHelp();
return;
}
const version = pickVersion(argv);
const latest = hasLatest(argv);
let mode;
let resolvedVersion = version;
if (latest || version) {
// 显式指定:--latest 必走清单模式;给了 --version 则看清单在不在
if (latest) {
mode = 'spec';
} else {
const specFile = path.join(SPECS_DIR, `${version}.json`);
mode = fs.existsSync(specFile) ? 'spec' : 'affected';
}
} else {
// 零参数(OPS 固定命令):自动判定模式与版本,三层兜底,永不 die 于「没清单」
const specs = listSpecFiles().sort((a, b) => cmpVersion(a.version, b.version));
if (specs.length > 0) {
mode = 'spec'; // 下游用 --latest 取最大版本
} else {
const tagVersion = tryVersionFromGit();
if (tagVersion) {
mode = 'affected';
resolvedVersion = tagVersion;
} else {
// 第三层兜底:无清单、无 tag —— 典型是首次部署(仓库刚建、还没人 gen 过)。
// 全量扫描 workspace,所有子系统 + 主壳一次打齐,保证 ops 一条命令永不卡住。
mode = 'all';
}
}
}
const child =
mode === 'spec'
? 'scripts/release/release-spec.mjs'
: mode === 'affected'
? 'scripts/release/release-affected.mjs'
: 'scripts/release/release-all.mjs';
const childArgs = forwardArgs(argv, mode);
// 零参数场景下补齐分发所需的版本/--latest 参数(all 模式版本号可省,默认 1.0.0)
if (!version && !latest) {
if (mode === 'spec') childArgs.push('--latest');
else if (mode === 'affected') childArgs.push('--version', resolvedVersion);
}
const label =
mode === 'spec'
? latest || !version
? '清单模式(releases/ 最新版本,自动选取)'
: `清单模式(releases/${version}.json)`
: mode === 'affected'
? '自动模式(git 分析兜底)'
: '全量模式(无清单、无 git tag —— 首次部署兜底,扫描全部子系统 + 主壳)';
console.log(`[release:run] 模式: ${label}`);
if (mode === 'affected' && !version) {
console.log(`[release:run] 自动版本: ${resolvedVersion}(取自最新 git tag)`);
}
const res = spawnSync('node', [path.join(ROOT, child), ...childArgs], {
cwd: ROOT,
stdio: 'inherit',
});
process.exit(res.status ?? 1);
}
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
main();
}