@infly/libs 2.2.5 → 2.2.6
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/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/docker-build-push.js +96 -4
- package/build/project-files.js +47 -47
- 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
|
@@ -1,136 +1,136 @@
|
|
|
1
|
-
function shouldSkipWebhook() {
|
|
2
|
-
const args = process.argv || [];
|
|
3
|
-
const hasNoWebhookArg = args.some((arg = "") =>
|
|
4
|
-
["--nowebhook", "--noWebhook", "--no-webhook"].includes(arg)
|
|
5
|
-
);
|
|
6
|
-
|
|
7
|
-
const env = process.env || {};
|
|
8
|
-
const envValues = [
|
|
9
|
-
env.npm_config_nowebhook,
|
|
10
|
-
env.npm_config_noWebhook,
|
|
11
|
-
env.NO_WEBHOOK,
|
|
12
|
-
env.DISABLE_WEBHOOK
|
|
13
|
-
];
|
|
14
|
-
const hasNoWebhookEnv = envValues.some((value) => {
|
|
15
|
-
const normalized = String(value || "").toLowerCase();
|
|
16
|
-
return ["1", "true", "yes", "on"].includes(normalized);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
return hasNoWebhookArg || hasNoWebhookEnv;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function getWebhookMessage() {
|
|
23
|
-
const args = process.argv || [];
|
|
24
|
-
const env = process.env || {};
|
|
25
|
-
const messageEnv =
|
|
26
|
-
env.npm_config_m ||
|
|
27
|
-
env.npm_config_message ||
|
|
28
|
-
env.WEBHOOK_MESSAGE ||
|
|
29
|
-
env.BUILD_MESSAGE ||
|
|
30
|
-
"";
|
|
31
|
-
|
|
32
|
-
if (messageEnv) {
|
|
33
|
-
return String(messageEnv).trim();
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
for (let index = 0; index < args.length; index += 1) {
|
|
37
|
-
const arg = args[index] || "";
|
|
38
|
-
|
|
39
|
-
if (arg === "-m" || arg === "--message") {
|
|
40
|
-
return String(args[index + 1] || "").trim();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (arg.startsWith("-m=")) {
|
|
44
|
-
return arg.slice(3).trim();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (arg.startsWith("--message=")) {
|
|
48
|
-
return arg.slice(10).trim();
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return "";
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function postVersionFileAndMsg(publishText, extraParams = {}) {
|
|
56
|
-
const {
|
|
57
|
-
projectName,
|
|
58
|
-
newFileName,
|
|
59
|
-
gitInfo,
|
|
60
|
-
webhookUrl = "", // 启用企业微信推送
|
|
61
|
-
webhookAtUser = [],
|
|
62
|
-
webhookExtraText = "",
|
|
63
|
-
gitAutoPushRepos,
|
|
64
|
-
gitAutoPushReposBranchMap,
|
|
65
|
-
repos,
|
|
66
|
-
deployUrl = ""
|
|
67
|
-
} = extraParams || {};
|
|
68
|
-
const { branch, commitMsg, author } = gitInfo || {};
|
|
69
|
-
const webhookMessage = getWebhookMessage();
|
|
70
|
-
const GITPATH = `https://gitee.com/gdinfly_1/${projectName}/blob/${branch}/${newFileName}`;
|
|
71
|
-
const UPLOADURL = `https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=&type=file`;
|
|
72
|
-
/* const data = {
|
|
73
|
-
msgtype: "markdown",
|
|
74
|
-
markdown: {
|
|
75
|
-
content: `${publishText}\n构建文件:[点击下载](${GITPATH})`,
|
|
76
|
-
},
|
|
77
|
-
};
|
|
78
|
-
if (Array.isArray(webhookAtUser) && webhookAtUser.length > 0) {
|
|
79
|
-
webhookAtUser.forEach((item, index) => {
|
|
80
|
-
data.markdown.content = `${data.markdown.content}\n<@${item}>`;
|
|
81
|
-
});
|
|
82
|
-
} */
|
|
83
|
-
let content = publishText;
|
|
84
|
-
if (gitAutoPushReposBranchMap) {
|
|
85
|
-
content = [
|
|
86
|
-
publishText,
|
|
87
|
-
`构建文件:已推送对应仓库【${repos}】- 分支【${gitAutoPushReposBranchMap[branch]}】`,
|
|
88
|
-
`构建人:${author}`,
|
|
89
|
-
deployUrl ? `部署系统:${deployUrl}` : ""
|
|
90
|
-
].filter(Boolean).join("\n");
|
|
91
|
-
} else {
|
|
92
|
-
content = `${publishText}\n构建文件下载地址:${GITPATH}\n${webhookExtraText}\n`;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (webhookMessage) {
|
|
96
|
-
content = `${content}\n${webhookMessage}`;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// 防御性过滤:防止运行时数据中的 %s 等格式化占位符泄露到推送内容
|
|
100
|
-
content = (content || "").replace(/%s/g, "");
|
|
101
|
-
|
|
102
|
-
const data = {
|
|
103
|
-
msgtype: "text",
|
|
104
|
-
text: {
|
|
105
|
-
content,
|
|
106
|
-
mentioned_mobile_list: Array.isArray(webhookAtUser) ? webhookAtUser : webhookAtUser[branch] // 支持按分支配置不同的提醒人
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
if (shouldSkipWebhook()) {
|
|
111
|
-
console.log(global.logColor?.info || "%s", "🔕 已跳过企业微信机器人推送(nowebhook)");
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
try {
|
|
116
|
-
const axios = require("axios");
|
|
117
|
-
|
|
118
|
-
if (axios) {
|
|
119
|
-
axios
|
|
120
|
-
.post(webhookUrl, data, {
|
|
121
|
-
headers: {
|
|
122
|
-
"Content-Type": "application/json"
|
|
123
|
-
}
|
|
124
|
-
})
|
|
125
|
-
.catch((e) => {
|
|
126
|
-
console.error("请求出错:", e);
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
} catch (error) {
|
|
130
|
-
console.error("axios is not available, please install it.");
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
module.exports = {
|
|
135
|
-
postVersionFileAndMsg
|
|
136
|
-
};
|
|
1
|
+
function shouldSkipWebhook() {
|
|
2
|
+
const args = process.argv || [];
|
|
3
|
+
const hasNoWebhookArg = args.some((arg = "") =>
|
|
4
|
+
["--nowebhook", "--noWebhook", "--no-webhook"].includes(arg)
|
|
5
|
+
);
|
|
6
|
+
|
|
7
|
+
const env = process.env || {};
|
|
8
|
+
const envValues = [
|
|
9
|
+
env.npm_config_nowebhook,
|
|
10
|
+
env.npm_config_noWebhook,
|
|
11
|
+
env.NO_WEBHOOK,
|
|
12
|
+
env.DISABLE_WEBHOOK
|
|
13
|
+
];
|
|
14
|
+
const hasNoWebhookEnv = envValues.some((value) => {
|
|
15
|
+
const normalized = String(value || "").toLowerCase();
|
|
16
|
+
return ["1", "true", "yes", "on"].includes(normalized);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
return hasNoWebhookArg || hasNoWebhookEnv;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getWebhookMessage() {
|
|
23
|
+
const args = process.argv || [];
|
|
24
|
+
const env = process.env || {};
|
|
25
|
+
const messageEnv =
|
|
26
|
+
env.npm_config_m ||
|
|
27
|
+
env.npm_config_message ||
|
|
28
|
+
env.WEBHOOK_MESSAGE ||
|
|
29
|
+
env.BUILD_MESSAGE ||
|
|
30
|
+
"";
|
|
31
|
+
|
|
32
|
+
if (messageEnv) {
|
|
33
|
+
return String(messageEnv).trim();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
37
|
+
const arg = args[index] || "";
|
|
38
|
+
|
|
39
|
+
if (arg === "-m" || arg === "--message") {
|
|
40
|
+
return String(args[index + 1] || "").trim();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (arg.startsWith("-m=")) {
|
|
44
|
+
return arg.slice(3).trim();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (arg.startsWith("--message=")) {
|
|
48
|
+
return arg.slice(10).trim();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return "";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function postVersionFileAndMsg(publishText, extraParams = {}) {
|
|
56
|
+
const {
|
|
57
|
+
projectName,
|
|
58
|
+
newFileName,
|
|
59
|
+
gitInfo,
|
|
60
|
+
webhookUrl = "", // 启用企业微信推送
|
|
61
|
+
webhookAtUser = [],
|
|
62
|
+
webhookExtraText = "",
|
|
63
|
+
gitAutoPushRepos,
|
|
64
|
+
gitAutoPushReposBranchMap,
|
|
65
|
+
repos,
|
|
66
|
+
deployUrl = ""
|
|
67
|
+
} = extraParams || {};
|
|
68
|
+
const { branch, commitMsg, author } = gitInfo || {};
|
|
69
|
+
const webhookMessage = getWebhookMessage();
|
|
70
|
+
const GITPATH = `https://gitee.com/gdinfly_1/${projectName}/blob/${branch}/${newFileName}`;
|
|
71
|
+
const UPLOADURL = `https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=&type=file`;
|
|
72
|
+
/* const data = {
|
|
73
|
+
msgtype: "markdown",
|
|
74
|
+
markdown: {
|
|
75
|
+
content: `${publishText}\n构建文件:[点击下载](${GITPATH})`,
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
if (Array.isArray(webhookAtUser) && webhookAtUser.length > 0) {
|
|
79
|
+
webhookAtUser.forEach((item, index) => {
|
|
80
|
+
data.markdown.content = `${data.markdown.content}\n<@${item}>`;
|
|
81
|
+
});
|
|
82
|
+
} */
|
|
83
|
+
let content = publishText;
|
|
84
|
+
if (gitAutoPushReposBranchMap) {
|
|
85
|
+
content = [
|
|
86
|
+
publishText,
|
|
87
|
+
`构建文件:已推送对应仓库【${repos}】- 分支【${gitAutoPushReposBranchMap[branch]}】`,
|
|
88
|
+
`构建人:${author}`,
|
|
89
|
+
deployUrl ? `部署系统:${deployUrl}` : ""
|
|
90
|
+
].filter(Boolean).join("\n");
|
|
91
|
+
} else {
|
|
92
|
+
content = `${publishText}\n构建文件下载地址:${GITPATH}\n${webhookExtraText}\n`;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (webhookMessage) {
|
|
96
|
+
content = `${content}\n${webhookMessage}`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 防御性过滤:防止运行时数据中的 %s 等格式化占位符泄露到推送内容
|
|
100
|
+
content = (content || "").replace(/%s/g, "");
|
|
101
|
+
|
|
102
|
+
const data = {
|
|
103
|
+
msgtype: "text",
|
|
104
|
+
text: {
|
|
105
|
+
content,
|
|
106
|
+
mentioned_mobile_list: Array.isArray(webhookAtUser) ? webhookAtUser : webhookAtUser[branch] // 支持按分支配置不同的提醒人
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
if (shouldSkipWebhook()) {
|
|
111
|
+
console.log(global.logColor?.info || "%s", "🔕 已跳过企业微信机器人推送(nowebhook)");
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
const axios = require("axios");
|
|
117
|
+
|
|
118
|
+
if (axios) {
|
|
119
|
+
axios
|
|
120
|
+
.post(webhookUrl, data, {
|
|
121
|
+
headers: {
|
|
122
|
+
"Content-Type": "application/json"
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
.catch((e) => {
|
|
126
|
+
console.error("请求出错:", e);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
} catch (error) {
|
|
130
|
+
console.error("axios is not available, please install it.");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
module.exports = {
|
|
135
|
+
postVersionFileAndMsg
|
|
136
|
+
};
|
|
@@ -1,98 +1,98 @@
|
|
|
1
|
-
const { execFileSync } = require("node:child_process");
|
|
2
|
-
const fs = require("node:fs");
|
|
3
|
-
const path = require("node:path");
|
|
4
|
-
|
|
1
|
+
const { execFileSync } = require("node:child_process");
|
|
2
|
+
const fs = require("node:fs");
|
|
3
|
+
const path = require("node:path");
|
|
4
|
+
|
|
5
5
|
function bumpComposeImageVersion(content, bump = "patch") {
|
|
6
6
|
if (!["none", "patch"].includes(bump)) {
|
|
7
7
|
throw new Error(`Unsupported version bump: ${bump}. Supported values: none, patch.`);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const imageRegex = /^(\s*image:\s*)([^\s#]+):(\d+)\.(\d+)\.(\d+)(\s*(?:#.*)?)$/gm;
|
|
11
|
-
const matches = [...content.matchAll(imageRegex)];
|
|
12
|
-
|
|
13
|
-
if (matches.length !== 1) {
|
|
14
|
-
throw new Error(`Compose file must contain exactly one versioned image; found ${matches.length}.`);
|
|
15
|
-
}
|
|
16
|
-
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const imageRegex = /^(\s*image:\s*)([^\s#]+):(\d+)\.(\d+)\.(\d+)(\s*(?:#.*)?)$/gm;
|
|
11
|
+
const matches = [...content.matchAll(imageRegex)];
|
|
12
|
+
|
|
13
|
+
if (matches.length !== 1) {
|
|
14
|
+
throw new Error(`Compose file must contain exactly one versioned image; found ${matches.length}.`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
17
|
const [, prefix, image, major, minor, patchVersion, suffix] = matches[0];
|
|
18
18
|
const from = `${image}:${major}.${minor}.${patchVersion}`;
|
|
19
19
|
const to = bump === "none"
|
|
20
20
|
? from
|
|
21
21
|
: `${image}:${major}.${minor}.${Number(patchVersion) + 1}`;
|
|
22
22
|
const replacement = `${prefix}${to}${suffix}`;
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
content: content.replace(imageRegex, replacement),
|
|
26
|
-
from,
|
|
27
|
-
to,
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function resolveComposePath(projectDir, file) {
|
|
32
|
-
if (!projectDir) throw new Error("--project-dir is required.");
|
|
33
|
-
if (!file) throw new Error("--file is required.");
|
|
34
|
-
|
|
35
|
-
const resolvedProjectDir = path.resolve(projectDir);
|
|
36
|
-
const composePath = path.resolve(resolvedProjectDir, file);
|
|
37
|
-
const relative = path.relative(resolvedProjectDir, composePath);
|
|
38
|
-
|
|
39
|
-
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
40
|
-
throw new Error("Compose file must be inside project-dir.");
|
|
41
|
-
}
|
|
42
|
-
if (!fs.existsSync(composePath)) {
|
|
43
|
-
throw new Error(`Compose file not found: ${composePath}`);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return { projectDir: resolvedProjectDir, composePath };
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function defaultRunDocker(args, options) {
|
|
50
|
-
execFileSync("docker", args, { cwd: options.cwd, stdio: "inherit" });
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function composeRelease(options = {}, dependencies = {}) {
|
|
54
|
-
const {
|
|
55
|
-
projectDir,
|
|
56
|
-
file,
|
|
57
|
-
bump = "patch",
|
|
58
|
-
push = false,
|
|
59
|
-
dryRun = false,
|
|
60
|
-
} = options;
|
|
61
|
-
const runDocker = dependencies.runDocker || defaultRunDocker;
|
|
62
|
-
const resolved = resolveComposePath(projectDir, file);
|
|
63
|
-
const original = fs.readFileSync(resolved.composePath, "utf8");
|
|
64
|
-
const updated = bumpComposeImageVersion(original, bump);
|
|
65
|
-
const baseArgs = ["compose", "-f", resolved.composePath];
|
|
66
|
-
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
content: content.replace(imageRegex, replacement),
|
|
26
|
+
from,
|
|
27
|
+
to,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function resolveComposePath(projectDir, file) {
|
|
32
|
+
if (!projectDir) throw new Error("--project-dir is required.");
|
|
33
|
+
if (!file) throw new Error("--file is required.");
|
|
34
|
+
|
|
35
|
+
const resolvedProjectDir = path.resolve(projectDir);
|
|
36
|
+
const composePath = path.resolve(resolvedProjectDir, file);
|
|
37
|
+
const relative = path.relative(resolvedProjectDir, composePath);
|
|
38
|
+
|
|
39
|
+
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
40
|
+
throw new Error("Compose file must be inside project-dir.");
|
|
41
|
+
}
|
|
42
|
+
if (!fs.existsSync(composePath)) {
|
|
43
|
+
throw new Error(`Compose file not found: ${composePath}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return { projectDir: resolvedProjectDir, composePath };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function defaultRunDocker(args, options) {
|
|
50
|
+
execFileSync("docker", args, { cwd: options.cwd, stdio: "inherit" });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function composeRelease(options = {}, dependencies = {}) {
|
|
54
|
+
const {
|
|
55
|
+
projectDir,
|
|
56
|
+
file,
|
|
57
|
+
bump = "patch",
|
|
58
|
+
push = false,
|
|
59
|
+
dryRun = false,
|
|
60
|
+
} = options;
|
|
61
|
+
const runDocker = dependencies.runDocker || defaultRunDocker;
|
|
62
|
+
const resolved = resolveComposePath(projectDir, file);
|
|
63
|
+
const original = fs.readFileSync(resolved.composePath, "utf8");
|
|
64
|
+
const updated = bumpComposeImageVersion(original, bump);
|
|
65
|
+
const baseArgs = ["compose", "-f", resolved.composePath];
|
|
66
|
+
|
|
67
67
|
if (bump === "none") {
|
|
68
68
|
console.log(`Version fixed: ${updated.from}`);
|
|
69
69
|
} else {
|
|
70
70
|
console.log(`Version update: ${updated.from} -> ${updated.to}`);
|
|
71
71
|
}
|
|
72
|
-
|
|
73
|
-
if (dryRun) {
|
|
74
|
-
console.log(`[dry-run] docker ${[...baseArgs, "config"].join(" ")}`);
|
|
75
|
-
console.log(`[dry-run] docker ${[...baseArgs, "build"].join(" ")}`);
|
|
76
|
-
if (push) console.log(`[dry-run] docker ${[...baseArgs, "push"].join(" ")}`);
|
|
77
|
-
return { ...updated, projectDir: resolved.projectDir, composePath: resolved.composePath };
|
|
78
|
-
}
|
|
79
|
-
|
|
72
|
+
|
|
73
|
+
if (dryRun) {
|
|
74
|
+
console.log(`[dry-run] docker ${[...baseArgs, "config"].join(" ")}`);
|
|
75
|
+
console.log(`[dry-run] docker ${[...baseArgs, "build"].join(" ")}`);
|
|
76
|
+
if (push) console.log(`[dry-run] docker ${[...baseArgs, "push"].join(" ")}`);
|
|
77
|
+
return { ...updated, projectDir: resolved.projectDir, composePath: resolved.composePath };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
80
|
const versionChanged = updated.content !== original;
|
|
81
81
|
if (versionChanged) fs.writeFileSync(resolved.composePath, updated.content, "utf8");
|
|
82
82
|
try {
|
|
83
|
-
runDocker([...baseArgs, "config"], { cwd: resolved.projectDir });
|
|
84
|
-
runDocker([...baseArgs, "build"], { cwd: resolved.projectDir });
|
|
85
|
-
if (push) runDocker([...baseArgs, "push"], { cwd: resolved.projectDir });
|
|
83
|
+
runDocker([...baseArgs, "config"], { cwd: resolved.projectDir });
|
|
84
|
+
runDocker([...baseArgs, "build"], { cwd: resolved.projectDir });
|
|
85
|
+
if (push) runDocker([...baseArgs, "push"], { cwd: resolved.projectDir });
|
|
86
86
|
} catch (error) {
|
|
87
87
|
if (versionChanged) fs.writeFileSync(resolved.composePath, original, "utf8");
|
|
88
|
-
throw error;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return { ...updated, projectDir: resolved.projectDir, composePath: resolved.composePath };
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
module.exports = {
|
|
95
|
-
bumpComposeImageVersion,
|
|
96
|
-
composeRelease,
|
|
97
|
-
resolveComposePath,
|
|
98
|
-
};
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return { ...updated, projectDir: resolved.projectDir, composePath: resolved.composePath };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
module.exports = {
|
|
95
|
+
bumpComposeImageVersion,
|
|
96
|
+
composeRelease,
|
|
97
|
+
resolveComposePath,
|
|
98
|
+
};
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* [--health-check] [--health-timeout SECONDS] [--health-add-host HOST:IP]
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
|
-
const { execSync } = require("child_process");
|
|
20
|
+
const { execSync, spawn } = require("child_process");
|
|
21
21
|
const fs = require("fs");
|
|
22
22
|
const path = require("path");
|
|
23
23
|
|
|
@@ -125,6 +125,83 @@ function run(cmd, { silent = false, dryRun = false, cwd } = {}) {
|
|
|
125
125
|
});
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
+
function isDockerContextBusyError(error) {
|
|
129
|
+
const detail = [error?.message, error?.stderr]
|
|
130
|
+
.filter(Boolean)
|
|
131
|
+
.map(String)
|
|
132
|
+
.join("\n");
|
|
133
|
+
return /unable to resolve docker endpoint/i.test(detail)
|
|
134
|
+
&& /contexts[\\/]meta/i.test(detail)
|
|
135
|
+
&& /(being used by another process|used by another process|process cannot access the file)/i.test(detail);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function executeDocker(commandArgs, { cwd } = {}) {
|
|
139
|
+
return new Promise((resolve, reject) => {
|
|
140
|
+
let stderr = "";
|
|
141
|
+
let settled = false;
|
|
142
|
+
const child = spawn("docker", commandArgs, {
|
|
143
|
+
cwd: cwd || process.cwd(),
|
|
144
|
+
env: process.env,
|
|
145
|
+
shell: false,
|
|
146
|
+
stdio: ["inherit", "pipe", "pipe"],
|
|
147
|
+
windowsHide: true,
|
|
148
|
+
});
|
|
149
|
+
const finish = (callback) => {
|
|
150
|
+
if (settled) return;
|
|
151
|
+
settled = true;
|
|
152
|
+
callback();
|
|
153
|
+
};
|
|
154
|
+
child.stdout.on("data", (chunk) => process.stdout.write(chunk));
|
|
155
|
+
child.stderr.on("data", (chunk) => {
|
|
156
|
+
stderr = `${stderr}${chunk}`.slice(-128 * 1024);
|
|
157
|
+
process.stderr.write(chunk);
|
|
158
|
+
});
|
|
159
|
+
child.once("error", (cause) => finish(() => {
|
|
160
|
+
const error = new Error(`docker ${commandArgs[0]} 无法启动: ${cause.message}`, { cause });
|
|
161
|
+
error.stderr = stderr;
|
|
162
|
+
reject(error);
|
|
163
|
+
}));
|
|
164
|
+
child.once("close", (code) => finish(() => {
|
|
165
|
+
if (code === 0) {
|
|
166
|
+
resolve();
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const error = new Error(`docker ${commandArgs[0]} 执行失败,退出码: ${code}`);
|
|
170
|
+
error.status = code;
|
|
171
|
+
error.stderr = stderr;
|
|
172
|
+
reject(error);
|
|
173
|
+
}));
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function shouldUseDockerTtyProgress({
|
|
178
|
+
local,
|
|
179
|
+
platform = process.platform,
|
|
180
|
+
stdoutIsTTY = process.stdout.isTTY,
|
|
181
|
+
stderrIsTTY = process.stderr.isTTY,
|
|
182
|
+
} = {}) {
|
|
183
|
+
// Windows Docker CLI 无法在当前管道化 stderr 上初始化 TTY console;强制 tty 会直接报 handle invalid。
|
|
184
|
+
return Boolean(local && platform !== "win32" && stdoutIsTTY && stderrIsTTY);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function runDockerBuildWithRetry(commandArgs, {
|
|
188
|
+
cwd,
|
|
189
|
+
local = false,
|
|
190
|
+
platform = process.platform,
|
|
191
|
+
execute = executeDocker,
|
|
192
|
+
sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
193
|
+
log = console.warn,
|
|
194
|
+
} = {}) {
|
|
195
|
+
try {
|
|
196
|
+
await execute(commandArgs, { cwd });
|
|
197
|
+
} catch (error) {
|
|
198
|
+
if (!local || platform !== "win32" || !isDockerContextBusyError(error)) throw error;
|
|
199
|
+
log("检测到 Docker context 元数据被临时占用,1 秒后自动重试构建…");
|
|
200
|
+
await sleep(1000);
|
|
201
|
+
await execute(commandArgs, { cwd });
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
128
205
|
function normalizeHealthOptions({ healthTimeout = "60", healthAddHost } = {}) {
|
|
129
206
|
const timeoutSeconds = Number(healthTimeout);
|
|
130
207
|
if (!Number.isInteger(timeoutSeconds) || timeoutSeconds < 1 || timeoutSeconds > 600) {
|
|
@@ -348,9 +425,21 @@ async function dockerBuildPush(options = {}) {
|
|
|
348
425
|
|
|
349
426
|
// 7. 构建
|
|
350
427
|
console.log("\x1b[0;32m>>> 开始构建 Docker 镜像\x1b[0m");
|
|
351
|
-
const
|
|
352
|
-
|
|
353
|
-
|
|
428
|
+
const buildArgs = ["build", "-f", dockerfileArg];
|
|
429
|
+
tags.forEach((tag) => buildArgs.push("-t", tag));
|
|
430
|
+
buildArgs.push(
|
|
431
|
+
"--build-arg", `GIT_COMMIT=${fullCommit}`,
|
|
432
|
+
"--label", `org.opencontainers.image.revision=${fullCommit}`,
|
|
433
|
+
"--label", `org.opencontainers.image.version=${commitHash}`
|
|
434
|
+
);
|
|
435
|
+
// 捕获 stderr 会让 Docker 子进程看到管道;非 Windows 本地交互终端可显式保留彩色紧凑输出。
|
|
436
|
+
if (shouldUseDockerTtyProgress({ local })) buildArgs.push("--progress=tty");
|
|
437
|
+
buildArgs.push(".");
|
|
438
|
+
if (dryRun) {
|
|
439
|
+
console.log(` [DRY RUN] docker ${buildArgs.join(" ")}`);
|
|
440
|
+
} else {
|
|
441
|
+
await runDockerBuildWithRetry(buildArgs, { cwd: buildContext, local });
|
|
442
|
+
}
|
|
354
443
|
console.log("\x1b[0;32m✓ 镜像构建完成\x1b[0m");
|
|
355
444
|
|
|
356
445
|
// 8. 可选镜像健康门禁(失败会抛错,任何标签都不会推送)
|
|
@@ -395,7 +484,10 @@ module.exports = {
|
|
|
395
484
|
detectMode,
|
|
396
485
|
formatCicdLink,
|
|
397
486
|
healthCheckImage,
|
|
487
|
+
isDockerContextBusyError,
|
|
398
488
|
normalizeHealthOptions,
|
|
399
489
|
normalizeTagPrefix,
|
|
400
490
|
resolveImageTags,
|
|
491
|
+
runDockerBuildWithRetry,
|
|
492
|
+
shouldUseDockerTtyProgress,
|
|
401
493
|
};
|
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
|
+
};
|