@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,521 +1,521 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const path = require("path");
|
|
3
|
-
const crypto = require("crypto");
|
|
4
|
-
const { execSync, exec } = require("child_process");
|
|
5
|
-
const { promisify } = require("util");
|
|
6
|
-
const { getGitCommonDir } = require("./git-utils");
|
|
7
|
-
const execAsync = promisify(exec);
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* 获取当前 Git 分支名称
|
|
11
|
-
* @returns
|
|
12
|
-
*/
|
|
13
|
-
function getCurrentBranch() {
|
|
14
|
-
return execSync("git rev-parse --abbrev-ref HEAD").toString().trim();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 获取 Git 提交信息
|
|
19
|
-
* @returns {Object} 包含提交信息的对象,如果获取失败则返回空对象
|
|
20
|
-
*/
|
|
21
|
-
function getLatestCommitInfo() {
|
|
22
|
-
// 定义 Git 命令
|
|
23
|
-
const commands = {
|
|
24
|
-
commitMsg: "git log -1 --no-merges --pretty=%B",
|
|
25
|
-
branch: "git rev-parse --abbrev-ref HEAD",
|
|
26
|
-
fullHash: "git rev-parse HEAD",
|
|
27
|
-
author: "git log -1 --no-merges --pretty=%an",
|
|
28
|
-
email: "git log -1 --no-merges --pretty=%ae",
|
|
29
|
-
date: "git log -1 --no-merges --pretty=%cd --date=iso"
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
// 结果对象
|
|
33
|
-
const commitInfo = {};
|
|
34
|
-
|
|
35
|
-
try {
|
|
36
|
-
// 检查当前目录是否是 Git 仓库
|
|
37
|
-
execSync("git rev-parse --is-inside-work-tree", { stdio: "ignore" });
|
|
38
|
-
|
|
39
|
-
// 遍历命令并执行
|
|
40
|
-
for (const [key, command] of Object.entries(commands)) {
|
|
41
|
-
try {
|
|
42
|
-
commitInfo[key] = execSync(`${command}`, { encoding: "utf-8" }).trim();
|
|
43
|
-
} catch (cmdError) {
|
|
44
|
-
console.warn(global.logColor.warning, `⚠️️ 获取 Git 信息失败(${key}):${cmdError.message}`);
|
|
45
|
-
commitInfo[key] = ""; // 失败时设置为空字符串
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
} catch (error) {
|
|
49
|
-
console.error(
|
|
50
|
-
global.logColor.error,
|
|
51
|
-
`❌ 获取 Git 信息失败:当前目录不是 Git 仓库或 Git 未安装。错误信息:${error.message}`
|
|
52
|
-
);
|
|
53
|
-
return {}; // 返回空对象
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return commitInfo;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* 获取指定文件的最后一次提交信息
|
|
61
|
-
* @param {String} filePath - 文件路径
|
|
62
|
-
* @returns {Object|null} 包含提交信息的对象,如果获取失败则返回 null
|
|
63
|
-
*/
|
|
64
|
-
function getLastCommitByFile(filePath) {
|
|
65
|
-
try {
|
|
66
|
-
// 执行 Git 命令获取最后一次提交信息:ml-citation{ref="3,5" data="citationList"}
|
|
67
|
-
const cmd = `git log -1 --format="%H|%an|%ae|%at|%s" -- ${filePath}`;
|
|
68
|
-
const output = execSync(cmd, { encoding: "utf8" }).trim();
|
|
69
|
-
if (!output) return null;
|
|
70
|
-
|
|
71
|
-
// 解析提交信息:ml-citation{ref="1,5" data="citationList"}
|
|
72
|
-
const [hash, author, email, timestamp, message] = output.split("|");
|
|
73
|
-
return {
|
|
74
|
-
commitMsg: message.trim(),
|
|
75
|
-
hash,
|
|
76
|
-
author,
|
|
77
|
-
email,
|
|
78
|
-
date: new Date(parseInt(timestamp) * 1000).toISOString()
|
|
79
|
-
};
|
|
80
|
-
} catch {
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* 检查是否构建文件
|
|
87
|
-
* @param {String} targetProjectOutputDir 构建文件输出绝对路径
|
|
88
|
-
* @param {String} gitStatusLine 带文件状态的
|
|
89
|
-
* @returns
|
|
90
|
-
*/
|
|
91
|
-
function isBuildFile(targetProjectOutputDir = "", gitLine = "", sliceLen = -1) {
|
|
92
|
-
if (!targetProjectOutputDir) {
|
|
93
|
-
return true;
|
|
94
|
-
}
|
|
95
|
-
// 统一路径分隔符
|
|
96
|
-
targetProjectOutputDir = targetProjectOutputDir.replace(/\\/g, "/");
|
|
97
|
-
|
|
98
|
-
// 取目录的最后 N 级,比如 frontend/ea
|
|
99
|
-
const dirFragment = targetProjectOutputDir.split("/").slice(sliceLen).join("/");
|
|
100
|
-
|
|
101
|
-
// 去掉状态前缀 (M, A, D, ?? 等)
|
|
102
|
-
const gitPath = gitLine.trim().slice(2).trim();
|
|
103
|
-
|
|
104
|
-
// 判断是否以 dirFragment 开头
|
|
105
|
-
return gitPath.startsWith(dirFragment + "/");
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* 检查 Git 状态
|
|
110
|
-
* @param {String} excludeDir - 排除的目录
|
|
111
|
-
* @param {String} extraFilter - 额外过滤条件
|
|
112
|
-
* @param {Object} extraParams - 额外的配置
|
|
113
|
-
*/
|
|
114
|
-
function checkGitStatus(excludeDir, extraFilter, extraParams) {
|
|
115
|
-
const {
|
|
116
|
-
deleteOldFile = [],
|
|
117
|
-
versionFileName = "version-config.json",
|
|
118
|
-
gitAutoPushRepos,
|
|
119
|
-
targetProjectOutputDir,
|
|
120
|
-
targetProjectOutputDirCmd = gitAutoPushRepos ? ` -C ${targetProjectOutputDir}` : "",
|
|
121
|
-
onlyCheckStatus,
|
|
122
|
-
startLogText = "Git自动提交开始执行, 正在检查非构建文件"
|
|
123
|
-
} = extraParams || {};
|
|
124
|
-
|
|
125
|
-
// 如果指定了构建输出目录但目录不存在,则自动创建并初始化 Git 仓库
|
|
126
|
-
if (targetProjectOutputDir && !fs.existsSync(targetProjectOutputDir)) {
|
|
127
|
-
console.log(global.logColor.warning, `⚠️ 构建目录不存在,正在自动创建:${targetProjectOutputDir}`);
|
|
128
|
-
fs.mkdirSync(targetProjectOutputDir, { recursive: true });
|
|
129
|
-
console.log(global.logColor.success, `✅ 构建目录已创建并初始化 Git 仓库:${targetProjectOutputDir}`);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
try {
|
|
133
|
-
// 获取所有未跟踪和已修改的文件列表
|
|
134
|
-
const files = execSync(`git${targetProjectOutputDirCmd} status --porcelain -z`, { encoding: "utf8" })
|
|
135
|
-
.toString()
|
|
136
|
-
.split("\u0000")
|
|
137
|
-
.filter((file) => file.trim() && isBuildFile(targetProjectOutputDir, file));
|
|
138
|
-
const excludeFiles = []; // 非指定构建相关文件
|
|
139
|
-
const includeFiles = []; // 指定的构建相关文件
|
|
140
|
-
|
|
141
|
-
if (onlyCheckStatus || targetProjectOutputDirCmd) {
|
|
142
|
-
return files || [];
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
console.log(global.logColor.success, `🚀 ${startLogText}`);
|
|
146
|
-
|
|
147
|
-
// 进行当前项目文件检查,如果有其它非配置文件则取消自动提交改为用户手动确认
|
|
148
|
-
files.forEach((file) => {
|
|
149
|
-
const existsDelFile = deleteOldFile.find((item) => file.includes(item));
|
|
150
|
-
if (
|
|
151
|
-
file.includes(`${excludeDir}/`) ||
|
|
152
|
-
file.includes(versionFileName) ||
|
|
153
|
-
file.includes(extraFilter) ||
|
|
154
|
-
existsDelFile
|
|
155
|
-
) {
|
|
156
|
-
includeFiles.push(file);
|
|
157
|
-
} else {
|
|
158
|
-
excludeFiles.push(file);
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
if (excludeFiles.length > 0) {
|
|
163
|
-
throw new Error(`存在非构建文件未提交,请手动确定提交信息,本次自动提交终止\n${excludeFiles.join("\n")}`);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return includeFiles || [];
|
|
167
|
-
} catch (err) {
|
|
168
|
-
console.error(global.logColor.error, `❌ 自动化构建Git状态检查存在问题:${err.message}`);
|
|
169
|
-
process.exit(1);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
function runGitCommand(command, options = {}) {
|
|
174
|
-
try {
|
|
175
|
-
const result = execSync(command, {
|
|
176
|
-
...options,
|
|
177
|
-
encoding: "utf8"
|
|
178
|
-
});
|
|
179
|
-
if (result) {
|
|
180
|
-
return result.trim();
|
|
181
|
-
}
|
|
182
|
-
} catch (error) {
|
|
183
|
-
console.error(global.logColor.error, `❌ 执行 Git 命令失败:${error.message}`);
|
|
184
|
-
process.exit(1);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
function hasStagedChanges(targetProjectOutputDirCmd = "") {
|
|
189
|
-
try {
|
|
190
|
-
execSync(`git${targetProjectOutputDirCmd} diff --cached --quiet`, {
|
|
191
|
-
stdio: "ignore"
|
|
192
|
-
});
|
|
193
|
-
return false;
|
|
194
|
-
} catch (error) {
|
|
195
|
-
return error.status === 1;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function sleepSync(ms) {
|
|
200
|
-
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function withGitRepoLock(lockDir, lockKey, task) {
|
|
204
|
-
if (!lockDir || !lockKey) {
|
|
205
|
-
return task();
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
fs.mkdirSync(lockDir, { recursive: true });
|
|
209
|
-
const lockName = crypto.createHash("md5").update(lockKey).digest("hex");
|
|
210
|
-
const lockPath = path.join(lockDir, `${lockName}.lock`);
|
|
211
|
-
const startTime = Date.now();
|
|
212
|
-
const timeout = 10 * 60 * 1000;
|
|
213
|
-
|
|
214
|
-
while (true) {
|
|
215
|
-
try {
|
|
216
|
-
fs.mkdirSync(lockPath);
|
|
217
|
-
break;
|
|
218
|
-
} catch (error) {
|
|
219
|
-
if (error.code !== "EEXIST") {
|
|
220
|
-
throw error;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if (Date.now() - startTime > timeout) {
|
|
224
|
-
throw new Error(`等待 Git 仓库锁超时: ${lockKey}`);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
sleepSync(500);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
try {
|
|
232
|
-
return task();
|
|
233
|
-
} finally {
|
|
234
|
-
fs.rmSync(lockPath, { recursive: true, force: true });
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* git自动推送
|
|
240
|
-
* @param {String} buildFoldName 构建文件名称
|
|
241
|
-
* @param {String} newFileName 新压缩文件名
|
|
242
|
-
* @param {Object} extraParams 额外的参数
|
|
243
|
-
* @returns
|
|
244
|
-
*/
|
|
245
|
-
function autoGitProcess(buildFoldName, newFileName, extraParams = {}) {
|
|
246
|
-
const {
|
|
247
|
-
newVersion,
|
|
248
|
-
gitInfo = {},
|
|
249
|
-
gitAutoCommitText = gitInfo.commitMsg,
|
|
250
|
-
gitAutoPushRepos,
|
|
251
|
-
targetProjectOutputDir,
|
|
252
|
-
gitAutoPushReposBranchMap = {},
|
|
253
|
-
targetProjectOutputDirCmd = gitAutoPushRepos ? ` -C ${targetProjectOutputDir}` : "",
|
|
254
|
-
projectName,
|
|
255
|
-
gitLockDir,
|
|
256
|
-
gitProcessOrder
|
|
257
|
-
} = extraParams || {};
|
|
258
|
-
const appFirst = gitAutoPushRepos && gitProcessOrder === "app-first";
|
|
259
|
-
const runAppGitProcess = () => {
|
|
260
|
-
const overrideParams = {
|
|
261
|
-
...extraParams,
|
|
262
|
-
targetProjectOutputDir: "",
|
|
263
|
-
targetProjectOutputDirCmd: "",
|
|
264
|
-
gitAutoPushRepos: false
|
|
265
|
-
};
|
|
266
|
-
autoGitProcess(buildFoldName, newFileName, overrideParams);
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
if (appFirst) {
|
|
270
|
-
runAppGitProcess();
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
const { commitMsg, branch } = gitInfo || {};
|
|
274
|
-
const gitRepos = gitAutoPushRepos ? `构建仓库: ${targetProjectOutputDir}` : `当前项目: ${projectName}`;
|
|
275
|
-
let tempGitAutoCommitText = gitAutoCommitText ? gitAutoCommitText.replace("{version}", newVersion) : "";
|
|
276
|
-
|
|
277
|
-
if (gitAutoPushRepos) {
|
|
278
|
-
tempGitAutoCommitText = commitMsg;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const tempBranch = gitAutoPushRepos ? gitAutoPushReposBranchMap[branch] : branch;
|
|
282
|
-
const addCmd = `git${targetProjectOutputDirCmd} add -A`;
|
|
283
|
-
const commitCmd = `git${targetProjectOutputDirCmd} commit -m "${tempGitAutoCommitText}"`;
|
|
284
|
-
const pushCommand = `git${targetProjectOutputDirCmd} push origin ${tempBranch}`;
|
|
285
|
-
|
|
286
|
-
if (!tempBranch) {
|
|
287
|
-
console.log(global.logColor.error, `❌ 缺少分支配置`);
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
const checkCommitAndPush = () => {
|
|
292
|
-
const fileList = checkGitStatus(buildFoldName, newFileName, extraParams) || [];
|
|
293
|
-
|
|
294
|
-
if (fileList.length === 0) {
|
|
295
|
-
return;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
runGitCommand(addCmd);
|
|
299
|
-
if (!hasStagedChanges(targetProjectOutputDirCmd)) {
|
|
300
|
-
console.log(global.logColor.warning, `⚠️ Git ${gitRepos} 没有可提交的变更,跳过自动提交`);
|
|
301
|
-
} else {
|
|
302
|
-
runGitCommand(commitCmd);
|
|
303
|
-
runGitCommand(pushCommand);
|
|
304
|
-
}
|
|
305
|
-
};
|
|
306
|
-
|
|
307
|
-
if (gitAutoPushRepos) {
|
|
308
|
-
withGitRepoLock(gitLockDir, getGitCommonDir(targetProjectOutputDir), checkCommitAndPush);
|
|
309
|
-
} else {
|
|
310
|
-
checkCommitAndPush();
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
if (gitAutoPushRepos && !appFirst) {
|
|
314
|
-
runAppGitProcess();
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
console.log(global.logColor.success, `✅ Git \x1b[34m${gitRepos}\x1b[32m 自动提交和推送成功!`);
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* 自动分支处理
|
|
321
|
-
* @param {Object} config - 分支配置
|
|
322
|
-
*/
|
|
323
|
-
async function autoBranchProcess(config) {
|
|
324
|
-
const { gitAutoPushRepos, gitAutoPushReposBranchMap, currentBranch, targetProjectOutputDir, validateConfig } =
|
|
325
|
-
config || {};
|
|
326
|
-
const tempBranch = gitAutoPushRepos ? gitAutoPushReposBranchMap[currentBranch] : currentBranch;
|
|
327
|
-
const targetProjectOutputDirCmd = gitAutoPushRepos ? ` -C ${targetProjectOutputDir}` : "";
|
|
328
|
-
const checkoutCmd = `git${targetProjectOutputDirCmd} checkout ${tempBranch}`;
|
|
329
|
-
const pullCmd = `git${targetProjectOutputDirCmd} pull origin ${tempBranch}`;
|
|
330
|
-
const fileList = checkGitStatus(undefined, undefined, {
|
|
331
|
-
onlyCheckStatus: true,
|
|
332
|
-
targetProjectOutputDir,
|
|
333
|
-
targetProjectOutputDirCmd,
|
|
334
|
-
startLogText: "Git自动化开始执行..."
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
if (validateConfig && typeof validateConfig === "function") {
|
|
338
|
-
validateConfig();
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
if (!tempBranch) {
|
|
342
|
-
console.error(global.logColor.error, `❌ 缺少项目分支对应构建仓库分支配置,当前分支:${currentBranch},请检查`);
|
|
343
|
-
process.exit(1);
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
if (fileList.length > 0) {
|
|
347
|
-
console.error(global.logColor.error, `❌ 构建仓库 ${targetProjectOutputDir} 存在未提交文件,请检查`);
|
|
348
|
-
process.exit(1);
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
runGitCommand(checkoutCmd);
|
|
352
|
-
runGitCommand(pullCmd);
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* 检查 packages 下的所有模块是否有未提交的代码
|
|
357
|
-
* @param {string} packagesDir - packages 目录的绝对路径
|
|
358
|
-
* @returns {Object} 检查结果
|
|
359
|
-
*/
|
|
360
|
-
async function checkPackagesGitStatus(packagesDir) {
|
|
361
|
-
const fs = require("fs");
|
|
362
|
-
|
|
363
|
-
if (!fs.existsSync(packagesDir)) {
|
|
364
|
-
console.error(global.logColor.error, `❌ packages 目录不存在: ${packagesDir}`);
|
|
365
|
-
return { hasUncommitted: false, modules: [] };
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
const modules = fs.readdirSync(packagesDir).filter((item) => {
|
|
369
|
-
const modulePath = path.join(packagesDir, item);
|
|
370
|
-
return fs.statSync(modulePath).isDirectory();
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
// console.log(global.logColor.info, `📦 检查 packages 下的 ${modules.length} 个模块...`);
|
|
374
|
-
|
|
375
|
-
const uncommittedModules = await Promise.all(
|
|
376
|
-
modules.map(async (module) => {
|
|
377
|
-
const modulePath = path.join(packagesDir, module);
|
|
378
|
-
const gitPath = path.join(modulePath, ".git");
|
|
379
|
-
|
|
380
|
-
// 检查是否是 git 仓库
|
|
381
|
-
if (!fs.existsSync(gitPath)) {
|
|
382
|
-
console.log(global.logColor.warning, `⚠️ ${module}: 不是 git 仓库,跳过检查`);
|
|
383
|
-
return null;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
try {
|
|
387
|
-
// 切换到模块目录并检查 git status
|
|
388
|
-
const { stdout } = await execAsync("git status --porcelain", { cwd: modulePath });
|
|
389
|
-
const statusOutput = stdout.trim();
|
|
390
|
-
|
|
391
|
-
if (statusOutput) {
|
|
392
|
-
console.log(global.logColor.error, `❌ ${module}: 存在未提交的更改`);
|
|
393
|
-
console.log(global.logColor.error, ` ${statusOutput.split("\n").join("\n ")}`);
|
|
394
|
-
return {
|
|
395
|
-
name: module,
|
|
396
|
-
path: modulePath,
|
|
397
|
-
changes: statusOutput.split("\n")
|
|
398
|
-
};
|
|
399
|
-
}
|
|
400
|
-
return null;
|
|
401
|
-
} catch (error) {
|
|
402
|
-
console.error(global.logColor.error, `❌ ${module}: 检查失败 - ${error.message}`);
|
|
403
|
-
return {
|
|
404
|
-
name: module,
|
|
405
|
-
path: modulePath,
|
|
406
|
-
error: error.message
|
|
407
|
-
};
|
|
408
|
-
}
|
|
409
|
-
})
|
|
410
|
-
).then(results => results.filter(r => r !== null));
|
|
411
|
-
|
|
412
|
-
return {
|
|
413
|
-
hasUncommitted: uncommittedModules.length > 0,
|
|
414
|
-
modules: uncommittedModules,
|
|
415
|
-
totalModules: modules.length,
|
|
416
|
-
checkedModules: modules
|
|
417
|
-
};
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
/**
|
|
421
|
-
* 切换所有 packages 模块到 master 分支并拉取最新代码
|
|
422
|
-
* @param {string} packagesDir - packages 目录的绝对路径
|
|
423
|
-
* @param {string} targetBranch - 目标分支名,默认为 'master'
|
|
424
|
-
*/
|
|
425
|
-
async function pullPackagesFromMaster(packagesDir, targetBranch = "master") {
|
|
426
|
-
const fs = require("fs");
|
|
427
|
-
|
|
428
|
-
if (!fs.existsSync(packagesDir)) {
|
|
429
|
-
console.error(global.logColor.error, `❌ packages 目录不存在: ${packagesDir}`);
|
|
430
|
-
return;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
const modules = fs.readdirSync(packagesDir).filter((item) => {
|
|
434
|
-
const modulePath = path.join(packagesDir, item);
|
|
435
|
-
return fs.statSync(modulePath).isDirectory();
|
|
436
|
-
});
|
|
437
|
-
|
|
438
|
-
// console.log(global.logColor.info, `🔄 切换 packages 下的模块到 ${targetBranch} 分支并拉取最新代码...`);
|
|
439
|
-
|
|
440
|
-
const results = await Promise.all(
|
|
441
|
-
modules.map(async (module) => {
|
|
442
|
-
const modulePath = path.join(packagesDir, module);
|
|
443
|
-
const gitPath = path.join(modulePath, ".git");
|
|
444
|
-
|
|
445
|
-
// 检查是否是 git 仓库
|
|
446
|
-
if (!fs.existsSync(gitPath)) {
|
|
447
|
-
console.log(global.logColor.warning, `⚠️ ${module}: 不是 git 仓库,跳过操作`);
|
|
448
|
-
return { module, success: false, reason: "不是 git 仓库" };
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
try {
|
|
452
|
-
// 获取当前分支
|
|
453
|
-
const { stdout: branchOut } = await execAsync("git rev-parse --abbrev-ref HEAD", { cwd: modulePath });
|
|
454
|
-
const currentBranch = branchOut.trim();
|
|
455
|
-
|
|
456
|
-
// 如果不在目标分支,则切换
|
|
457
|
-
if (currentBranch !== targetBranch) {
|
|
458
|
-
await execAsync(`git checkout ${targetBranch}`, { cwd: modulePath });
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
// 拉取最新代码
|
|
462
|
-
await execAsync(`git pull origin ${targetBranch}`, { cwd: modulePath });
|
|
463
|
-
|
|
464
|
-
console.log(global.logColor.success, `✅ \x1b[34m${module}\x1b[32m[${targetBranch}]: 更新到最新代码`);
|
|
465
|
-
return { module, success: true };
|
|
466
|
-
} catch (error) {
|
|
467
|
-
console.error(global.logColor.error, `❌ ${module}: 操作失败 - ${error.message}`);
|
|
468
|
-
return { module, success: false, reason: error.message };
|
|
469
|
-
}
|
|
470
|
-
})
|
|
471
|
-
);
|
|
472
|
-
|
|
473
|
-
// 输出结果摘要
|
|
474
|
-
const successful = results.filter((r) => r.success).length;
|
|
475
|
-
const failed = results.filter((r) => !r.success).length;
|
|
476
|
-
|
|
477
|
-
// console.log(global.logColor.info, `\n📊 操作完成: 成功 ${successful} 个,失败 ${failed} 个`);
|
|
478
|
-
|
|
479
|
-
return results;
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
// 获取 packages 目录路径(相对于当前项目根目录)
|
|
483
|
-
function hasGitModules(packagesDir) {
|
|
484
|
-
if (!fs.existsSync(packagesDir)) {
|
|
485
|
-
return false;
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
return ["infly-libs", "infly-ui"].some((item) => fs.existsSync(path.join(packagesDir, item, ".git")));
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
function checkAndUpdateRootPackages(packagesDir, targetBranch = "master") {
|
|
492
|
-
const rootDir = path.resolve(packagesDir, "..");
|
|
493
|
-
const gitPath = path.join(rootDir, ".git");
|
|
494
|
-
|
|
495
|
-
if (!fs.existsSync(gitPath)) {
|
|
496
|
-
console.log(global.logColor.warning, `⚠️ packages 不属于独立 git 仓库,也未找到根仓库,跳过 packages 更新`);
|
|
497
|
-
return [];
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
const statusOutput = execSync("git status --porcelain", {
|
|
501
|
-
cwd: rootDir,
|
|
502
|
-
encoding: "utf8"
|
|
503
|
-
}).trim();
|
|
504
|
-
|
|
505
|
-
if (statusOutput) {
|
|
506
|
-
console.log(global.logColor.error, `\n❌ admin-monorepo 存在未提交的更改,无法合并 ${targetBranch}`);
|
|
507
|
-
console.log(global.logColor.error, ` ${statusOutput.split("\n").join("\n ")}`);
|
|
508
|
-
process.exit(1);
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
const currentBranch = execSync("git rev-parse --abbrev-ref HEAD", {
|
|
512
|
-
cwd: rootDir,
|
|
513
|
-
encoding: "utf8"
|
|
514
|
-
}).trim();
|
|
515
|
-
|
|
516
|
-
console.log(global.logColor.info, `🔄 保持当前分支 ${currentBranch},合并 ${targetBranch} 的最新更改...`);
|
|
517
|
-
|
|
518
|
-
// 拉取远程最新的 targetBranch
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const crypto = require("crypto");
|
|
4
|
+
const { execSync, exec } = require("child_process");
|
|
5
|
+
const { promisify } = require("util");
|
|
6
|
+
const { getGitCommonDir } = require("./git-utils");
|
|
7
|
+
const execAsync = promisify(exec);
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 获取当前 Git 分支名称
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
function getCurrentBranch() {
|
|
14
|
+
return execSync("git rev-parse --abbrev-ref HEAD").toString().trim();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 获取 Git 提交信息
|
|
19
|
+
* @returns {Object} 包含提交信息的对象,如果获取失败则返回空对象
|
|
20
|
+
*/
|
|
21
|
+
function getLatestCommitInfo() {
|
|
22
|
+
// 定义 Git 命令
|
|
23
|
+
const commands = {
|
|
24
|
+
commitMsg: "git log -1 --no-merges --pretty=%B",
|
|
25
|
+
branch: "git rev-parse --abbrev-ref HEAD",
|
|
26
|
+
fullHash: "git rev-parse HEAD",
|
|
27
|
+
author: "git log -1 --no-merges --pretty=%an",
|
|
28
|
+
email: "git log -1 --no-merges --pretty=%ae",
|
|
29
|
+
date: "git log -1 --no-merges --pretty=%cd --date=iso"
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// 结果对象
|
|
33
|
+
const commitInfo = {};
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
// 检查当前目录是否是 Git 仓库
|
|
37
|
+
execSync("git rev-parse --is-inside-work-tree", { stdio: "ignore" });
|
|
38
|
+
|
|
39
|
+
// 遍历命令并执行
|
|
40
|
+
for (const [key, command] of Object.entries(commands)) {
|
|
41
|
+
try {
|
|
42
|
+
commitInfo[key] = execSync(`${command}`, { encoding: "utf-8" }).trim();
|
|
43
|
+
} catch (cmdError) {
|
|
44
|
+
console.warn(global.logColor.warning, `⚠️️ 获取 Git 信息失败(${key}):${cmdError.message}`);
|
|
45
|
+
commitInfo[key] = ""; // 失败时设置为空字符串
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.error(
|
|
50
|
+
global.logColor.error,
|
|
51
|
+
`❌ 获取 Git 信息失败:当前目录不是 Git 仓库或 Git 未安装。错误信息:${error.message}`
|
|
52
|
+
);
|
|
53
|
+
return {}; // 返回空对象
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return commitInfo;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 获取指定文件的最后一次提交信息
|
|
61
|
+
* @param {String} filePath - 文件路径
|
|
62
|
+
* @returns {Object|null} 包含提交信息的对象,如果获取失败则返回 null
|
|
63
|
+
*/
|
|
64
|
+
function getLastCommitByFile(filePath) {
|
|
65
|
+
try {
|
|
66
|
+
// 执行 Git 命令获取最后一次提交信息:ml-citation{ref="3,5" data="citationList"}
|
|
67
|
+
const cmd = `git log -1 --format="%H|%an|%ae|%at|%s" -- ${filePath}`;
|
|
68
|
+
const output = execSync(cmd, { encoding: "utf8" }).trim();
|
|
69
|
+
if (!output) return null;
|
|
70
|
+
|
|
71
|
+
// 解析提交信息:ml-citation{ref="1,5" data="citationList"}
|
|
72
|
+
const [hash, author, email, timestamp, message] = output.split("|");
|
|
73
|
+
return {
|
|
74
|
+
commitMsg: message.trim(),
|
|
75
|
+
hash,
|
|
76
|
+
author,
|
|
77
|
+
email,
|
|
78
|
+
date: new Date(parseInt(timestamp) * 1000).toISOString()
|
|
79
|
+
};
|
|
80
|
+
} catch {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 检查是否构建文件
|
|
87
|
+
* @param {String} targetProjectOutputDir 构建文件输出绝对路径
|
|
88
|
+
* @param {String} gitStatusLine 带文件状态的
|
|
89
|
+
* @returns
|
|
90
|
+
*/
|
|
91
|
+
function isBuildFile(targetProjectOutputDir = "", gitLine = "", sliceLen = -1) {
|
|
92
|
+
if (!targetProjectOutputDir) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
// 统一路径分隔符
|
|
96
|
+
targetProjectOutputDir = targetProjectOutputDir.replace(/\\/g, "/");
|
|
97
|
+
|
|
98
|
+
// 取目录的最后 N 级,比如 frontend/ea
|
|
99
|
+
const dirFragment = targetProjectOutputDir.split("/").slice(sliceLen).join("/");
|
|
100
|
+
|
|
101
|
+
// 去掉状态前缀 (M, A, D, ?? 等)
|
|
102
|
+
const gitPath = gitLine.trim().slice(2).trim();
|
|
103
|
+
|
|
104
|
+
// 判断是否以 dirFragment 开头
|
|
105
|
+
return gitPath.startsWith(dirFragment + "/");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 检查 Git 状态
|
|
110
|
+
* @param {String} excludeDir - 排除的目录
|
|
111
|
+
* @param {String} extraFilter - 额外过滤条件
|
|
112
|
+
* @param {Object} extraParams - 额外的配置
|
|
113
|
+
*/
|
|
114
|
+
function checkGitStatus(excludeDir, extraFilter, extraParams) {
|
|
115
|
+
const {
|
|
116
|
+
deleteOldFile = [],
|
|
117
|
+
versionFileName = "version-config.json",
|
|
118
|
+
gitAutoPushRepos,
|
|
119
|
+
targetProjectOutputDir,
|
|
120
|
+
targetProjectOutputDirCmd = gitAutoPushRepos ? ` -C ${targetProjectOutputDir}` : "",
|
|
121
|
+
onlyCheckStatus,
|
|
122
|
+
startLogText = "Git自动提交开始执行, 正在检查非构建文件"
|
|
123
|
+
} = extraParams || {};
|
|
124
|
+
|
|
125
|
+
// 如果指定了构建输出目录但目录不存在,则自动创建并初始化 Git 仓库
|
|
126
|
+
if (targetProjectOutputDir && !fs.existsSync(targetProjectOutputDir)) {
|
|
127
|
+
console.log(global.logColor.warning, `⚠️ 构建目录不存在,正在自动创建:${targetProjectOutputDir}`);
|
|
128
|
+
fs.mkdirSync(targetProjectOutputDir, { recursive: true });
|
|
129
|
+
console.log(global.logColor.success, `✅ 构建目录已创建并初始化 Git 仓库:${targetProjectOutputDir}`);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
// 获取所有未跟踪和已修改的文件列表
|
|
134
|
+
const files = execSync(`git${targetProjectOutputDirCmd} status --porcelain -z`, { encoding: "utf8" })
|
|
135
|
+
.toString()
|
|
136
|
+
.split("\u0000")
|
|
137
|
+
.filter((file) => file.trim() && isBuildFile(targetProjectOutputDir, file));
|
|
138
|
+
const excludeFiles = []; // 非指定构建相关文件
|
|
139
|
+
const includeFiles = []; // 指定的构建相关文件
|
|
140
|
+
|
|
141
|
+
if (onlyCheckStatus || targetProjectOutputDirCmd) {
|
|
142
|
+
return files || [];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
console.log(global.logColor.success, `🚀 ${startLogText}`);
|
|
146
|
+
|
|
147
|
+
// 进行当前项目文件检查,如果有其它非配置文件则取消自动提交改为用户手动确认
|
|
148
|
+
files.forEach((file) => {
|
|
149
|
+
const existsDelFile = deleteOldFile.find((item) => file.includes(item));
|
|
150
|
+
if (
|
|
151
|
+
file.includes(`${excludeDir}/`) ||
|
|
152
|
+
file.includes(versionFileName) ||
|
|
153
|
+
file.includes(extraFilter) ||
|
|
154
|
+
existsDelFile
|
|
155
|
+
) {
|
|
156
|
+
includeFiles.push(file);
|
|
157
|
+
} else {
|
|
158
|
+
excludeFiles.push(file);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
if (excludeFiles.length > 0) {
|
|
163
|
+
throw new Error(`存在非构建文件未提交,请手动确定提交信息,本次自动提交终止\n${excludeFiles.join("\n")}`);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return includeFiles || [];
|
|
167
|
+
} catch (err) {
|
|
168
|
+
console.error(global.logColor.error, `❌ 自动化构建Git状态检查存在问题:${err.message}`);
|
|
169
|
+
process.exit(1);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function runGitCommand(command, options = {}) {
|
|
174
|
+
try {
|
|
175
|
+
const result = execSync(command, {
|
|
176
|
+
...options,
|
|
177
|
+
encoding: "utf8"
|
|
178
|
+
});
|
|
179
|
+
if (result) {
|
|
180
|
+
return result.trim();
|
|
181
|
+
}
|
|
182
|
+
} catch (error) {
|
|
183
|
+
console.error(global.logColor.error, `❌ 执行 Git 命令失败:${error.message}`);
|
|
184
|
+
process.exit(1);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function hasStagedChanges(targetProjectOutputDirCmd = "") {
|
|
189
|
+
try {
|
|
190
|
+
execSync(`git${targetProjectOutputDirCmd} diff --cached --quiet`, {
|
|
191
|
+
stdio: "ignore"
|
|
192
|
+
});
|
|
193
|
+
return false;
|
|
194
|
+
} catch (error) {
|
|
195
|
+
return error.status === 1;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function sleepSync(ms) {
|
|
200
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function withGitRepoLock(lockDir, lockKey, task) {
|
|
204
|
+
if (!lockDir || !lockKey) {
|
|
205
|
+
return task();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
fs.mkdirSync(lockDir, { recursive: true });
|
|
209
|
+
const lockName = crypto.createHash("md5").update(lockKey).digest("hex");
|
|
210
|
+
const lockPath = path.join(lockDir, `${lockName}.lock`);
|
|
211
|
+
const startTime = Date.now();
|
|
212
|
+
const timeout = 10 * 60 * 1000;
|
|
213
|
+
|
|
214
|
+
while (true) {
|
|
215
|
+
try {
|
|
216
|
+
fs.mkdirSync(lockPath);
|
|
217
|
+
break;
|
|
218
|
+
} catch (error) {
|
|
219
|
+
if (error.code !== "EEXIST") {
|
|
220
|
+
throw error;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (Date.now() - startTime > timeout) {
|
|
224
|
+
throw new Error(`等待 Git 仓库锁超时: ${lockKey}`);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
sleepSync(500);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
try {
|
|
232
|
+
return task();
|
|
233
|
+
} finally {
|
|
234
|
+
fs.rmSync(lockPath, { recursive: true, force: true });
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* git自动推送
|
|
240
|
+
* @param {String} buildFoldName 构建文件名称
|
|
241
|
+
* @param {String} newFileName 新压缩文件名
|
|
242
|
+
* @param {Object} extraParams 额外的参数
|
|
243
|
+
* @returns
|
|
244
|
+
*/
|
|
245
|
+
function autoGitProcess(buildFoldName, newFileName, extraParams = {}) {
|
|
246
|
+
const {
|
|
247
|
+
newVersion,
|
|
248
|
+
gitInfo = {},
|
|
249
|
+
gitAutoCommitText = gitInfo.commitMsg,
|
|
250
|
+
gitAutoPushRepos,
|
|
251
|
+
targetProjectOutputDir,
|
|
252
|
+
gitAutoPushReposBranchMap = {},
|
|
253
|
+
targetProjectOutputDirCmd = gitAutoPushRepos ? ` -C ${targetProjectOutputDir}` : "",
|
|
254
|
+
projectName,
|
|
255
|
+
gitLockDir,
|
|
256
|
+
gitProcessOrder
|
|
257
|
+
} = extraParams || {};
|
|
258
|
+
const appFirst = gitAutoPushRepos && gitProcessOrder === "app-first";
|
|
259
|
+
const runAppGitProcess = () => {
|
|
260
|
+
const overrideParams = {
|
|
261
|
+
...extraParams,
|
|
262
|
+
targetProjectOutputDir: "",
|
|
263
|
+
targetProjectOutputDirCmd: "",
|
|
264
|
+
gitAutoPushRepos: false
|
|
265
|
+
};
|
|
266
|
+
autoGitProcess(buildFoldName, newFileName, overrideParams);
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
if (appFirst) {
|
|
270
|
+
runAppGitProcess();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const { commitMsg, branch } = gitInfo || {};
|
|
274
|
+
const gitRepos = gitAutoPushRepos ? `构建仓库: ${targetProjectOutputDir}` : `当前项目: ${projectName}`;
|
|
275
|
+
let tempGitAutoCommitText = gitAutoCommitText ? gitAutoCommitText.replace("{version}", newVersion) : "";
|
|
276
|
+
|
|
277
|
+
if (gitAutoPushRepos) {
|
|
278
|
+
tempGitAutoCommitText = commitMsg;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const tempBranch = gitAutoPushRepos ? gitAutoPushReposBranchMap[branch] : branch;
|
|
282
|
+
const addCmd = `git${targetProjectOutputDirCmd} add -A`;
|
|
283
|
+
const commitCmd = `git${targetProjectOutputDirCmd} commit -m "${tempGitAutoCommitText}"`;
|
|
284
|
+
const pushCommand = `git${targetProjectOutputDirCmd} push origin ${tempBranch}`;
|
|
285
|
+
|
|
286
|
+
if (!tempBranch) {
|
|
287
|
+
console.log(global.logColor.error, `❌ 缺少分支配置`);
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const checkCommitAndPush = () => {
|
|
292
|
+
const fileList = checkGitStatus(buildFoldName, newFileName, extraParams) || [];
|
|
293
|
+
|
|
294
|
+
if (fileList.length === 0) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
runGitCommand(addCmd);
|
|
299
|
+
if (!hasStagedChanges(targetProjectOutputDirCmd)) {
|
|
300
|
+
console.log(global.logColor.warning, `⚠️ Git ${gitRepos} 没有可提交的变更,跳过自动提交`);
|
|
301
|
+
} else {
|
|
302
|
+
runGitCommand(commitCmd);
|
|
303
|
+
runGitCommand(pushCommand);
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
if (gitAutoPushRepos) {
|
|
308
|
+
withGitRepoLock(gitLockDir, getGitCommonDir(targetProjectOutputDir), checkCommitAndPush);
|
|
309
|
+
} else {
|
|
310
|
+
checkCommitAndPush();
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (gitAutoPushRepos && !appFirst) {
|
|
314
|
+
runAppGitProcess();
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
console.log(global.logColor.success, `✅ Git \x1b[34m${gitRepos}\x1b[32m 自动提交和推送成功!`);
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* 自动分支处理
|
|
321
|
+
* @param {Object} config - 分支配置
|
|
322
|
+
*/
|
|
323
|
+
async function autoBranchProcess(config) {
|
|
324
|
+
const { gitAutoPushRepos, gitAutoPushReposBranchMap, currentBranch, targetProjectOutputDir, validateConfig } =
|
|
325
|
+
config || {};
|
|
326
|
+
const tempBranch = gitAutoPushRepos ? gitAutoPushReposBranchMap[currentBranch] : currentBranch;
|
|
327
|
+
const targetProjectOutputDirCmd = gitAutoPushRepos ? ` -C ${targetProjectOutputDir}` : "";
|
|
328
|
+
const checkoutCmd = `git${targetProjectOutputDirCmd} checkout ${tempBranch}`;
|
|
329
|
+
const pullCmd = `git${targetProjectOutputDirCmd} pull origin ${tempBranch}`;
|
|
330
|
+
const fileList = checkGitStatus(undefined, undefined, {
|
|
331
|
+
onlyCheckStatus: true,
|
|
332
|
+
targetProjectOutputDir,
|
|
333
|
+
targetProjectOutputDirCmd,
|
|
334
|
+
startLogText: "Git自动化开始执行..."
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
if (validateConfig && typeof validateConfig === "function") {
|
|
338
|
+
validateConfig();
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (!tempBranch) {
|
|
342
|
+
console.error(global.logColor.error, `❌ 缺少项目分支对应构建仓库分支配置,当前分支:${currentBranch},请检查`);
|
|
343
|
+
process.exit(1);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (fileList.length > 0) {
|
|
347
|
+
console.error(global.logColor.error, `❌ 构建仓库 ${targetProjectOutputDir} 存在未提交文件,请检查`);
|
|
348
|
+
process.exit(1);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
runGitCommand(checkoutCmd);
|
|
352
|
+
runGitCommand(pullCmd);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* 检查 packages 下的所有模块是否有未提交的代码
|
|
357
|
+
* @param {string} packagesDir - packages 目录的绝对路径
|
|
358
|
+
* @returns {Object} 检查结果
|
|
359
|
+
*/
|
|
360
|
+
async function checkPackagesGitStatus(packagesDir) {
|
|
361
|
+
const fs = require("fs");
|
|
362
|
+
|
|
363
|
+
if (!fs.existsSync(packagesDir)) {
|
|
364
|
+
console.error(global.logColor.error, `❌ packages 目录不存在: ${packagesDir}`);
|
|
365
|
+
return { hasUncommitted: false, modules: [] };
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const modules = fs.readdirSync(packagesDir).filter((item) => {
|
|
369
|
+
const modulePath = path.join(packagesDir, item);
|
|
370
|
+
return fs.statSync(modulePath).isDirectory();
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
// console.log(global.logColor.info, `📦 检查 packages 下的 ${modules.length} 个模块...`);
|
|
374
|
+
|
|
375
|
+
const uncommittedModules = await Promise.all(
|
|
376
|
+
modules.map(async (module) => {
|
|
377
|
+
const modulePath = path.join(packagesDir, module);
|
|
378
|
+
const gitPath = path.join(modulePath, ".git");
|
|
379
|
+
|
|
380
|
+
// 检查是否是 git 仓库
|
|
381
|
+
if (!fs.existsSync(gitPath)) {
|
|
382
|
+
console.log(global.logColor.warning, `⚠️ ${module}: 不是 git 仓库,跳过检查`);
|
|
383
|
+
return null;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
try {
|
|
387
|
+
// 切换到模块目录并检查 git status
|
|
388
|
+
const { stdout } = await execAsync("git status --porcelain", { cwd: modulePath });
|
|
389
|
+
const statusOutput = stdout.trim();
|
|
390
|
+
|
|
391
|
+
if (statusOutput) {
|
|
392
|
+
console.log(global.logColor.error, `❌ ${module}: 存在未提交的更改`);
|
|
393
|
+
console.log(global.logColor.error, ` ${statusOutput.split("\n").join("\n ")}`);
|
|
394
|
+
return {
|
|
395
|
+
name: module,
|
|
396
|
+
path: modulePath,
|
|
397
|
+
changes: statusOutput.split("\n")
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
return null;
|
|
401
|
+
} catch (error) {
|
|
402
|
+
console.error(global.logColor.error, `❌ ${module}: 检查失败 - ${error.message}`);
|
|
403
|
+
return {
|
|
404
|
+
name: module,
|
|
405
|
+
path: modulePath,
|
|
406
|
+
error: error.message
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
})
|
|
410
|
+
).then(results => results.filter(r => r !== null));
|
|
411
|
+
|
|
412
|
+
return {
|
|
413
|
+
hasUncommitted: uncommittedModules.length > 0,
|
|
414
|
+
modules: uncommittedModules,
|
|
415
|
+
totalModules: modules.length,
|
|
416
|
+
checkedModules: modules
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* 切换所有 packages 模块到 master 分支并拉取最新代码
|
|
422
|
+
* @param {string} packagesDir - packages 目录的绝对路径
|
|
423
|
+
* @param {string} targetBranch - 目标分支名,默认为 'master'
|
|
424
|
+
*/
|
|
425
|
+
async function pullPackagesFromMaster(packagesDir, targetBranch = "master") {
|
|
426
|
+
const fs = require("fs");
|
|
427
|
+
|
|
428
|
+
if (!fs.existsSync(packagesDir)) {
|
|
429
|
+
console.error(global.logColor.error, `❌ packages 目录不存在: ${packagesDir}`);
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
const modules = fs.readdirSync(packagesDir).filter((item) => {
|
|
434
|
+
const modulePath = path.join(packagesDir, item);
|
|
435
|
+
return fs.statSync(modulePath).isDirectory();
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
// console.log(global.logColor.info, `🔄 切换 packages 下的模块到 ${targetBranch} 分支并拉取最新代码...`);
|
|
439
|
+
|
|
440
|
+
const results = await Promise.all(
|
|
441
|
+
modules.map(async (module) => {
|
|
442
|
+
const modulePath = path.join(packagesDir, module);
|
|
443
|
+
const gitPath = path.join(modulePath, ".git");
|
|
444
|
+
|
|
445
|
+
// 检查是否是 git 仓库
|
|
446
|
+
if (!fs.existsSync(gitPath)) {
|
|
447
|
+
console.log(global.logColor.warning, `⚠️ ${module}: 不是 git 仓库,跳过操作`);
|
|
448
|
+
return { module, success: false, reason: "不是 git 仓库" };
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
try {
|
|
452
|
+
// 获取当前分支
|
|
453
|
+
const { stdout: branchOut } = await execAsync("git rev-parse --abbrev-ref HEAD", { cwd: modulePath });
|
|
454
|
+
const currentBranch = branchOut.trim();
|
|
455
|
+
|
|
456
|
+
// 如果不在目标分支,则切换
|
|
457
|
+
if (currentBranch !== targetBranch) {
|
|
458
|
+
await execAsync(`git checkout ${targetBranch}`, { cwd: modulePath });
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// 拉取最新代码
|
|
462
|
+
await execAsync(`git pull origin ${targetBranch}`, { cwd: modulePath });
|
|
463
|
+
|
|
464
|
+
console.log(global.logColor.success, `✅ \x1b[34m${module}\x1b[32m[${targetBranch}]: 更新到最新代码`);
|
|
465
|
+
return { module, success: true };
|
|
466
|
+
} catch (error) {
|
|
467
|
+
console.error(global.logColor.error, `❌ ${module}: 操作失败 - ${error.message}`);
|
|
468
|
+
return { module, success: false, reason: error.message };
|
|
469
|
+
}
|
|
470
|
+
})
|
|
471
|
+
);
|
|
472
|
+
|
|
473
|
+
// 输出结果摘要
|
|
474
|
+
const successful = results.filter((r) => r.success).length;
|
|
475
|
+
const failed = results.filter((r) => !r.success).length;
|
|
476
|
+
|
|
477
|
+
// console.log(global.logColor.info, `\n📊 操作完成: 成功 ${successful} 个,失败 ${failed} 个`);
|
|
478
|
+
|
|
479
|
+
return results;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// 获取 packages 目录路径(相对于当前项目根目录)
|
|
483
|
+
function hasGitModules(packagesDir) {
|
|
484
|
+
if (!fs.existsSync(packagesDir)) {
|
|
485
|
+
return false;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
return ["infly-libs", "infly-ui"].some((item) => fs.existsSync(path.join(packagesDir, item, ".git")));
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
function checkAndUpdateRootPackages(packagesDir, targetBranch = "master") {
|
|
492
|
+
const rootDir = path.resolve(packagesDir, "..");
|
|
493
|
+
const gitPath = path.join(rootDir, ".git");
|
|
494
|
+
|
|
495
|
+
if (!fs.existsSync(gitPath)) {
|
|
496
|
+
console.log(global.logColor.warning, `⚠️ packages 不属于独立 git 仓库,也未找到根仓库,跳过 packages 更新`);
|
|
497
|
+
return [];
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const statusOutput = execSync("git status --porcelain", {
|
|
501
|
+
cwd: rootDir,
|
|
502
|
+
encoding: "utf8"
|
|
503
|
+
}).trim();
|
|
504
|
+
|
|
505
|
+
if (statusOutput) {
|
|
506
|
+
console.log(global.logColor.error, `\n❌ admin-monorepo 存在未提交的更改,无法合并 ${targetBranch}`);
|
|
507
|
+
console.log(global.logColor.error, ` ${statusOutput.split("\n").join("\n ")}`);
|
|
508
|
+
process.exit(1);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
const currentBranch = execSync("git rev-parse --abbrev-ref HEAD", {
|
|
512
|
+
cwd: rootDir,
|
|
513
|
+
encoding: "utf8"
|
|
514
|
+
}).trim();
|
|
515
|
+
|
|
516
|
+
console.log(global.logColor.info, `🔄 保持当前分支 ${currentBranch},合并 ${targetBranch} 的最新更改...`);
|
|
517
|
+
|
|
518
|
+
// 拉取远程最新的 targetBranch
|
|
519
519
|
execSync(`git fetch origin ${targetBranch}`, {
|
|
520
520
|
cwd: rootDir,
|
|
521
521
|
stdio: "inherit"
|
|
@@ -529,188 +529,188 @@ function checkAndUpdateRootPackages(packagesDir, targetBranch = "master") {
|
|
|
529
529
|
});
|
|
530
530
|
|
|
531
531
|
console.log(global.logColor.success, `✅ admin-monorepo[${currentBranch}]: 已合并 ${targetBranch} 的最新更改到 packages、docs 等目录`);
|
|
532
|
-
return [{ module: "admin-monorepo", success: true }];
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
function findPackagesDir(currentDir) {
|
|
536
|
-
let searchDir = currentDir || process.cwd();
|
|
537
|
-
|
|
538
|
-
// 向上查找 packages 目录,最多查找 3 层
|
|
539
|
-
for (let i = 0; i < 3; i++) {
|
|
540
|
-
const packagesPath = path.resolve(searchDir, "packages");
|
|
541
|
-
if (fs.existsSync(packagesPath)) {
|
|
542
|
-
return packagesPath;
|
|
543
|
-
}
|
|
544
|
-
const parentDir = path.dirname(searchDir);
|
|
545
|
-
if (parentDir === searchDir) break; // 已到根目录
|
|
546
|
-
searchDir = parentDir;
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
return null;
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
/**
|
|
553
|
-
* 检查指定目录的 Git 状态
|
|
554
|
-
* @param {string} dirPath - 目录的绝对路径
|
|
555
|
-
* @param {string} dirName - 目录名称(用于日志显示)
|
|
556
|
-
*/
|
|
557
|
-
function checkSingleDirGitStatus(dirPath, dirName) {
|
|
558
|
-
const fs = require("fs");
|
|
559
|
-
|
|
560
|
-
if (!fs.existsSync(dirPath)) {
|
|
561
|
-
console.log(global.logColor.warning, `⚠️ ${dirName} 目录不存在: ${dirPath}`);
|
|
562
|
-
return { hasUncommitted: false, changes: [] };
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
const gitPath = path.join(dirPath, ".git");
|
|
566
|
-
if (!fs.existsSync(gitPath)) {
|
|
567
|
-
console.log(global.logColor.warning, `⚠️ ${dirName} 不是 git 仓库,跳过检查`);
|
|
568
|
-
return { hasUncommitted: false, changes: [] };
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
try {
|
|
572
|
-
const statusOutput = execSync("git status --porcelain", {
|
|
573
|
-
cwd: dirPath,
|
|
574
|
-
encoding: "utf8"
|
|
575
|
-
}).trim();
|
|
576
|
-
|
|
577
|
-
if (statusOutput) {
|
|
578
|
-
console.log(global.logColor.error, `❌ ${dirName}: 存在未提交的更改`);
|
|
579
|
-
console.log(global.logColor.error, ` ${statusOutput.split("\n").join("\n ")}`);
|
|
580
|
-
return {
|
|
581
|
-
hasUncommitted: true,
|
|
582
|
-
changes: statusOutput.split("\n"),
|
|
583
|
-
path: dirPath
|
|
584
|
-
};
|
|
585
|
-
} else {
|
|
586
|
-
// console.log(global.logColor.success, `✅ ${dirName}: 工作区干净`);
|
|
587
|
-
return { hasUncommitted: false, changes: [] };
|
|
588
|
-
}
|
|
589
|
-
} catch (error) {
|
|
590
|
-
console.error(global.logColor.error, `❌ ${dirName}: 检查失败 - ${error.message}`);
|
|
591
|
-
return {
|
|
592
|
-
hasUncommitted: true,
|
|
593
|
-
error: error.message,
|
|
594
|
-
path: dirPath
|
|
595
|
-
};
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
/**
|
|
600
|
-
* 切换指定目录到目标分支并拉取最新代码
|
|
601
|
-
* @param {string} dirPath - 目录的绝对路径
|
|
602
|
-
* @param {string} dirName - 目录名称(用于日志显示)
|
|
603
|
-
* @param {string} targetBranch - 目标分支名
|
|
604
|
-
*/
|
|
605
|
-
function pullSingleDirFromBranch(dirPath, dirName, targetBranch = "master") {
|
|
606
|
-
const fs = require("fs");
|
|
607
|
-
|
|
608
|
-
if (!fs.existsSync(dirPath)) {
|
|
609
|
-
console.log(global.logColor.warning, `⚠️ ${dirName} 目录不存在: ${dirPath}`);
|
|
610
|
-
return { success: false, reason: "目录不存在" };
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
const gitPath = path.join(dirPath, ".git");
|
|
614
|
-
if (!fs.existsSync(gitPath)) {
|
|
615
|
-
console.log(global.logColor.warning, `⚠️ ${dirName} 不是 git 仓库,跳过操作`);
|
|
616
|
-
return { success: false, reason: "不是 git 仓库" };
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
try {
|
|
620
|
-
// 获取当前分支
|
|
621
|
-
const currentBranch = execSync("git rev-parse --abbrev-ref HEAD", {
|
|
622
|
-
cwd: dirPath,
|
|
623
|
-
encoding: "utf8"
|
|
624
|
-
}).trim();
|
|
625
|
-
|
|
626
|
-
// 如果不在目标分支,则切换
|
|
627
|
-
if (currentBranch !== targetBranch) {
|
|
628
|
-
// console.log(global.logColor.info, ` ${dirName}: 切换到 ${targetBranch} 分支...`);
|
|
629
|
-
execSync(`git checkout ${targetBranch}`, {
|
|
630
|
-
cwd: dirPath,
|
|
631
|
-
stdio: "pipe"
|
|
632
|
-
});
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
// 拉取最新代码
|
|
636
|
-
// console.log(global.logColor.info, ` ${dirName}: 拉取 ${targetBranch} 最新代码...`);
|
|
637
|
-
execSync(`git pull origin ${targetBranch}`, {
|
|
638
|
-
cwd: dirPath,
|
|
639
|
-
stdio: "pipe"
|
|
640
|
-
});
|
|
641
|
-
|
|
642
|
-
console.log(global.logColor.success, `✅ \x1b[34m${dirName}\x1b[32m[${targetBranch}]: 更新到最新代码`);
|
|
643
|
-
return { success: true };
|
|
644
|
-
} catch (error) {
|
|
645
|
-
console.error(global.logColor.error, `❌ ${dirName}: 操作失败 - ${error.message}`);
|
|
646
|
-
return { success: false, reason: error.message };
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
/**
|
|
651
|
-
* 完整的 packages 检查和更新流程
|
|
652
|
-
* @param {string} packagesDir - packages 目录的绝对路径
|
|
653
|
-
* @param {string} targetBranch - 目标分支名,默认为 'master'
|
|
654
|
-
*/
|
|
655
|
-
async function checkAndUpdatePackages(outPackagesDir, targetBranch = "master") {
|
|
656
|
-
try {
|
|
657
|
-
const packagesDir = outPackagesDir || findPackagesDir();
|
|
658
|
-
|
|
659
|
-
// 检查 packages 目录是否存在
|
|
660
|
-
if (!packagesDir) {
|
|
661
|
-
console.log(global.logColor.warning, `⚠️ packages 目录不存在,跳过检查`);
|
|
662
|
-
return;
|
|
663
|
-
}
|
|
664
|
-
|
|
665
|
-
console.log(global.logColor.info, `\n🚀 开始检查和更新 packages 模块...`);
|
|
666
|
-
|
|
667
|
-
// 1. 检查未提交的更改
|
|
668
|
-
if (!hasGitModules(packagesDir)) {
|
|
669
|
-
return checkAndUpdateRootPackages(packagesDir, targetBranch);
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
const checkResult = await checkPackagesGitStatus(packagesDir);
|
|
673
|
-
|
|
674
|
-
if (checkResult.hasUncommitted) {
|
|
675
|
-
// console.log(global.logColor.error, `\n❌ 发现 ${checkResult.modules.length} 个模块存在未提交的更改:`);
|
|
676
|
-
checkResult.modules.forEach((module) => {
|
|
677
|
-
// console.log(global.logColor.error, ` - ${module.name}`);
|
|
678
|
-
});
|
|
679
|
-
console.log(global.logColor.error, `\n请先提交或储藏这些更改后再继续执行!`);
|
|
680
|
-
process.exit(1);
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
// console.log(global.logColor.success, `\n✅ 所有模块工作区都是干净的,继续执行...`);
|
|
684
|
-
|
|
685
|
-
// 2. 切换到 master 并拉取最新代码
|
|
686
|
-
const updateResults = await pullPackagesFromMaster(packagesDir, targetBranch);
|
|
687
|
-
|
|
688
|
-
// 3. 检查是否有失败的操作
|
|
689
|
-
const failedUpdates = updateResults.filter((r) => !r.success);
|
|
690
|
-
if (failedUpdates.length > 0) {
|
|
691
|
-
console.log(global.logColor.warning, `\n⚠️ 有 ${failedUpdates.length} 个模块更新失败,但可以继续执行`);
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
// console.log(global.logColor.success, `\n🎉 packages 检查和更新流程完成!`);
|
|
695
|
-
return updateResults;
|
|
696
|
-
} catch (error) {
|
|
697
|
-
console.error(global.logColor.error, `❌ packages 检查失败: ${error.message}`);
|
|
698
|
-
console.error(global.logColor.error, "请检查 packages 下的模块是否有未提交的更改");
|
|
699
|
-
process.exit(1);
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
module.exports = {
|
|
704
|
-
getCurrentBranch,
|
|
705
|
-
getLatestCommitInfo,
|
|
706
|
-
getLastCommitByFile,
|
|
707
|
-
checkGitStatus,
|
|
708
|
-
autoGitProcess,
|
|
709
|
-
runGitCommand,
|
|
710
|
-
autoBranchProcess,
|
|
711
|
-
checkPackagesGitStatus,
|
|
712
|
-
pullPackagesFromMaster,
|
|
713
|
-
checkAndUpdatePackages,
|
|
714
|
-
checkSingleDirGitStatus,
|
|
715
|
-
pullSingleDirFromBranch
|
|
716
|
-
};
|
|
532
|
+
return [{ module: "admin-monorepo", success: true }];
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function findPackagesDir(currentDir) {
|
|
536
|
+
let searchDir = currentDir || process.cwd();
|
|
537
|
+
|
|
538
|
+
// 向上查找 packages 目录,最多查找 3 层
|
|
539
|
+
for (let i = 0; i < 3; i++) {
|
|
540
|
+
const packagesPath = path.resolve(searchDir, "packages");
|
|
541
|
+
if (fs.existsSync(packagesPath)) {
|
|
542
|
+
return packagesPath;
|
|
543
|
+
}
|
|
544
|
+
const parentDir = path.dirname(searchDir);
|
|
545
|
+
if (parentDir === searchDir) break; // 已到根目录
|
|
546
|
+
searchDir = parentDir;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
return null;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* 检查指定目录的 Git 状态
|
|
554
|
+
* @param {string} dirPath - 目录的绝对路径
|
|
555
|
+
* @param {string} dirName - 目录名称(用于日志显示)
|
|
556
|
+
*/
|
|
557
|
+
function checkSingleDirGitStatus(dirPath, dirName) {
|
|
558
|
+
const fs = require("fs");
|
|
559
|
+
|
|
560
|
+
if (!fs.existsSync(dirPath)) {
|
|
561
|
+
console.log(global.logColor.warning, `⚠️ ${dirName} 目录不存在: ${dirPath}`);
|
|
562
|
+
return { hasUncommitted: false, changes: [] };
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
const gitPath = path.join(dirPath, ".git");
|
|
566
|
+
if (!fs.existsSync(gitPath)) {
|
|
567
|
+
console.log(global.logColor.warning, `⚠️ ${dirName} 不是 git 仓库,跳过检查`);
|
|
568
|
+
return { hasUncommitted: false, changes: [] };
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
try {
|
|
572
|
+
const statusOutput = execSync("git status --porcelain", {
|
|
573
|
+
cwd: dirPath,
|
|
574
|
+
encoding: "utf8"
|
|
575
|
+
}).trim();
|
|
576
|
+
|
|
577
|
+
if (statusOutput) {
|
|
578
|
+
console.log(global.logColor.error, `❌ ${dirName}: 存在未提交的更改`);
|
|
579
|
+
console.log(global.logColor.error, ` ${statusOutput.split("\n").join("\n ")}`);
|
|
580
|
+
return {
|
|
581
|
+
hasUncommitted: true,
|
|
582
|
+
changes: statusOutput.split("\n"),
|
|
583
|
+
path: dirPath
|
|
584
|
+
};
|
|
585
|
+
} else {
|
|
586
|
+
// console.log(global.logColor.success, `✅ ${dirName}: 工作区干净`);
|
|
587
|
+
return { hasUncommitted: false, changes: [] };
|
|
588
|
+
}
|
|
589
|
+
} catch (error) {
|
|
590
|
+
console.error(global.logColor.error, `❌ ${dirName}: 检查失败 - ${error.message}`);
|
|
591
|
+
return {
|
|
592
|
+
hasUncommitted: true,
|
|
593
|
+
error: error.message,
|
|
594
|
+
path: dirPath
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* 切换指定目录到目标分支并拉取最新代码
|
|
601
|
+
* @param {string} dirPath - 目录的绝对路径
|
|
602
|
+
* @param {string} dirName - 目录名称(用于日志显示)
|
|
603
|
+
* @param {string} targetBranch - 目标分支名
|
|
604
|
+
*/
|
|
605
|
+
function pullSingleDirFromBranch(dirPath, dirName, targetBranch = "master") {
|
|
606
|
+
const fs = require("fs");
|
|
607
|
+
|
|
608
|
+
if (!fs.existsSync(dirPath)) {
|
|
609
|
+
console.log(global.logColor.warning, `⚠️ ${dirName} 目录不存在: ${dirPath}`);
|
|
610
|
+
return { success: false, reason: "目录不存在" };
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
const gitPath = path.join(dirPath, ".git");
|
|
614
|
+
if (!fs.existsSync(gitPath)) {
|
|
615
|
+
console.log(global.logColor.warning, `⚠️ ${dirName} 不是 git 仓库,跳过操作`);
|
|
616
|
+
return { success: false, reason: "不是 git 仓库" };
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
try {
|
|
620
|
+
// 获取当前分支
|
|
621
|
+
const currentBranch = execSync("git rev-parse --abbrev-ref HEAD", {
|
|
622
|
+
cwd: dirPath,
|
|
623
|
+
encoding: "utf8"
|
|
624
|
+
}).trim();
|
|
625
|
+
|
|
626
|
+
// 如果不在目标分支,则切换
|
|
627
|
+
if (currentBranch !== targetBranch) {
|
|
628
|
+
// console.log(global.logColor.info, ` ${dirName}: 切换到 ${targetBranch} 分支...`);
|
|
629
|
+
execSync(`git checkout ${targetBranch}`, {
|
|
630
|
+
cwd: dirPath,
|
|
631
|
+
stdio: "pipe"
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
// 拉取最新代码
|
|
636
|
+
// console.log(global.logColor.info, ` ${dirName}: 拉取 ${targetBranch} 最新代码...`);
|
|
637
|
+
execSync(`git pull origin ${targetBranch}`, {
|
|
638
|
+
cwd: dirPath,
|
|
639
|
+
stdio: "pipe"
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
console.log(global.logColor.success, `✅ \x1b[34m${dirName}\x1b[32m[${targetBranch}]: 更新到最新代码`);
|
|
643
|
+
return { success: true };
|
|
644
|
+
} catch (error) {
|
|
645
|
+
console.error(global.logColor.error, `❌ ${dirName}: 操作失败 - ${error.message}`);
|
|
646
|
+
return { success: false, reason: error.message };
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* 完整的 packages 检查和更新流程
|
|
652
|
+
* @param {string} packagesDir - packages 目录的绝对路径
|
|
653
|
+
* @param {string} targetBranch - 目标分支名,默认为 'master'
|
|
654
|
+
*/
|
|
655
|
+
async function checkAndUpdatePackages(outPackagesDir, targetBranch = "master") {
|
|
656
|
+
try {
|
|
657
|
+
const packagesDir = outPackagesDir || findPackagesDir();
|
|
658
|
+
|
|
659
|
+
// 检查 packages 目录是否存在
|
|
660
|
+
if (!packagesDir) {
|
|
661
|
+
console.log(global.logColor.warning, `⚠️ packages 目录不存在,跳过检查`);
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
console.log(global.logColor.info, `\n🚀 开始检查和更新 packages 模块...`);
|
|
666
|
+
|
|
667
|
+
// 1. 检查未提交的更改
|
|
668
|
+
if (!hasGitModules(packagesDir)) {
|
|
669
|
+
return checkAndUpdateRootPackages(packagesDir, targetBranch);
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
const checkResult = await checkPackagesGitStatus(packagesDir);
|
|
673
|
+
|
|
674
|
+
if (checkResult.hasUncommitted) {
|
|
675
|
+
// console.log(global.logColor.error, `\n❌ 发现 ${checkResult.modules.length} 个模块存在未提交的更改:`);
|
|
676
|
+
checkResult.modules.forEach((module) => {
|
|
677
|
+
// console.log(global.logColor.error, ` - ${module.name}`);
|
|
678
|
+
});
|
|
679
|
+
console.log(global.logColor.error, `\n请先提交或储藏这些更改后再继续执行!`);
|
|
680
|
+
process.exit(1);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
// console.log(global.logColor.success, `\n✅ 所有模块工作区都是干净的,继续执行...`);
|
|
684
|
+
|
|
685
|
+
// 2. 切换到 master 并拉取最新代码
|
|
686
|
+
const updateResults = await pullPackagesFromMaster(packagesDir, targetBranch);
|
|
687
|
+
|
|
688
|
+
// 3. 检查是否有失败的操作
|
|
689
|
+
const failedUpdates = updateResults.filter((r) => !r.success);
|
|
690
|
+
if (failedUpdates.length > 0) {
|
|
691
|
+
console.log(global.logColor.warning, `\n⚠️ 有 ${failedUpdates.length} 个模块更新失败,但可以继续执行`);
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// console.log(global.logColor.success, `\n🎉 packages 检查和更新流程完成!`);
|
|
695
|
+
return updateResults;
|
|
696
|
+
} catch (error) {
|
|
697
|
+
console.error(global.logColor.error, `❌ packages 检查失败: ${error.message}`);
|
|
698
|
+
console.error(global.logColor.error, "请检查 packages 下的模块是否有未提交的更改");
|
|
699
|
+
process.exit(1);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
module.exports = {
|
|
704
|
+
getCurrentBranch,
|
|
705
|
+
getLatestCommitInfo,
|
|
706
|
+
getLastCommitByFile,
|
|
707
|
+
checkGitStatus,
|
|
708
|
+
autoGitProcess,
|
|
709
|
+
runGitCommand,
|
|
710
|
+
autoBranchProcess,
|
|
711
|
+
checkPackagesGitStatus,
|
|
712
|
+
pullPackagesFromMaster,
|
|
713
|
+
checkAndUpdatePackages,
|
|
714
|
+
checkSingleDirGitStatus,
|
|
715
|
+
pullSingleDirFromBranch
|
|
716
|
+
};
|