@infly/libs 2.1.0 → 2.1.2
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 +9 -3
- package/adapters/webpack/auto-export.js +52 -52
- package/build/build-utils/command.js +57 -57
- 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-command.js +73 -73
- package/build/docker/compose-release.js +79 -79
- package/build/docker/deployment/promote-image.sh +2 -2
- package/build/docker/target-release.js +88 -4
- package/build/project-files.js +47 -47
- package/cli/select-option.js +53 -51
- package/core/async/concurrency.js +29 -29
- package/package.json +1 -1
- package/script/git-automation/submodule-utils.js +16 -16
- package/workspace/config.js +144 -144
|
@@ -3,11 +3,55 @@ const os = require("node:os");
|
|
|
3
3
|
const path = require("node:path");
|
|
4
4
|
const { execFileSync } = require("node:child_process");
|
|
5
5
|
const { EXIT_SELECTION, selectOption, selectOptions } = require("../../cli/select-option");
|
|
6
|
+
const { composeRelease } = require("./compose-release");
|
|
7
|
+
const { formatCicdLink } = require("./docker-build-push");
|
|
8
|
+
|
|
9
|
+
async function checkDocker(run, projectDir, buildx = true, dependencies = {}, autoStart = true) {
|
|
10
|
+
const quiet = { cwd: projectDir, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] };
|
|
11
|
+
try {
|
|
12
|
+
run("docker", ["info"], quiet);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
const detail = String(error.stderr || error.message || "").trim();
|
|
15
|
+
const env = dependencies.env || process.env;
|
|
16
|
+
const platform = dependencies.platform || process.platform;
|
|
17
|
+
if (autoStart && platform === "win32" && !env.CI && !env.JENKINS_URL && !env.DOCKER_HOST
|
|
18
|
+
&& /dockerDesktopLinuxEngine/i.test(detail)) {
|
|
19
|
+
// 只针对明确的本机 Desktop 管道启动软件,不切换 context,也不处理远端服务。
|
|
20
|
+
const log = dependencies.log || console.log;
|
|
21
|
+
log("Docker Desktop Linux 引擎未就绪,正在启动本机 Docker Desktop…");
|
|
22
|
+
run("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command",
|
|
23
|
+
"Start-Process -FilePath 'C:\\Program Files\\Docker\\Docker\\Docker Desktop.exe' -WindowStyle Hidden"], quiet);
|
|
24
|
+
const sleep = dependencies.sleep || ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
25
|
+
for (let attempt = 0; attempt < 60; attempt++) {
|
|
26
|
+
await sleep(2000);
|
|
27
|
+
try { run("docker", ["info"], quiet); }
|
|
28
|
+
catch {
|
|
29
|
+
if (attempt % 10 === 9) log(`等待 Docker Linux 引擎就绪…${(attempt + 1) * 2}s`);
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
log("Docker 引擎已就绪,继续构建。");
|
|
33
|
+
return checkDocker(run, projectDir, buildx, dependencies, false);
|
|
34
|
+
}
|
|
35
|
+
throw new Error("Docker Desktop 启动后 120 秒内 Linux 引擎仍未就绪,请检查 Docker Desktop 的启动错误后重试。", { cause: error });
|
|
36
|
+
}
|
|
37
|
+
const hint = error.code === "ENOENT"
|
|
38
|
+
? "未找到 Docker CLI,请安装 Docker 并确认 docker 位于 PATH。"
|
|
39
|
+
: /dockerDesktopLinuxEngine/i.test(detail)
|
|
40
|
+
? "无法连接 Docker Desktop Linux 引擎。请打开 Docker Desktop,等待 Linux 引擎启动,再运行 docker info 确认 Server 可访问。"
|
|
41
|
+
: "无法连接 Docker 引擎。请检查 docker context show、DOCKER_HOST 和 Docker 服务状态,再运行 docker info。";
|
|
42
|
+
throw new Error(`${hint}\n${detail}`, { cause: error });
|
|
43
|
+
}
|
|
44
|
+
if (buildx) {
|
|
45
|
+
try { run("docker", ["buildx", "version"], quiet); }
|
|
46
|
+
catch (error) { throw new Error("Docker 引擎已连接,但 Buildx 不可用。请检查 docker buildx version。", { cause: error }); }
|
|
47
|
+
}
|
|
48
|
+
}
|
|
6
49
|
|
|
7
50
|
function parseReleaseArgs(argv) {
|
|
8
51
|
const options = { push: false };
|
|
9
52
|
const keys = { "--target": "target", "--platform": "target", "--components": "components",
|
|
10
|
-
"--project-dir": "projectDir", "--backend-dir": "backendDir", "--config": "config", "--output": "output"
|
|
53
|
+
"--project-dir": "projectDir", "--backend-dir": "backendDir", "--config": "config", "--output": "output",
|
|
54
|
+
"--cicd-url": "cicdUrl" };
|
|
11
55
|
for (let i = 0; i < argv.length; i++) {
|
|
12
56
|
const [flag, ...suffix] = argv[i].split("=");
|
|
13
57
|
if (flag === "--") continue;
|
|
@@ -73,10 +117,24 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
73
117
|
const targets = config?.targets;
|
|
74
118
|
if (!targets || !Object.keys(targets).length) throw new Error("请配置 infly.docker.targets");
|
|
75
119
|
const log = dependencies.log || console.log;
|
|
120
|
+
const cicdLink = formatCicdLink(options.cicdUrl);
|
|
76
121
|
const target = options.target || await (dependencies.selectOption || selectOption)("请选择构建平台",
|
|
77
|
-
Object.entries(targets).
|
|
122
|
+
Object.entries(targets).map(([value, item]) => ({ value, label: item.label || value })));
|
|
78
123
|
if (target === EXIT_SELECTION) return { cancelled: true };
|
|
79
124
|
const profile = targets[target];
|
|
125
|
+
if (profile && !profile.components && profile.file) {
|
|
126
|
+
// 旧目标只有 Compose 文件,保留既有镜像/版本语义,不把前端目标静默升级为前后端生产发布。
|
|
127
|
+
const components = options.components || await (dependencies.selectOptions || selectOptions)("请选择构建范围",
|
|
128
|
+
[{ value: "frontend", label: "前端(Compose)" }]);
|
|
129
|
+
if (components === EXIT_SELECTION) return { cancelled: true };
|
|
130
|
+
if (!Array.isArray(components) || components.length !== 1 || components[0] !== "frontend") {
|
|
131
|
+
throw new Error(`平台 ${target} 只配置了 Compose 前端构建,不支持后端`);
|
|
132
|
+
}
|
|
133
|
+
log(`平台 ${profile.label || target} 使用 Compose 配置中的镜像标签,不自动升级版本;不生成 qc 镜像。`);
|
|
134
|
+
if (!options.dryRun) await checkDocker(dependencies.run || execute, projectDir, false, dependencies);
|
|
135
|
+
return (dependencies.composeRelease || composeRelease)({ projectDir, file: profile.file,
|
|
136
|
+
bump: "none", push: Boolean(options.push), dryRun: Boolean(options.dryRun) });
|
|
137
|
+
}
|
|
80
138
|
if (!profile?.components) throw new Error(`平台 ${target} 未配置 components`);
|
|
81
139
|
const components = options.components || await (dependencies.selectOptions || selectOptions)("请选择构建范围",
|
|
82
140
|
Object.entries(profile.components).map(([value, item]) => ({ value, label: item.label || value })));
|
|
@@ -110,8 +168,7 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
110
168
|
}
|
|
111
169
|
return { target, missingSources: missing.map((item) => item.name) };
|
|
112
170
|
}
|
|
113
|
-
run
|
|
114
|
-
run("docker", ["buildx", "version"], { cwd: projectDir });
|
|
171
|
+
await checkDocker(run, projectDir, true, dependencies);
|
|
115
172
|
const results = [];
|
|
116
173
|
for (const item of plan) {
|
|
117
174
|
let tempRoot;
|
|
@@ -154,6 +211,7 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
154
211
|
if (!io.existsSync(dockerfile)) throw new Error(`缺少 Dockerfile: ${dockerfile}`);
|
|
155
212
|
const short = commit.slice(0, 12);
|
|
156
213
|
const image = `${item.repository}:qc-${short}`;
|
|
214
|
+
// --load 只把当前构建节点平台的镜像载入本地镜像存储;发布流程按单平台镜像晋升。
|
|
157
215
|
run("docker", ["buildx", "build", "--load", "--file", dockerfile,
|
|
158
216
|
"--label", `org.opencontainers.image.revision=${commit}`, "--label", `org.opencontainers.image.version=${short}`,
|
|
159
217
|
"--label", `com.infly.platform=${target}`, "--tag", image, context], { cwd: projectDir });
|
|
@@ -174,6 +232,32 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
174
232
|
if (tempRoot) io.rmSync(tempRoot, { recursive: true, force: true });
|
|
175
233
|
}
|
|
176
234
|
}
|
|
235
|
+
|
|
236
|
+
// 构建结果汇总(排版与 build:docker 保持一致:✅ 完成 + 可用标签 + 可选发布系统链接)。
|
|
237
|
+
// 构建时已逐组件打印 frontend_commit= 等机器行,供失败前人工留档;这里汇总为可直接复制到
|
|
238
|
+
// Jenkins 发布 Job 的参数区块。
|
|
239
|
+
if (results.length) {
|
|
240
|
+
const pushNote = options.push
|
|
241
|
+
? "本次新建 qc 隔离镜像;正式标签由 Jenkins 校验后晋升"
|
|
242
|
+
: "本次新建 qc 隔离镜像(仅本地,未推送)";
|
|
243
|
+
log("");
|
|
244
|
+
log("\x1b[0;34m==========================================");
|
|
245
|
+
log("✅ 完成");
|
|
246
|
+
log("==========================================\x1b[0m");
|
|
247
|
+
log(`可用标签(${pushNote}):`);
|
|
248
|
+
for (const r of results) {
|
|
249
|
+
log(` ${r.component}=${r.image} (commit 精确版本)`);
|
|
250
|
+
}
|
|
251
|
+
log("");
|
|
252
|
+
log("发布参数(复制到 Jenkins 发布 Job 对应输入框):");
|
|
253
|
+
for (const r of results) {
|
|
254
|
+
log(` ${r.component}_commit=${r.commit}`);
|
|
255
|
+
}
|
|
256
|
+
if (cicdLink) {
|
|
257
|
+
log("");
|
|
258
|
+
log(cicdLink);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
177
261
|
return { target, results };
|
|
178
262
|
}
|
|
179
263
|
|
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
|
+
};
|
package/cli/select-option.js
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
1
|
// CLI 交互选择适配,业务选项由调用方注入。
|
|
2
2
|
const EXIT_SELECTION = "__exit__";
|
|
3
|
-
|
|
4
|
-
function isReadlineClosedError(error) {
|
|
5
|
-
return error?.code === "ERR_USE_AFTER_CLOSE";
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
function makePromptCancellationSafe(prompt) {
|
|
9
|
-
const unsafeStop = prompt.stop;
|
|
10
|
-
if (typeof unsafeStop !== "function") return;
|
|
11
|
-
|
|
12
|
-
prompt.removeListener("close", unsafeStop);
|
|
13
|
-
const safeStop = () => {
|
|
14
|
-
try {
|
|
15
|
-
unsafeStop();
|
|
16
|
-
} catch (error) {
|
|
17
|
-
if (!isReadlineClosedError(error)) throw error;
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
prompt.stop = safeStop;
|
|
21
|
-
prompt.once("close", safeStop);
|
|
22
|
-
}
|
|
23
|
-
|
|
3
|
+
|
|
4
|
+
function isReadlineClosedError(error) {
|
|
5
|
+
return error?.code === "ERR_USE_AFTER_CLOSE";
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function makePromptCancellationSafe(prompt) {
|
|
9
|
+
const unsafeStop = prompt.stop;
|
|
10
|
+
if (typeof unsafeStop !== "function") return;
|
|
11
|
+
|
|
12
|
+
prompt.removeListener("close", unsafeStop);
|
|
13
|
+
const safeStop = () => {
|
|
14
|
+
try {
|
|
15
|
+
unsafeStop();
|
|
16
|
+
} catch (error) {
|
|
17
|
+
if (!isReadlineClosedError(error)) throw error;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
prompt.stop = safeStop;
|
|
21
|
+
prompt.once("close", safeStop);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
24
|
async function selectOption(message, choices, dependencies = {}) {
|
|
25
|
-
const stdin = dependencies.stdin || process.stdin;
|
|
26
|
-
const stdout = dependencies.stdout || process.stdout;
|
|
27
|
-
if (!stdin.isTTY || !stdout.isTTY) {
|
|
28
|
-
throw new Error(`${message}: interactive selection requires a TTY; pass an explicit option.`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const Select = dependencies.Select || require("enquirer").Select;
|
|
32
|
-
const prompt = new Select({
|
|
33
|
-
name: "selection",
|
|
34
|
-
message,
|
|
35
|
-
stdin,
|
|
36
|
-
stdout,
|
|
37
|
-
choices: [
|
|
38
|
-
...choices.map((choice) => ({
|
|
39
|
-
name: choice.value,
|
|
40
|
-
message: choice.label,
|
|
41
|
-
})),
|
|
42
|
-
{ name: EXIT_SELECTION, message: "退出" },
|
|
43
|
-
],
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
prompt.once("start", makePromptCancellationSafe);
|
|
47
|
-
try {
|
|
48
|
-
return await prompt.run();
|
|
49
|
-
} catch (error) {
|
|
50
|
-
if (!error || error.name === "CancelPromptError" || isReadlineClosedError(error)) {
|
|
51
|
-
return EXIT_SELECTION;
|
|
52
|
-
}
|
|
53
|
-
throw error;
|
|
54
|
-
}
|
|
25
|
+
const stdin = dependencies.stdin || process.stdin;
|
|
26
|
+
const stdout = dependencies.stdout || process.stdout;
|
|
27
|
+
if (!stdin.isTTY || !stdout.isTTY) {
|
|
28
|
+
throw new Error(`${message}: interactive selection requires a TTY; pass an explicit option.`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const Select = dependencies.Select || require("enquirer").Select;
|
|
32
|
+
const prompt = new Select({
|
|
33
|
+
name: "selection",
|
|
34
|
+
message,
|
|
35
|
+
stdin,
|
|
36
|
+
stdout,
|
|
37
|
+
choices: [
|
|
38
|
+
...choices.map((choice) => ({
|
|
39
|
+
name: choice.value,
|
|
40
|
+
message: choice.label,
|
|
41
|
+
})),
|
|
42
|
+
{ name: EXIT_SELECTION, message: "退出" },
|
|
43
|
+
],
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
prompt.once("start", makePromptCancellationSafe);
|
|
47
|
+
try {
|
|
48
|
+
return await prompt.run();
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (!error || error.name === "CancelPromptError" || isReadlineClosedError(error)) {
|
|
51
|
+
return EXIT_SELECTION;
|
|
52
|
+
}
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
async function selectOptions(message, choices, dependencies = {}) {
|
|
@@ -65,6 +65,8 @@ async function selectOptions(message, choices, dependencies = {}) {
|
|
|
65
65
|
const prompt = new MultiSelect({
|
|
66
66
|
name: "selections",
|
|
67
67
|
message,
|
|
68
|
+
indicator: { on: "[√ ]", off: "[ ]" },
|
|
69
|
+
hint: "↑/↓ 移动,空格勾选/取消,回车确认,Ctrl+C 退出(至少选择一项)",
|
|
68
70
|
stdin,
|
|
69
71
|
stdout,
|
|
70
72
|
min: 1,
|
|
@@ -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
|
@@ -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 };
|