release-run.mjs
8.84 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
#!/usr/bin/env node
/**
* 统一发版入口(release:run)—— ops 只跑这一条命令,且【零参数、永远不变】。
*
* pnpm release:run ← ops 固定命令(推荐,全程不变)
* pnpm release:run --version 1.3.0 ← 也可显式覆盖,但 ops 不必用
*
* 设计约束(来自团队约定):
* - ops 永远只执行同一条固定命令,不发版前判断「用哪种模式」、也不填版本号;
* - 版本号由本命令自动解析,OPS 不接触:
* * 有 releases/*.json 清单 → 取版本号最大的那份(清单模式,开发者显式声明);
* * 无清单 → 版本号驱动增量(release:diff):各子系统 package.json version
* 大于线上版本则发、否则跳过;首次线上无清单视为全量。
* - 「用不用清单」由开发者侧事实决定——releases/<version>.json 是否存在;
* - 零参数命令永远走 diff(版本号驱动增量),ops 在任何仓库状态下都能发版,永不卡住。
*
* 本脚本只做「模式分发 + 版本解析」,不重复实现发版逻辑:解析后 spawn 对应的
* scripts/release/release-{spec,affected,diff,all}.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)
- 无清单 → 版本号驱动增量(release:diff):各子系统 package.json
version > 线上则发、<= 跳过;首次线上无清单则全量
模式自动判定(零参数、ops 固定命令):
- releases/<ver>.json 存在 → 清单模式(release:spec)
- 都不存在 → 版本号驱动增量(release:diff):读各子系统 package.json
version 与线上比对,> 则发、<= 跳过;首次线上空则全量
透传选项(ops 一般不改):
--version <ver> 版本号。仅用于①选中 releases/<ver>.json 清单(spec 模式),或②无清单时
退回 git 改动分析(affected,给改动过的子系统统一打该号)。日常增量发版
不需要传——直接升对应 package.json version 跑零参数即可。diff 模式忽略此参数。
--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 也没关系——命令会自动走版本号驱动增量
(release:diff),读各 package.json version 与线上比对,> 则发、<= 跳过。
`);
}
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'; // 开发者手写了 releases/<ver>.json → 照单执行
} else {
// 无清单:版本号驱动增量(release:diff)。各子系统 package.json version
// > 线上则发、<= 则跳过;首次线上无清单 → 全部 > 空 → 全量。不再依赖 git tag
// (release:run 从不自动打 tag,依赖 tag 会退化成全量重打)。
mode = 'diff';
}
}
const child =
mode === 'spec'
? 'scripts/release/release-spec.mjs'
: mode === 'affected'
? 'scripts/release/release-affected.mjs'
: mode === 'diff'
? 'scripts/release/release-diff.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 分析兜底)'
: mode === 'diff'
? '版本号驱动增量(package.json version > 线上则发,<= 跳过)'
: '全量模式(显式 --version 且清单存在时)';
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();
}