@infly/libs 2.2.0 → 2.2.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 +49 -11
- package/adapters/webpack/auto-export.js +52 -52
- package/bin/cli.js +10 -0
- 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 +6 -8
- package/build/docker/compose-release.js +79 -79
- package/build/docker/config-loader.js +127 -0
- package/build/docker/deployment/release.sh +9 -4
- package/build/docker/deployment-bundle.js +3 -4
- package/build/docker/docker-build-push.js +14 -7
- package/build/docker/release-docker.js +2 -5
- package/build/docker/target-release.js +93 -21
- package/build/project-files.js +47 -47
- package/core/async/concurrency.js +29 -29
- package/package.json +1 -1
- package/script/git-automation/index.js +703 -703
- package/script/git-automation/submodule-utils.js +16 -16
- package/workspace/config.js +144 -144
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const fs = require("node:fs");
|
|
2
2
|
const path = require("node:path");
|
|
3
|
+
const { loadDockerConfig } = require("./config-loader");
|
|
3
4
|
const { resolveDockerConfig } = require("./target-config");
|
|
4
5
|
|
|
5
6
|
const quote = (value) => "'" + String(value).replaceAll("'", "'\\''") + "'";
|
|
@@ -125,10 +126,8 @@ function renderDeploymentConfig(config, target) {
|
|
|
125
126
|
|
|
126
127
|
function createDeploymentBundle(options = {}) {
|
|
127
128
|
const projectDir = path.resolve(options.projectDir || process.cwd());
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
: JSON.parse(fs.readFileSync(path.join(projectDir, "package.json"), "utf8")).infly?.docker);
|
|
131
|
-
const config = resolveDockerConfig(rawConfig);
|
|
129
|
+
const config = loadDockerConfig({ projectDir, configPath: options.config,
|
|
130
|
+
configObject: options.configObject, configDir: options.configDir });
|
|
132
131
|
const rendered = renderDeploymentConfig(config, options.target);
|
|
133
132
|
if (options.dryRun) return { target: options.target, config: rendered };
|
|
134
133
|
const output = path.resolve(projectDir, options.output || ".infly-deploy");
|
|
@@ -54,14 +54,20 @@ function formatCicdLink(value, deployParams) {
|
|
|
54
54
|
|
|
55
55
|
// 单组件推送成功后预填“平台/镜像版本”参数,新平台首次部署无需从历史选项里挑选;仅对 /build 构建入口追加。
|
|
56
56
|
if (deployParams !== undefined) {
|
|
57
|
-
const { platform, tag, date } = deployParams;
|
|
57
|
+
const { platform, platformLabel, tag, date } = deployParams;
|
|
58
58
|
if (typeof platform !== "string" || !/^[a-z0-9]+$/.test(platform)
|
|
59
|
-
|| typeof tag !== "string" || !/^[0-9a-f]{
|
|
59
|
+
|| typeof tag !== "string" || !/^[0-9a-f]{40}$/.test(tag)
|
|
60
60
|
|| typeof date !== "string" || !/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/.test(date)) {
|
|
61
|
-
throw new Error("发布系统深链参数必须是合法平台、
|
|
61
|
+
throw new Error("发布系统深链参数必须是合法平台、40 位提交版本和 yyyy-MM-dd HH:mm 时间");
|
|
62
|
+
}
|
|
63
|
+
if (platformLabel !== undefined && (typeof platformLabel !== "string" || !platformLabel.trim()
|
|
64
|
+
|| /[\u0000-\u001f\u007f]/.test(platformLabel))) {
|
|
65
|
+
throw new Error("发布系统深链平台名称必须是非空且不含控制字符的字符串");
|
|
62
66
|
}
|
|
63
67
|
if (parsedUrl.pathname.endsWith("/build")) {
|
|
64
|
-
|
|
68
|
+
// Jenkins 原生 choice 的显示文本就是提交值;传入标签时须与“中文名称 / 编码”选项完全一致。
|
|
69
|
+
const platformChoice = platformLabel === undefined ? platform : `${platformLabel.trim()} / ${platform}`;
|
|
70
|
+
const suffix = `&${encodeURIComponent("平台")}=${encodeURIComponent(platformChoice)}`
|
|
65
71
|
+ `&${encodeURIComponent("镜像版本")}=${encodeURIComponent(`${platform} / ${tag} / ${date}`)}`;
|
|
66
72
|
parsedUrl.search = parsedUrl.search ? `${parsedUrl.search}${suffix}` : `?${suffix.slice(1)}`;
|
|
67
73
|
}
|
|
@@ -294,14 +300,15 @@ async function dockerBuildPush(options = {}) {
|
|
|
294
300
|
const imageName = pkg.name.replace(/^@[^/]+\//, "");
|
|
295
301
|
|
|
296
302
|
// 3. 获取版本号(monorepo 模式取 superproject 的 commit)
|
|
297
|
-
const commitHash = version || execSync("git rev-parse
|
|
303
|
+
const commitHash = version || execSync("git rev-parse HEAD", {
|
|
298
304
|
cwd: contextRoot, encoding: "utf-8",
|
|
299
305
|
}).trim();
|
|
300
306
|
const fullCommit = revision || execSync("git rev-parse HEAD", {
|
|
301
307
|
cwd: contextRoot, encoding: "utf-8",
|
|
302
308
|
}).trim();
|
|
303
|
-
if (!/^[0-9a-f]{
|
|
304
|
-
|
|
309
|
+
if (!/^[0-9a-f]{40}$/i.test(commitHash) || !/^[0-9a-f]{40}$/i.test(fullCommit)
|
|
310
|
+
|| commitHash.toLowerCase() !== fullCommit.toLowerCase()) {
|
|
311
|
+
throw new Error("Docker 镜像版本和 revision 必须是同一个完整 40 位 Git commit hash");
|
|
305
312
|
}
|
|
306
313
|
|
|
307
314
|
// 4. 镜像标签
|
|
@@ -141,11 +141,8 @@ function resolveProjectVersion(projectRoot) {
|
|
|
141
141
|
cwd: projectRoot,
|
|
142
142
|
encoding: "utf-8",
|
|
143
143
|
}).trim();
|
|
144
|
-
const commitHash =
|
|
145
|
-
|
|
146
|
-
encoding: "utf-8",
|
|
147
|
-
}).trim();
|
|
148
|
-
if (!/^[0-9a-f]{7,40}$/i.test(fullCommit) || !/^[0-9a-f]{7,40}$/i.test(commitHash)) {
|
|
144
|
+
const commitHash = fullCommit;
|
|
145
|
+
if (!/^[0-9a-f]{40}$/i.test(fullCommit)) {
|
|
149
146
|
throw new Error("无法从当前项目解析合法的 Git commit hash");
|
|
150
147
|
}
|
|
151
148
|
return { commitHash: commitHash.toLowerCase(), fullCommit: fullCommit.toLowerCase() };
|
|
@@ -2,10 +2,10 @@ const fs = require("node:fs");
|
|
|
2
2
|
const os = require("node:os");
|
|
3
3
|
const path = require("node:path");
|
|
4
4
|
const { execFileSync } = require("node:child_process");
|
|
5
|
-
const { EXIT_SELECTION, selectOption, selectOptions } = require("../../cli/select-option");
|
|
5
|
+
const { EXIT_SELECTION, inputText, selectOption, selectOptions } = require("../../cli/select-option");
|
|
6
6
|
const { composeRelease } = require("./compose-release");
|
|
7
7
|
const { formatCicdLink } = require("./docker-build-push");
|
|
8
|
-
const {
|
|
8
|
+
const { loadDockerConfig } = require("./config-loader");
|
|
9
9
|
|
|
10
10
|
// Jenkins 构建页“镜像版本”与 Webhook 通知均按北京时间展示。
|
|
11
11
|
function beijingDateTime(now = new Date()) {
|
|
@@ -87,17 +87,48 @@ function inside(root, relative) {
|
|
|
87
87
|
return result;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
function normalizeSourceDirectories(value, name) {
|
|
91
|
+
const directories = Array.isArray(value) ? value : [value];
|
|
92
|
+
if (!directories.length || directories.some((item) => typeof item !== "string" || !item.trim())) {
|
|
93
|
+
throw new Error(`${name} source.directory 必须是非空路径字符串或非空路径数组`);
|
|
94
|
+
}
|
|
95
|
+
return [...new Set(directories.map((item) => item.trim()))];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function resolveSourceDirectories(projectDir, value, name) {
|
|
99
|
+
return normalizeSourceDirectories(value, name).map((directory) => path.resolve(projectDir, directory));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function trimPathInput(value) {
|
|
103
|
+
const trimmed = String(value || "").trim();
|
|
104
|
+
return trimmed.length >= 2 && ((trimmed.startsWith('"') && trimmed.endsWith('"'))
|
|
105
|
+
|| (trimmed.startsWith("'") && trimmed.endsWith("'")))
|
|
106
|
+
? trimmed.slice(1, -1).trim()
|
|
107
|
+
: trimmed;
|
|
108
|
+
}
|
|
109
|
+
|
|
90
110
|
function validateComponent(component, name) {
|
|
91
111
|
if (!component || !/^[a-z0-9][a-z0-9._:/-]*$/.test(component.repository || "") || component.repository.includes("@")) {
|
|
92
112
|
throw new Error(`${name} 必须配置有效的镜像 repository(不含标签)`);
|
|
93
113
|
}
|
|
94
114
|
if (component.repository.split("/").at(-1).includes(":")) throw new Error(`${name} repository 不得包含标签`);
|
|
95
115
|
if (!component.expectedRef && !component.source?.branch) throw new Error(`${name} 必须配置 expectedRef 或 source.branch`);
|
|
96
|
-
if (component.source
|
|
97
|
-
|
|
116
|
+
if (component.source) {
|
|
117
|
+
normalizeSourceDirectories(component.source.directory, name);
|
|
118
|
+
if (!component.source.branch || component.source.branch.startsWith("-")) {
|
|
119
|
+
throw new Error(`${name} source 必须配置合法 branch`);
|
|
120
|
+
}
|
|
98
121
|
}
|
|
99
122
|
inside("/config", component.dockerfile || "Dockerfile");
|
|
100
123
|
inside("/config", component.context || ".");
|
|
124
|
+
if (component.target != null && (typeof component.target !== "string" || !/^[a-zA-Z0-9_.-]+$/.test(component.target))) {
|
|
125
|
+
throw new Error(`${name} target 必须是合法 Docker stage`);
|
|
126
|
+
}
|
|
127
|
+
if (component.buildArgs != null && (!component.buildArgs || typeof component.buildArgs !== "object"
|
|
128
|
+
|| Array.isArray(component.buildArgs) || Object.entries(component.buildArgs).some(([key, value]) =>
|
|
129
|
+
!/^[A-Z_][A-Z0-9_]*$/.test(key) || !["string", "number", "boolean"].includes(typeof value)))) {
|
|
130
|
+
throw new Error(`${name} buildArgs 必须是合法构建参数对象`);
|
|
131
|
+
}
|
|
101
132
|
if (component.requiredFiles) {
|
|
102
133
|
if (!Array.isArray(component.requiredFiles)) throw new Error(`${name} requiredFiles 必须是数组`);
|
|
103
134
|
for (const file of component.requiredFiles) inside("/config", file);
|
|
@@ -122,10 +153,8 @@ function execute(command, args, options = {}) {
|
|
|
122
153
|
|
|
123
154
|
async function runTargetRelease(options = {}, dependencies = {}) {
|
|
124
155
|
const projectDir = path.resolve(options.projectDir || process.cwd());
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
: JSON.parse(fs.readFileSync(path.join(projectDir, "package.json"), "utf8")).infly?.docker);
|
|
128
|
-
const config = resolveDockerConfig(rawConfig);
|
|
156
|
+
const config = loadDockerConfig({ projectDir, configPath: options.config,
|
|
157
|
+
configObject: options.configObject, configDir: options.configDir });
|
|
129
158
|
const targets = config?.targets;
|
|
130
159
|
if (!targets || !Object.keys(targets).length) throw new Error("请配置 infly.docker.targets");
|
|
131
160
|
const releaseScripts = config?.releaseScripts || {};
|
|
@@ -155,19 +184,54 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
155
184
|
if (!Array.isArray(components) || !components.length || new Set(components).size !== components.length) {
|
|
156
185
|
throw new Error("请至少选择一个构建范围,且不能重复");
|
|
157
186
|
}
|
|
187
|
+
const io = dependencies.fs || fs;
|
|
158
188
|
const plan = components.map((name) => {
|
|
159
189
|
if (!Object.hasOwn(profile.components, name)) throw new Error(`不支持的构建范围: ${name}`);
|
|
160
190
|
const component = profile.components[name];
|
|
161
191
|
validateComponent(component, name);
|
|
162
|
-
return { name, ...component
|
|
163
|
-
|
|
192
|
+
if (!component.source) return { name, ...component };
|
|
193
|
+
const configuredDirectory = name === "backend" && options.backendDir ? options.backendDir : component.source.directory;
|
|
194
|
+
const directoryCandidates = resolveSourceDirectories(projectDir, configuredDirectory, name);
|
|
195
|
+
return { name, ...component, source: { ...component.source,
|
|
196
|
+
directory: directoryCandidates[0], directoryCandidates } };
|
|
164
197
|
});
|
|
165
198
|
// dry-run 只解析配置,不联网、不调用 Git/Docker、不创建临时目录。
|
|
166
199
|
if (options.dryRun) { log(JSON.stringify({ target, push: Boolean(options.push), plan }, null, 2)); return { target, plan }; }
|
|
167
|
-
const io = dependencies.fs || fs;
|
|
168
200
|
const run = dependencies.run || execute;
|
|
169
201
|
const capture = (command, args) => String(run(command, args, { cwd: projectDir, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }) || "").trim();
|
|
170
|
-
//
|
|
202
|
+
// 全部组件先预检,数组按顺序采用第一个 Git 仓;交互终端允许补录机器本地路径。
|
|
203
|
+
const stdin = dependencies.stdin || process.stdin;
|
|
204
|
+
const stdout = dependencies.stdout || process.stdout;
|
|
205
|
+
const interactive = dependencies.interactive ?? Boolean(stdin.isTTY && stdout.isTTY);
|
|
206
|
+
for (const item of plan.filter((candidate) => candidate.source)) {
|
|
207
|
+
const matched = item.source.directoryCandidates.find((directory) => io.existsSync(path.join(directory, ".git")));
|
|
208
|
+
if (matched) {
|
|
209
|
+
item.source.directory = matched;
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (!interactive) continue;
|
|
213
|
+
log(`未在 ${item.name} 源码候选路径中找到 Git 仓库:`);
|
|
214
|
+
item.source.directoryCandidates.forEach((directory, index) => log(` ${index + 1}. ${directory}`));
|
|
215
|
+
const example = process.platform === "win32" ? "D:\\Repo\\equity_project" : "/opt/src/equity_project";
|
|
216
|
+
log(`请输入源码仓路径,例如:${example}`);
|
|
217
|
+
log(`也可以退出后使用 --backend-dir "${example}" 重新执行。`);
|
|
218
|
+
while (true) {
|
|
219
|
+
const entered = await (dependencies.inputText || inputText)(
|
|
220
|
+
`请输入 ${item.name} 源码 Git 仓库路径(例如 ${example})`,
|
|
221
|
+
{ stdin, stdout },
|
|
222
|
+
);
|
|
223
|
+
if (entered === EXIT_SELECTION) return { cancelled: true };
|
|
224
|
+
const normalized = trimPathInput(entered);
|
|
225
|
+
const candidate = normalized ? path.resolve(projectDir, normalized) : "";
|
|
226
|
+
if (candidate && io.existsSync(path.join(candidate, ".git"))) {
|
|
227
|
+
item.source.directory = candidate;
|
|
228
|
+
item.source.directoryCandidates.push(candidate);
|
|
229
|
+
log(`已使用 ${item.name} 源码仓:${candidate}`);
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
log(`路径不是有效 Git 仓库(缺少 .git):${candidate || "<空>"}`);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
171
235
|
const missing = plan.filter((item) => item.source && !io.existsSync(path.join(item.source.directory, ".git")));
|
|
172
236
|
if (missing.length) {
|
|
173
237
|
for (const item of missing) {
|
|
@@ -229,12 +293,16 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
229
293
|
dockerfile = inside(context, item.dockerfile || "Dockerfile");
|
|
230
294
|
}
|
|
231
295
|
if (!io.existsSync(dockerfile)) throw new Error(`缺少 Dockerfile: ${dockerfile}`);
|
|
232
|
-
const
|
|
233
|
-
const image = `${item.repository}:${short}`;
|
|
296
|
+
const image = `${item.repository}:${commit}`;
|
|
234
297
|
const latestImage = `${item.repository}:latest`;
|
|
235
298
|
// --load 只把当前构建节点平台的镜像载入本地镜像存储;发布流程直接使用单平台镜像。
|
|
236
|
-
|
|
237
|
-
|
|
299
|
+
const buildOptions = ["buildx", "build", "--load", "--file", dockerfile];
|
|
300
|
+
if (item.target) buildOptions.push("--target", item.target);
|
|
301
|
+
for (const [key, value] of Object.entries(item.buildArgs || {})) {
|
|
302
|
+
buildOptions.push("--build-arg", `${key}=${value}`);
|
|
303
|
+
}
|
|
304
|
+
run("docker", [...buildOptions,
|
|
305
|
+
"--label", `org.opencontainers.image.revision=${commit}`, "--label", `org.opencontainers.image.version=${commit}`,
|
|
238
306
|
"--label", `com.infly.platform=${target}`, "--tag", image, "--tag", latestImage, context], { cwd: projectDir });
|
|
239
307
|
if (capture("docker", ["image", "inspect", "--format", '{{ index .Config.Labels "org.opencontainers.image.revision" }}', image]) !== commit) {
|
|
240
308
|
throw new Error(`${item.name} 镜像 revision 校验失败`);
|
|
@@ -263,7 +331,9 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
263
331
|
// 输出同时面向本地核对与 Jenkins 下拉部署,避免再手工复制提交参数。
|
|
264
332
|
if (results.length) {
|
|
265
333
|
const pushNote = options.push
|
|
266
|
-
?
|
|
334
|
+
? profile.deployment?.ready === true
|
|
335
|
+
? "已推送不可变提交标签和 latest 最新推送标签,可直接在 Jenkins 选择部署"
|
|
336
|
+
: "已推送不可变提交标签和 latest 最新推送标签;该平台尚未开放 Jenkins 部署"
|
|
267
337
|
: "仅本地生成,未推送镜像";
|
|
268
338
|
log("");
|
|
269
339
|
log("\x1b[0;34m==========================================");
|
|
@@ -274,12 +344,14 @@ async function runTargetRelease(options = {}, dependencies = {}) {
|
|
|
274
344
|
log(` ${r.component}=${r.image} (不可变提交版本)`);
|
|
275
345
|
log(` ${r.component}_latest=${r.latestImage} (最新推送镜像,即所选平台 registry 的 latest)`);
|
|
276
346
|
}
|
|
277
|
-
if (cicdLink) {
|
|
347
|
+
if (cicdLink && profile.deployment?.ready === true) {
|
|
278
348
|
log("");
|
|
279
|
-
//
|
|
349
|
+
// 单组件推送成功后把平台与完整提交预填进 Jenkins 参数,新平台首次部署无需从历史下拉选项挑选;
|
|
280
350
|
// 多组件对应对应多个任务时保持原链接,非 /build 地址由 formatCicdLink 原样返回。
|
|
281
|
-
const deployLink = options.push &&
|
|
282
|
-
|
|
351
|
+
const deployLink = options.push && profile.deployment?.ready === true
|
|
352
|
+
&& results.length === 1 && /^[a-z0-9]+$/.test(target)
|
|
353
|
+
? formatCicdLink(options.cicdUrl, { platform: target, platformLabel: profile.label,
|
|
354
|
+
tag: results[0].commit,
|
|
283
355
|
date: beijingDateTime((dependencies.now || (() => new Date()))()) })
|
|
284
356
|
: cicdLink;
|
|
285
357
|
log(deployLink);
|
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 };
|