@infly/libs 2.2.11 → 2.2.13
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 +8 -3
- package/adapters/webpack/auto-export.js +52 -52
- package/build/build-utils/command.js +57 -57
- package/build/build-utils/deps.js +28 -28
- package/build/build-utils/git.js +254 -254
- package/build/build-utils/preview.js +78 -78
- package/build/build-utils/webhook-single.js +136 -136
- package/build/docker/compose-release.js +79 -79
- package/build/docker/target-release.js +125 -14
- package/build/project-files.js +47 -47
- package/core/async/concurrency.js +29 -29
- package/package.json +1 -1
- package/script/git-automation/git-utils.js +59 -59
- package/script/git-automation/index.js +72 -2
- package/script/git-automation/submodule-utils.js +16 -16
- package/workspace/config.js +144 -144
|
@@ -107,12 +107,102 @@ function trimPathInput(value) {
|
|
|
107
107
|
: trimmed;
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
|
|
110
|
+
const SAFE_GIT_NAME = /^[A-Za-z0-9][A-Za-z0-9._/-]*$/;
|
|
111
|
+
const SAFE_DOCKER_PLATFORM = /^[a-z0-9]+\/[a-z0-9]+(?:\/[a-z0-9._-]+)?$/;
|
|
112
|
+
const SAFE_VERSION_REQUIREMENT = /^>=(\d+)(?:\.(\d+))?(?:\.(\d+))?$/;
|
|
113
|
+
|
|
114
|
+
function validateReleaseProfile(profile, target) {
|
|
115
|
+
if (profile.pushBranch != null && (typeof profile.pushBranch !== "string"
|
|
116
|
+
|| !SAFE_GIT_NAME.test(profile.pushBranch) || profile.pushBranch.startsWith("-")
|
|
117
|
+
|| profile.pushBranch.includes(".."))) {
|
|
118
|
+
throw new Error(`${target} pushBranch 必须是合法 Git 分支名`);
|
|
119
|
+
}
|
|
120
|
+
if (profile.pushRef != null && (typeof profile.pushRef !== "string"
|
|
121
|
+
|| !SAFE_GIT_NAME.test(profile.pushRef) || profile.pushRef.startsWith("-")
|
|
122
|
+
|| profile.pushRef.includes("..") || !profile.pushRef.includes("/"))) {
|
|
123
|
+
throw new Error(`${target} pushRef 必须使用 remote/branch 格式`);
|
|
124
|
+
}
|
|
125
|
+
if ((profile.pushBranch == null) !== (profile.pushRef == null)) {
|
|
126
|
+
throw new Error(`${target} pushBranch 和 pushRef 必须同时配置`);
|
|
127
|
+
}
|
|
128
|
+
if (profile.requirements != null) {
|
|
129
|
+
if (!profile.requirements || typeof profile.requirements !== "object"
|
|
130
|
+
|| Array.isArray(profile.requirements)) throw new Error(`${target} requirements 必须是对象`);
|
|
131
|
+
for (const [tool, requirement] of Object.entries(profile.requirements)) {
|
|
132
|
+
if (!["node", "pnpm"].includes(tool) || typeof requirement !== "string"
|
|
133
|
+
|| !SAFE_VERSION_REQUIREMENT.test(requirement)) {
|
|
134
|
+
throw new Error(`${target} requirements 只支持 node/pnpm 的 >=x.y.z 版本约束`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (profile.preBuild != null && (!Array.isArray(profile.preBuild) || profile.preBuild.length === 0
|
|
139
|
+
|| profile.preBuild.some((command) => !Array.isArray(command) || command.length === 0
|
|
140
|
+
|| command.some((arg) => typeof arg !== "string" || !arg || /[\r\n\0]/.test(arg))))) {
|
|
141
|
+
throw new Error(`${target} preBuild 必须是非空命令参数数组列表`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function compareVersions(actual, required) {
|
|
146
|
+
const actualMatch = String(actual).trim().replace(/^v/, "").match(/^(\d+)(?:\.(\d+))?(?:\.(\d+))?/);
|
|
147
|
+
const requiredMatch = String(required).match(SAFE_VERSION_REQUIREMENT);
|
|
148
|
+
if (!actualMatch || !requiredMatch) return false;
|
|
149
|
+
for (let index = 1; index <= 3; index++) {
|
|
150
|
+
const left = Number(actualMatch[index] || 0);
|
|
151
|
+
const right = Number(requiredMatch[index] || 0);
|
|
152
|
+
if (left !== right) return left > right;
|
|
153
|
+
}
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function windowsExecutable(command, platform = process.platform) {
|
|
158
|
+
return platform === "win32" && ["npm", "npx", "pnpm", "yarn"].includes(command)
|
|
159
|
+
? `${command}.cmd`
|
|
160
|
+
: command;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function runReleasePreparation(profile, { run, capture, projectDir, push, platform = process.platform, log = console.log }) {
|
|
164
|
+
validateReleaseProfile(profile, profile.label || "Docker target");
|
|
165
|
+
const git = (...args) => capture("git", ["-C", projectDir, ...args]);
|
|
166
|
+
if (git("status", "--porcelain", "--untracked-files=all")) {
|
|
167
|
+
throw new Error("当前项目存在未提交改动,不能构建发布镜像");
|
|
168
|
+
}
|
|
169
|
+
if (push && profile.pushBranch) {
|
|
170
|
+
const branch = git("branch", "--show-current");
|
|
171
|
+
if (branch !== profile.pushBranch) {
|
|
172
|
+
throw new Error(`镜像只允许从 ${profile.pushBranch} 推送;当前分支: ${branch || "detached HEAD"}`);
|
|
173
|
+
}
|
|
174
|
+
const slash = profile.pushRef.indexOf("/");
|
|
175
|
+
const remote = profile.pushRef.slice(0, slash);
|
|
176
|
+
const remoteBranch = profile.pushRef.slice(slash + 1);
|
|
177
|
+
run("git", ["-C", projectDir, "fetch", remote, remoteBranch], { cwd: projectDir });
|
|
178
|
+
const commit = git("rev-parse", "HEAD").toLowerCase();
|
|
179
|
+
const remoteCommit = git("rev-parse", `${profile.pushRef}^{commit}`).toLowerCase();
|
|
180
|
+
if (commit !== remoteCommit) {
|
|
181
|
+
throw new Error(`${profile.pushBranch} 必须已推送并与 ${profile.pushRef} 同步`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
for (const [tool, requirement] of Object.entries(profile.requirements || {})) {
|
|
185
|
+
const command = windowsExecutable(tool, platform);
|
|
186
|
+
const args = tool === "node" ? ["--version"] : ["--version"];
|
|
187
|
+
const actual = capture(command, args);
|
|
188
|
+
if (!compareVersions(actual, requirement)) {
|
|
189
|
+
throw new Error(`${tool} 必须满足 ${requirement},当前版本: ${actual || "无法识别"}`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
for (const [command, ...args] of profile.preBuild || []) {
|
|
193
|
+
log(`执行发布准备: ${command} ${args.join(" ")}`);
|
|
194
|
+
run(windowsExecutable(command, platform), args, { cwd: projectDir });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function validateComponent(component, name, guardedByProfile = false) {
|
|
111
199
|
if (!component || !/^[a-z0-9][a-z0-9._:/-]*$/.test(component.repository || "") || component.repository.includes("@")) {
|
|
112
200
|
throw new Error(`${name} 必须配置有效的镜像 repository(不含标签)`);
|
|
113
201
|
}
|
|
114
202
|
if (component.repository.split("/").at(-1).includes(":")) throw new Error(`${name} repository 不得包含标签`);
|
|
115
|
-
if (!component.expectedRef && !component.source?.branch
|
|
203
|
+
if (!component.expectedRef && !component.source?.branch && !guardedByProfile) {
|
|
204
|
+
throw new Error(`${name} 必须配置 expectedRef、source.branch 或 target pushRef`);
|
|
205
|
+
}
|
|
116
206
|
if (component.source) {
|
|
117
207
|
normalizeSourceDirectories(component.source.directory, name);
|
|
118
208
|
if (!component.source.branch || component.source.branch.startsWith("-")) {
|
|
@@ -124,6 +214,13 @@ function validateComponent(component, name) {
|
|
|
124
214
|
if (component.target != null && (typeof component.target !== "string" || !/^[a-zA-Z0-9_.-]+$/.test(component.target))) {
|
|
125
215
|
throw new Error(`${name} target 必须是合法 Docker stage`);
|
|
126
216
|
}
|
|
217
|
+
if (component.platform != null && (typeof component.platform !== "string"
|
|
218
|
+
|| !SAFE_DOCKER_PLATFORM.test(component.platform))) {
|
|
219
|
+
throw new Error(`${name} platform 必须是 linux/amd64 等合法 Docker 平台`);
|
|
220
|
+
}
|
|
221
|
+
if (component.mutableTags != null && typeof component.mutableTags !== "boolean") {
|
|
222
|
+
throw new Error(`${name} mutableTags 必须是布尔值`);
|
|
223
|
+
}
|
|
127
224
|
if (component.buildArgs != null && (!component.buildArgs || typeof component.buildArgs !== "object"
|
|
128
225
|
|| Array.isArray(component.buildArgs) || Object.entries(component.buildArgs).some(([key, value]) =>
|
|
129
226
|
!/^[A-Z_][A-Z0-9_]*$/.test(key) || !["string", "number", "boolean"].includes(typeof value)))) {
|
|
@@ -178,6 +275,7 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
178
275
|
bump: "none", push: Boolean(options.push), dryRun: Boolean(options.dryRun) });
|
|
179
276
|
}
|
|
180
277
|
if (!profile?.components) throw new Error(`平台 ${target} 未配置 components`);
|
|
278
|
+
validateReleaseProfile(profile, target);
|
|
181
279
|
const components = options.components || await (dependencies.selectOptions || selectOptions)("请选择构建范围",
|
|
182
280
|
Object.entries(profile.components).map(([value, item]) => ({ value, label: item.label || value })));
|
|
183
281
|
if (components === EXIT_SELECTION) return { cancelled: true };
|
|
@@ -188,7 +286,7 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
188
286
|
const plan = components.map((name) => {
|
|
189
287
|
if (!Object.hasOwn(profile.components, name)) throw new Error(`不支持的构建范围: ${name}`);
|
|
190
288
|
const component = profile.components[name];
|
|
191
|
-
validateComponent(component, name);
|
|
289
|
+
validateComponent(component, name, Boolean(profile.pushRef));
|
|
192
290
|
if (!component.source) return { name, ...component };
|
|
193
291
|
const configuredDirectory = name === "backend" && options.backendDir ? options.backendDir : component.source.directory;
|
|
194
292
|
const directoryCandidates = resolveSourceDirectories(projectDir, configuredDirectory, name);
|
|
@@ -197,8 +295,6 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
197
295
|
});
|
|
198
296
|
// dry-run 只解析配置,不联网、不调用 Git/Docker、不创建临时目录。
|
|
199
297
|
if (options.dryRun) { log(JSON.stringify({ target, push: Boolean(options.push), plan }, null, 2)); return { target, plan }; }
|
|
200
|
-
const run = dependencies.run || execute;
|
|
201
|
-
const capture = (command, args) => String(run(command, args, { cwd: projectDir, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }) || "").trim();
|
|
202
298
|
// 全部组件先预检,数组按顺序采用第一个 Git 仓;交互终端允许补录机器本地路径。
|
|
203
299
|
const stdin = dependencies.stdin || process.stdin;
|
|
204
300
|
const stdout = dependencies.stdout || process.stdout;
|
|
@@ -252,6 +348,12 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
252
348
|
}
|
|
253
349
|
return { target, missingSources: missing.map((item) => item.name) };
|
|
254
350
|
}
|
|
351
|
+
const run = dependencies.run || execute;
|
|
352
|
+
const capture = (command, args) => String(run(command, args, { cwd: projectDir, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }) || "").trim();
|
|
353
|
+
if (profile.pushRef || profile.requirements || profile.preBuild) {
|
|
354
|
+
runReleasePreparation(profile, { run, capture, projectDir, push: Boolean(options.push),
|
|
355
|
+
platform: dependencies.platform, log });
|
|
356
|
+
}
|
|
255
357
|
await checkDocker(run, projectDir, true, dependencies);
|
|
256
358
|
const results = [];
|
|
257
359
|
for (const item of plan) {
|
|
@@ -271,7 +373,8 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
271
373
|
if (git("status", "--porcelain", "--untracked-files=all")) throw new Error(`${item.name} 仓库存在未提交改动`);
|
|
272
374
|
const commit = git("rev-parse", "HEAD").toLowerCase();
|
|
273
375
|
const expectedRef = item.source ? `origin/${item.source.branch}` : item.expectedRef;
|
|
274
|
-
if (!/^[a-f0-9]{40}$/.test(commit) ||
|
|
376
|
+
if (!/^[a-f0-9]{40}$/.test(commit) || (expectedRef
|
|
377
|
+
&& commit !== git("rev-parse", `${expectedRef}^{commit}`).toLowerCase())) {
|
|
275
378
|
throw new Error(`${item.name} HEAD 与 ${expectedRef} 不一致,请先核对并 fetch 对应仓库`);
|
|
276
379
|
}
|
|
277
380
|
if (item.source) {
|
|
@@ -298,16 +401,18 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
298
401
|
}
|
|
299
402
|
if (!io.existsSync(dockerfile)) throw new Error(`缺少 Dockerfile: ${dockerfile}`);
|
|
300
403
|
const image = `${item.repository}:${commit}`;
|
|
301
|
-
const latestImage = `${item.repository}:latest`;
|
|
404
|
+
const latestImage = item.mutableTags === false ? null : `${item.repository}:latest`;
|
|
302
405
|
// --load 只把当前构建节点平台的镜像载入本地镜像存储;发布流程直接使用单平台镜像。
|
|
303
406
|
const buildOptions = ["buildx", "build", "--load", "--file", dockerfile];
|
|
407
|
+
if (item.platform) buildOptions.push("--platform", item.platform);
|
|
304
408
|
if (item.target) buildOptions.push("--target", item.target);
|
|
305
409
|
for (const [key, value] of Object.entries(item.buildArgs || {})) {
|
|
306
410
|
buildOptions.push("--build-arg", `${key}=${value}`);
|
|
307
411
|
}
|
|
308
412
|
run("docker", [...buildOptions,
|
|
309
413
|
"--label", `org.opencontainers.image.revision=${commit}`, "--label", `org.opencontainers.image.version=${commit}`,
|
|
310
|
-
"--label", `com.infly.platform=${target}`, "--tag", image,
|
|
414
|
+
"--label", `com.infly.platform=${target}`, "--tag", image,
|
|
415
|
+
...(latestImage ? ["--tag", latestImage] : []), context], { cwd: projectDir });
|
|
311
416
|
if (capture("docker", ["image", "inspect", "--format", '{{ index .Config.Labels "org.opencontainers.image.revision" }}', image]) !== commit) {
|
|
312
417
|
throw new Error(`${item.name} 镜像 revision 校验失败`);
|
|
313
418
|
}
|
|
@@ -317,11 +422,11 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
317
422
|
}
|
|
318
423
|
if (options.push) {
|
|
319
424
|
run("docker", ["push", image], { cwd: projectDir });
|
|
320
|
-
run("docker", ["push", latestImage], { cwd: projectDir });
|
|
425
|
+
if (latestImage) run("docker", ["push", latestImage], { cwd: projectDir });
|
|
321
426
|
}
|
|
322
427
|
log(`${item.name}_commit=${commit}`);
|
|
323
428
|
log(`${item.name}=${image}`);
|
|
324
|
-
log(`${item.name}_latest=${latestImage}`);
|
|
429
|
+
if (latestImage) log(`${item.name}_latest=${latestImage}`);
|
|
325
430
|
results.push({ component: item.name, commit, image, latestImage, pushed: Boolean(options.push) });
|
|
326
431
|
} finally {
|
|
327
432
|
// 只移除本次创建的隔离 worktree;清理失败保留目录供人工恢复,绝不清空开发工作区。
|
|
@@ -334,10 +439,15 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
334
439
|
// 构建时已逐组件打印 frontend_commit= 等机器行,供失败前人工留档;这里汇总为可直接复制到
|
|
335
440
|
// 输出同时面向本地核对与 Jenkins 下拉部署,避免再手工复制提交参数。
|
|
336
441
|
if (results.length) {
|
|
442
|
+
const usesMutableTags = results.some((item) => item.latestImage);
|
|
337
443
|
const pushNote = options.push
|
|
338
444
|
? profile.deployment?.ready === true
|
|
339
|
-
?
|
|
340
|
-
|
|
445
|
+
? usesMutableTags
|
|
446
|
+
? "已推送不可变提交标签和 latest 最新推送标签,可直接在 Jenkins 选择部署"
|
|
447
|
+
: "已推送不可变提交标签,可直接在 Jenkins 选择部署"
|
|
448
|
+
: usesMutableTags
|
|
449
|
+
? "已推送不可变提交标签和 latest 最新推送标签;该平台尚未开放 Jenkins 部署"
|
|
450
|
+
: "已推送不可变提交标签;该平台尚未开放 Jenkins 部署"
|
|
341
451
|
: "仅本地生成,未推送镜像";
|
|
342
452
|
log("");
|
|
343
453
|
log("\x1b[0;34m==========================================");
|
|
@@ -346,7 +456,7 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
346
456
|
log(`可用标签(${pushNote}):`);
|
|
347
457
|
for (const r of results) {
|
|
348
458
|
log(` ${r.component}=${r.image} (不可变提交版本)`);
|
|
349
|
-
log(` ${r.component}_latest=${r.latestImage} (最新推送镜像,即所选平台 registry 的 latest)`);
|
|
459
|
+
if (r.latestImage) log(` ${r.component}_latest=${r.latestImage} (最新推送镜像,即所选平台 registry 的 latest)`);
|
|
350
460
|
}
|
|
351
461
|
if (cicdLink && profile.deployment?.ready === true) {
|
|
352
462
|
log("");
|
|
@@ -364,4 +474,5 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
364
474
|
return { target, results };
|
|
365
475
|
}
|
|
366
476
|
|
|
367
|
-
module.exports = { parseReleaseArgs, runTargetRelease
|
|
477
|
+
module.exports = { compareVersions, parseReleaseArgs, runReleasePreparation, runTargetRelease,
|
|
478
|
+
validateReleaseProfile, windowsExecutable };
|
package/build/project-files.js
CHANGED
|
@@ -3,53 +3,53 @@ const path = require("path");
|
|
|
3
3
|
|
|
4
4
|
// 兼容旧 tools/file-process.js 的 __dirname 路径语义。
|
|
5
5
|
const legacyToolsDir = path.resolve(__dirname, "../tools");
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 覆盖写入构建脚本
|
|
9
|
-
* @param {String} filePath - 覆盖文件路径
|
|
10
|
-
* @param {Array | Object} scriptsList - 写入脚本配置
|
|
11
|
-
* @param {String} 更新字段值
|
|
12
|
-
*/
|
|
13
|
-
function scriptWrite(filePath, scriptsList, updateKey = "scripts") {
|
|
14
|
-
const pkg = JSON.parse(fs.readFileSync(filePath));
|
|
15
|
-
pkg[updateKey] = pkg[updateKey] || {};
|
|
16
|
-
if (Array.isArray(scriptsList)) {
|
|
17
|
-
scriptsList.forEach(item => {
|
|
18
|
-
if (pkg[updateKey][item.key]) {
|
|
19
|
-
pkg[updateKey][`origin:${item.key}`] = pkg[updateKey][item.key];
|
|
20
|
-
}
|
|
21
|
-
pkg[updateKey][item.key] = item.value;
|
|
22
|
-
});
|
|
23
|
-
} else if (typeof scriptsList === "object") {
|
|
24
|
-
pkg[updateKey] = scriptsList;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
fs.writeFileSync(filePath, JSON.stringify(pkg, null, 2));
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 获取文件绝对路径
|
|
32
|
-
* @param {String} filePath - 文件路径
|
|
33
|
-
* @returns
|
|
34
|
-
*/
|
|
35
|
-
function targetProjectFileResolve(filePath) {
|
|
36
|
-
// 初始脚本自动执行路径会自动读取node_modules
|
|
37
|
-
const projectRoot = process.cwd();
|
|
38
|
-
const absoluteProjectRoot = projectRoot.replace(/\\node_modules.*$/g, "");
|
|
39
|
-
return path.resolve(absoluteProjectRoot, filePath);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* 获取文件绝对路径
|
|
44
|
-
* @param {String} filePath - 文件路径
|
|
45
|
-
* @returns
|
|
46
|
-
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 覆盖写入构建脚本
|
|
9
|
+
* @param {String} filePath - 覆盖文件路径
|
|
10
|
+
* @param {Array | Object} scriptsList - 写入脚本配置
|
|
11
|
+
* @param {String} 更新字段值
|
|
12
|
+
*/
|
|
13
|
+
function scriptWrite(filePath, scriptsList, updateKey = "scripts") {
|
|
14
|
+
const pkg = JSON.parse(fs.readFileSync(filePath));
|
|
15
|
+
pkg[updateKey] = pkg[updateKey] || {};
|
|
16
|
+
if (Array.isArray(scriptsList)) {
|
|
17
|
+
scriptsList.forEach(item => {
|
|
18
|
+
if (pkg[updateKey][item.key]) {
|
|
19
|
+
pkg[updateKey][`origin:${item.key}`] = pkg[updateKey][item.key];
|
|
20
|
+
}
|
|
21
|
+
pkg[updateKey][item.key] = item.value;
|
|
22
|
+
});
|
|
23
|
+
} else if (typeof scriptsList === "object") {
|
|
24
|
+
pkg[updateKey] = scriptsList;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
fs.writeFileSync(filePath, JSON.stringify(pkg, null, 2));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 获取文件绝对路径
|
|
32
|
+
* @param {String} filePath - 文件路径
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
function targetProjectFileResolve(filePath) {
|
|
36
|
+
// 初始脚本自动执行路径会自动读取node_modules
|
|
37
|
+
const projectRoot = process.cwd();
|
|
38
|
+
const absoluteProjectRoot = projectRoot.replace(/\\node_modules.*$/g, "");
|
|
39
|
+
return path.resolve(absoluteProjectRoot, filePath);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 获取文件绝对路径
|
|
44
|
+
* @param {String} filePath - 文件路径
|
|
45
|
+
* @returns
|
|
46
|
+
*/
|
|
47
47
|
function currentProjectFileResolve(filePath) {
|
|
48
48
|
return path.join(legacyToolsDir, filePath);
|
|
49
49
|
}
|
|
50
|
-
|
|
51
|
-
module.exports = {
|
|
52
|
-
scriptWrite,
|
|
53
|
-
targetProjectFileResolve,
|
|
54
|
-
currentProjectFileResolve
|
|
55
|
-
};
|
|
50
|
+
|
|
51
|
+
module.exports = {
|
|
52
|
+
scriptWrite,
|
|
53
|
+
targetProjectFileResolve,
|
|
54
|
+
currentProjectFileResolve
|
|
55
|
+
};
|
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 框架无关的异步并发控制。
|
|
3
|
-
* 异步并发限流器。
|
|
4
|
-
* 对 items 数组中的每一项执行 worker,同时最多运行 concurrency 个 worker。
|
|
5
|
-
*
|
|
6
|
-
* @param {Array} items - 待处理项
|
|
7
|
-
* @param {number} concurrency - 最大并发数
|
|
8
|
-
* @param {Function} worker - 处理函数 (item, index) => Promise
|
|
9
|
-
* @returns {Promise<Array>} 结果数组,索引与 items 一一对应
|
|
10
|
-
*/
|
|
11
|
-
async function runWithConcurrency(items, concurrency, worker) {
|
|
12
|
-
if (!Number.isInteger(concurrency) || concurrency < 1) {
|
|
13
|
-
throw new TypeError("concurrency must be a positive integer");
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const results = new Array(items.length);
|
|
17
|
-
let nextIndex = 0;
|
|
18
|
-
const runWorker = async () => {
|
|
19
|
-
while (nextIndex < items.length) {
|
|
20
|
-
const index = nextIndex;
|
|
21
|
-
nextIndex += 1;
|
|
22
|
-
results[index] = await worker(items[index], index);
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const workerCount = Math.min(concurrency, items.length);
|
|
27
|
-
await Promise.all(Array.from({ length: workerCount }, runWorker));
|
|
28
|
-
return results;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
module.exports = { runWithConcurrency };
|
|
3
|
+
* 异步并发限流器。
|
|
4
|
+
* 对 items 数组中的每一项执行 worker,同时最多运行 concurrency 个 worker。
|
|
5
|
+
*
|
|
6
|
+
* @param {Array} items - 待处理项
|
|
7
|
+
* @param {number} concurrency - 最大并发数
|
|
8
|
+
* @param {Function} worker - 处理函数 (item, index) => Promise
|
|
9
|
+
* @returns {Promise<Array>} 结果数组,索引与 items 一一对应
|
|
10
|
+
*/
|
|
11
|
+
async function runWithConcurrency(items, concurrency, worker) {
|
|
12
|
+
if (!Number.isInteger(concurrency) || concurrency < 1) {
|
|
13
|
+
throw new TypeError("concurrency must be a positive integer");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const results = new Array(items.length);
|
|
17
|
+
let nextIndex = 0;
|
|
18
|
+
const runWorker = async () => {
|
|
19
|
+
while (nextIndex < items.length) {
|
|
20
|
+
const index = nextIndex;
|
|
21
|
+
nextIndex += 1;
|
|
22
|
+
results[index] = await worker(items[index], index);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const workerCount = Math.min(concurrency, items.length);
|
|
27
|
+
await Promise.all(Array.from({ length: workerCount }, runWorker));
|
|
28
|
+
return results;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = { runWithConcurrency };
|
package/package.json
CHANGED
|
@@ -1,63 +1,63 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const path = require("path");
|
|
3
|
-
const { execFileSync } = require("child_process");
|
|
4
|
-
|
|
5
|
-
function gitOutput(args, cwd = process.cwd()) {
|
|
6
|
-
return execFileSync("git", args, {
|
|
7
|
-
cwd,
|
|
8
|
-
encoding: "utf8",
|
|
9
|
-
shell: process.platform === "win32"
|
|
10
|
-
}).trim();
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function findGitRepo(cwd) {
|
|
14
|
-
let currentDir = path.resolve(cwd || process.cwd());
|
|
15
|
-
|
|
16
|
-
while (true) {
|
|
17
|
-
const gitPath = path.join(currentDir, ".git");
|
|
18
|
-
|
|
19
|
-
if (fs.existsSync(gitPath)) {
|
|
20
|
-
const stat = fs.statSync(gitPath);
|
|
21
|
-
const repo = {
|
|
22
|
-
root: currentDir,
|
|
23
|
-
commonDir: gitPath
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
if (stat.isFile()) {
|
|
27
|
-
const content = fs.readFileSync(gitPath, "utf8");
|
|
28
|
-
const match = content.match(/gitdir:\s*(.+)/i);
|
|
29
|
-
|
|
30
|
-
if (match) {
|
|
31
|
-
repo.commonDir = path.resolve(currentDir, match[1].trim());
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return repo;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const parentDir = path.dirname(currentDir);
|
|
39
|
-
|
|
40
|
-
if (parentDir === currentDir) {
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
currentDir = parentDir;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const { execFileSync } = require("child_process");
|
|
4
|
+
|
|
5
|
+
function gitOutput(args, cwd = process.cwd()) {
|
|
6
|
+
return execFileSync("git", args, {
|
|
7
|
+
cwd,
|
|
8
|
+
encoding: "utf8",
|
|
9
|
+
shell: process.platform === "win32"
|
|
10
|
+
}).trim();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function findGitRepo(cwd) {
|
|
14
|
+
let currentDir = path.resolve(cwd || process.cwd());
|
|
15
|
+
|
|
16
|
+
while (true) {
|
|
17
|
+
const gitPath = path.join(currentDir, ".git");
|
|
18
|
+
|
|
19
|
+
if (fs.existsSync(gitPath)) {
|
|
20
|
+
const stat = fs.statSync(gitPath);
|
|
21
|
+
const repo = {
|
|
22
|
+
root: currentDir,
|
|
23
|
+
commonDir: gitPath
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
if (stat.isFile()) {
|
|
27
|
+
const content = fs.readFileSync(gitPath, "utf8");
|
|
28
|
+
const match = content.match(/gitdir:\s*(.+)/i);
|
|
29
|
+
|
|
30
|
+
if (match) {
|
|
31
|
+
repo.commonDir = path.resolve(currentDir, match[1].trim());
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return repo;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const parentDir = path.dirname(currentDir);
|
|
39
|
+
|
|
40
|
+
if (parentDir === currentDir) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
currentDir = parentDir;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
48
|
function getGitCommonDir(cwd) {
|
|
49
|
-
const repo = findGitRepo(cwd);
|
|
50
|
-
|
|
51
|
-
if (repo) {
|
|
52
|
-
return repo.commonDir;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
try {
|
|
56
|
-
const commonDir = gitOutput(["-C", cwd, "rev-parse", "--git-common-dir"]);
|
|
57
|
-
return path.resolve(cwd, commonDir);
|
|
58
|
-
} catch {
|
|
59
|
-
return path.resolve(cwd || process.cwd());
|
|
60
|
-
}
|
|
49
|
+
const repo = findGitRepo(cwd);
|
|
50
|
+
|
|
51
|
+
if (repo) {
|
|
52
|
+
return repo.commonDir;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const commonDir = gitOutput(["-C", cwd, "rev-parse", "--git-common-dir"]);
|
|
57
|
+
return path.resolve(cwd, commonDir);
|
|
58
|
+
} catch {
|
|
59
|
+
return path.resolve(cwd || process.cwd());
|
|
60
|
+
}
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
function resolveGitWorktreeRoot(cwd) {
|
|
@@ -67,6 +67,72 @@ function createCommitTextMatcher(template = "") {
|
|
|
67
67
|
return new RegExp(`^${escapedParts.join(".+?")}$`);
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
function isAncestorCommit(ancestor, descendant, cwd) {
|
|
71
|
+
try {
|
|
72
|
+
execSync(`git merge-base --is-ancestor ${ancestor} ${descendant}`, { cwd, stdio: "ignore" });
|
|
73
|
+
return true;
|
|
74
|
+
} catch {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 将侧分支上的版本提交映射到它首次进入当前分支第一父链的合并提交。
|
|
81
|
+
* 否则 `versionCommit..HEAD` 会把当前分支上与侧分支互不为祖先的大量旧提交误算为本次更新。
|
|
82
|
+
*/
|
|
83
|
+
function resolveBuildSummaryBaseline(versionCommit, cwd) {
|
|
84
|
+
if (!/^[0-9a-f]{40}$/i.test(versionCommit)) return versionCommit;
|
|
85
|
+
|
|
86
|
+
const firstParentHistory = execSync("git rev-list --first-parent HEAD", { cwd, encoding: "utf-8" })
|
|
87
|
+
.trim()
|
|
88
|
+
.split(/\r?\n/)
|
|
89
|
+
.filter(Boolean);
|
|
90
|
+
|
|
91
|
+
if (firstParentHistory.includes(versionCommit)) return versionCommit;
|
|
92
|
+
|
|
93
|
+
// 第一父链从新到旧排列;“包含版本提交”在链上形成连续前缀,可以二分找到最早引入点。
|
|
94
|
+
let low = 0;
|
|
95
|
+
let high = firstParentHistory.length - 1;
|
|
96
|
+
let oldestContainingIndex = -1;
|
|
97
|
+
while (low <= high) {
|
|
98
|
+
const middle = Math.floor((low + high) / 2);
|
|
99
|
+
if (isAncestorCommit(versionCommit, firstParentHistory[middle], cwd)) {
|
|
100
|
+
oldestContainingIndex = middle;
|
|
101
|
+
low = middle + 1;
|
|
102
|
+
} else {
|
|
103
|
+
high = middle - 1;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return oldestContainingIndex >= 0 ? firstParentHistory[oldestContainingIndex] : versionCommit;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function getCommitSubjectKey(subject) {
|
|
111
|
+
const workItem = subject.match(/#[a-z][a-z0-9]*-\d+\b/i)?.[0];
|
|
112
|
+
if (workItem) return `work-item:${workItem.toUpperCase()}`;
|
|
113
|
+
|
|
114
|
+
return subject
|
|
115
|
+
.trim()
|
|
116
|
+
.replace(/\s+/g, " ")
|
|
117
|
+
.replace(/\s+([,.;:!?#])/g, "$1")
|
|
118
|
+
.toLowerCase();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function deduplicateCommitSubjects(subjects) {
|
|
122
|
+
const seen = new Set();
|
|
123
|
+
return subjects.filter((subject) => {
|
|
124
|
+
const key = getCommitSubjectKey(subject);
|
|
125
|
+
if (seen.has(key)) return false;
|
|
126
|
+
seen.add(key);
|
|
127
|
+
return true;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function getHeadParents(cwd) {
|
|
132
|
+
const headWithParents = execSync("git rev-list --parents -n 1 HEAD", { cwd, encoding: "utf-8" }).trim();
|
|
133
|
+
return headWithParents ? headWithParents.split(/\s+/).slice(1) : [];
|
|
134
|
+
}
|
|
135
|
+
|
|
70
136
|
/**
|
|
71
137
|
* 获取上一次自动版本提交之后的业务提交摘要。
|
|
72
138
|
* Merge 提交只描述 Git 操作,不作为企业微信中的更新内容。
|
|
@@ -95,13 +161,17 @@ function getBuildUpdateText(gitAutoCommitText, cwd = process.cwd(), limit = 10)
|
|
|
95
161
|
|
|
96
162
|
if (!previousBuild) return "";
|
|
97
163
|
|
|
98
|
-
const
|
|
164
|
+
const baseline = resolveBuildSummaryBaseline(previousBuild.hash, cwd);
|
|
165
|
+
// 构建正好发生在 Merge HEAD 时,更新内容应描述本次合入的分支,而不是第一父链上更早的待发布提交。
|
|
166
|
+
const headParents = getHeadParents(cwd);
|
|
167
|
+
const range = headParents.length > 1 ? `${headParents[0]}..HEAD` : `${baseline}..HEAD`;
|
|
168
|
+
const subjects = deduplicateCommitSubjects(execSync(`git log --no-merges --format=%s ${range}`, {
|
|
99
169
|
cwd,
|
|
100
170
|
encoding: "utf-8"
|
|
101
171
|
})
|
|
102
172
|
.trim()
|
|
103
173
|
.split(/\r?\n/)
|
|
104
|
-
.filter(Boolean);
|
|
174
|
+
.filter(Boolean));
|
|
105
175
|
|
|
106
176
|
if (subjects.length === 0) return "无源码提交变更";
|
|
107
177
|
|
|
@@ -3,20 +3,20 @@ const fs = require("fs");
|
|
|
3
3
|
function normalizeRelativePath(value) {
|
|
4
4
|
return String(value).replace(/\\/g, "/");
|
|
5
5
|
}
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 从 .gitmodules 中解析所有子模块路径
|
|
9
|
-
*/
|
|
10
|
-
function parseGitmodulesPaths(rootDir) {
|
|
11
|
-
const p = require("path").join(rootDir, ".gitmodules");
|
|
12
|
-
if (!fs.existsSync(p)) return [];
|
|
13
|
-
const content = fs.readFileSync(p, "utf8");
|
|
14
|
-
const paths = [];
|
|
15
|
-
content.split(/\[submodule/).forEach((block) => {
|
|
16
|
-
const m = block.match(/path\s*=\s*(.+)/);
|
|
17
|
-
if (m) paths.push(m[1].trim());
|
|
18
|
-
});
|
|
19
|
-
return paths;
|
|
20
|
-
}
|
|
21
|
-
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 从 .gitmodules 中解析所有子模块路径
|
|
9
|
+
*/
|
|
10
|
+
function parseGitmodulesPaths(rootDir) {
|
|
11
|
+
const p = require("path").join(rootDir, ".gitmodules");
|
|
12
|
+
if (!fs.existsSync(p)) return [];
|
|
13
|
+
const content = fs.readFileSync(p, "utf8");
|
|
14
|
+
const paths = [];
|
|
15
|
+
content.split(/\[submodule/).forEach((block) => {
|
|
16
|
+
const m = block.match(/path\s*=\s*(.+)/);
|
|
17
|
+
if (m) paths.push(m[1].trim());
|
|
18
|
+
});
|
|
19
|
+
return paths;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
22
|
module.exports = { normalizeRelativePath, parseGitmodulesPaths };
|