@infly/libs 2.0.26 → 2.0.37

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.
Files changed (39) hide show
  1. package/bin/cli.js +29 -21
  2. package/build/build-dist/index.js +258 -61
  3. package/build/webpack5/remove-legacy-assets-plugin.js +84 -0
  4. package/build/webpack5/webpack.base.js +228 -20
  5. package/build/webpack5/webpack.base.test.js +59 -0
  6. package/index.js +10 -0
  7. package/module/Permission.js +121 -55
  8. package/module/REST.js +136 -26
  9. package/module/Router.js +15 -0
  10. package/module/Uts.js +380 -331
  11. package/module/cjs/deep-merge.cjs +38 -0
  12. package/module/cjs/page-config.cjs +119 -0
  13. package/module/cjs/request-url-rules.cjs +55 -0
  14. package/package.json +11 -10
  15. package/script/build/command.js +48 -0
  16. package/script/build/env.js +28 -0
  17. package/script/build/git.js +252 -0
  18. package/script/build/preview.js +75 -0
  19. package/script/build/webhook.js +118 -0
  20. package/script/git-automation/check-packages.js +11 -8
  21. package/script/git-automation/git-utils.js +67 -0
  22. package/script/git-automation/index.js +378 -106
  23. package/script/index.js +8 -8
  24. package/script/pts/cloud-scenes.mjs +65 -0
  25. package/script/pts/cloud.js +151 -0
  26. package/script/pts/generate-cloud-params.mjs +210 -0
  27. package/script/pts/generate-cloud-params.test.mjs +67 -0
  28. package/script/webhook/webhook.js +75 -4
  29. package/store/modules/user.js +111 -9
  30. package/tools/auto-export.js +56 -0
  31. package/tools/file-export.js +32 -9
  32. package/tools/file-process.js +3 -0
  33. package/tools/project-preview.js +110 -97
  34. package/dataInit/commonTypeMap.js +0 -31
  35. package/dataInit/marketingActivitiesMap.js +0 -214
  36. package/dataInit/orderMap.js +0 -13
  37. package/dataInit/personalMap.js +0 -19
  38. package/dataInit/settlementMap.js +0 -17
  39. package/types/unused.index.d.ts +0 -71
@@ -1,6 +1,10 @@
1
1
  const fs = require("fs");
2
2
  const path = require("path");
3
- const { execSync } = require("child_process");
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);
4
8
 
5
9
  /**
6
10
  * 获取当前 Git 分支名称
@@ -84,7 +88,10 @@ function getLastCommitByFile(filePath) {
84
88
  * @param {String} gitStatusLine 带文件状态的
85
89
  * @returns
86
90
  */
87
- function isBuildFile(targetProjectOutputDir = "", gitLine = "", sliceLen = -2) {
91
+ function isBuildFile(targetProjectOutputDir = "", gitLine = "", sliceLen = -1) {
92
+ if (!targetProjectOutputDir) {
93
+ return true;
94
+ }
88
95
  // 统一路径分隔符
89
96
  targetProjectOutputDir = targetProjectOutputDir.replace(/\\/g, "/");
90
97
 
@@ -114,30 +121,36 @@ function checkGitStatus(excludeDir, extraFilter, extraParams) {
114
121
  onlyCheckStatus,
115
122
  startLogText = "Git自动提交开始执行, 正在检查非构建文件"
116
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
+
117
132
  try {
118
133
  // 获取所有未跟踪和已修改的文件列表
119
134
  const files = execSync(`git${targetProjectOutputDirCmd} status --porcelain -z`, { encoding: "utf8" })
120
135
  .toString()
121
136
  .split("\u0000")
122
- .filter((line) => line.trim());
123
- // .map((line) => line.substring(3).trim()); // 提取文件路径
137
+ .filter((file) => file.trim() && isBuildFile(targetProjectOutputDir, file));
124
138
  const excludeFiles = []; // 非指定构建相关文件
125
139
  const includeFiles = []; // 指定的构建相关文件
126
140
 
127
- if (onlyCheckStatus) {
141
+ if (onlyCheckStatus || targetProjectOutputDirCmd) {
128
142
  return files || [];
129
143
  }
130
144
 
131
145
  console.log(global.logColor.success, `🚀 ${startLogText}`);
132
146
 
147
+ // 进行当前项目文件检查,如果有其它非配置文件则取消自动提交改为用户手动确认
133
148
  files.forEach((file) => {
134
149
  const existsDelFile = deleteOldFile.find((item) => file.includes(item));
135
150
  if (
136
151
  file.includes(`${excludeDir}/`) ||
137
152
  file.includes(versionFileName) ||
138
153
  file.includes(extraFilter) ||
139
- isBuildFile(targetProjectOutputDir, file) ||
140
- file.includes(`${gitAutoPushRepos}/`) || // 放宽文件判断条件,因为可能存在同时构建frontend/ea、frontend/e的情况
141
154
  existsDelFile
142
155
  ) {
143
156
  includeFiles.push(file);
@@ -149,6 +162,8 @@ function checkGitStatus(excludeDir, extraFilter, extraParams) {
149
162
  if (excludeFiles.length > 0) {
150
163
  throw new Error(`存在非构建文件未提交,请手动确定提交信息,本次自动提交终止\n${excludeFiles.join("\n")}`);
151
164
  }
165
+
166
+ return includeFiles || [];
152
167
  } catch (err) {
153
168
  console.error(global.logColor.error, `❌ 自动化构建Git状态检查存在问题:${err.message}`);
154
169
  process.exit(1);
@@ -170,6 +185,56 @@ function runGitCommand(command, options = {}) {
170
185
  }
171
186
  }
172
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
+
173
238
  /**
174
239
  * git自动推送
175
240
  * @param {String} buildFoldName 构建文件名称
@@ -187,7 +252,24 @@ function autoGitProcess(buildFoldName, newFileName, extraParams = {}) {
187
252
  gitAutoPushReposBranchMap = {},
188
253
  targetProjectOutputDirCmd = gitAutoPushRepos ? ` -C ${targetProjectOutputDir}` : "",
189
254
  projectName,
255
+ gitLockDir,
256
+ gitProcessOrder
190
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
+
191
273
  const { commitMsg, branch } = gitInfo || {};
192
274
  const gitRepos = gitAutoPushRepos ? `构建仓库: ${targetProjectOutputDir}` : `当前项目: ${projectName}`;
193
275
  let tempGitAutoCommitText = gitAutoCommitText ? gitAutoCommitText.replace("{version}", newVersion) : "";
@@ -201,32 +283,39 @@ function autoGitProcess(buildFoldName, newFileName, extraParams = {}) {
201
283
  const commitCmd = `git${targetProjectOutputDirCmd} commit -m "${tempGitAutoCommitText}"`;
202
284
  const pushCommand = `git${targetProjectOutputDirCmd} push origin ${tempBranch}`;
203
285
 
204
- // 无分支则不执行,自动推送到单独仓库必须得配置分支枚举对应
205
- /* "gitAutoPushReposBranchMap": {
206
- "release-zkhTest": "develop-zkhTest"
207
- }, */
208
286
  if (!tempBranch) {
209
287
  console.log(global.logColor.error, `❌ 缺少分支配置`);
210
288
  return;
211
289
  }
212
290
 
213
- // 先执行构建仓库的推送,防止git提交信息被覆盖
214
- runGitCommand(addCmd);
215
- runGitCommand(commitCmd);
216
- runGitCommand(pushCommand);
291
+ const checkCommitAndPush = () => {
292
+ const fileList = checkGitStatus(buildFoldName, newFileName, extraParams) || [];
217
293
 
218
- // 后执行当前项目的分支自动推送
219
- if (gitAutoPushRepos) {
220
- const overrideParams = { ...extraParams, gitAutoPushRepos: false };
221
- const fileList = checkGitStatus(buildFoldName, newFileName, { ...overrideParams, onlyCheckStatus: true });
222
- if (fileList.length > 0) {
223
- autoGitProcess(buildFoldName, newFileName, overrideParams); // 执行自动推送当前项目的git信息
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);
224
304
  }
305
+ };
306
+
307
+ if (gitAutoPushRepos) {
308
+ withGitRepoLock(gitLockDir, getGitCommonDir(targetProjectOutputDir), checkCommitAndPush);
309
+ } else {
310
+ checkCommitAndPush();
311
+ }
312
+
313
+ if (gitAutoPushRepos && !appFirst) {
314
+ runAppGitProcess();
225
315
  }
226
316
 
227
317
  console.log(global.logColor.success, `✅ Git \x1b[34m${gitRepos}\x1b[32m 自动提交和推送成功!`);
228
318
  }
229
-
230
319
  /**
231
320
  * 自动分支处理
232
321
  * @param {Object} config - 分支配置
@@ -240,6 +329,7 @@ async function autoBranchProcess(config) {
240
329
  const pullCmd = `git${targetProjectOutputDirCmd} pull origin ${tempBranch}`;
241
330
  const fileList = checkGitStatus(undefined, undefined, {
242
331
  onlyCheckStatus: true,
332
+ targetProjectOutputDir,
243
333
  targetProjectOutputDirCmd,
244
334
  startLogText: "Git自动化开始执行..."
245
335
  });
@@ -254,7 +344,7 @@ async function autoBranchProcess(config) {
254
344
  }
255
345
 
256
346
  if (fileList.length > 0) {
257
- console.error(global.logColor.error, `❌ 构建仓库存在未提交文件,请检查`);
347
+ console.error(global.logColor.error, `❌ 构建仓库 ${targetProjectOutputDir} 存在未提交文件,请检查`);
258
348
  process.exit(1);
259
349
  }
260
350
 
@@ -267,7 +357,7 @@ async function autoBranchProcess(config) {
267
357
  * @param {string} packagesDir - packages 目录的绝对路径
268
358
  * @returns {Object} 检查结果
269
359
  */
270
- function checkPackagesGitStatus(packagesDir) {
360
+ async function checkPackagesGitStatus(packagesDir) {
271
361
  const fs = require("fs");
272
362
 
273
363
  if (!fs.existsSync(packagesDir)) {
@@ -282,45 +372,42 @@ function checkPackagesGitStatus(packagesDir) {
282
372
 
283
373
  // console.log(global.logColor.info, `📦 检查 packages 下的 ${modules.length} 个模块...`);
284
374
 
285
- const uncommittedModules = [];
286
-
287
- for (const module of modules) {
288
- const modulePath = path.join(packagesDir, module);
289
- const gitPath = path.join(modulePath, ".git");
290
-
291
- // 检查是否是 git 仓库
292
- if (!fs.existsSync(gitPath)) {
293
- console.log(global.logColor.warning, `⚠️ ${module}: 不是 git 仓库,跳过检查`);
294
- continue;
295
- }
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");
296
379
 
297
- try {
298
- // 切换到模块目录并检查 git status
299
- const statusOutput = execSync("git status --porcelain", {
300
- cwd: modulePath,
301
- encoding: "utf8"
302
- }).trim();
380
+ // 检查是否是 git 仓库
381
+ if (!fs.existsSync(gitPath)) {
382
+ console.log(global.logColor.warning, `⚠️ ${module}: 不是 git 仓库,跳过检查`);
383
+ return null;
384
+ }
303
385
 
304
- if (statusOutput) {
305
- console.log(global.logColor.error, `❌ ${module}: 存在未提交的更改`);
306
- console.log(global.logColor.error, ` ${statusOutput.split("\n").join("\n ")}`);
307
- uncommittedModules.push({
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 {
308
404
  name: module,
309
405
  path: modulePath,
310
- changes: statusOutput.split("\n")
311
- });
312
- } else {
313
- // console.log(global.logColor.success, `✅ ${module}: 工作区干净`);
406
+ error: error.message
407
+ };
314
408
  }
315
- } catch (error) {
316
- console.error(global.logColor.error, `❌ ${module}: 检查失败 - ${error.message}`);
317
- uncommittedModules.push({
318
- name: module,
319
- path: modulePath,
320
- error: error.message
321
- });
322
- }
323
- }
409
+ })
410
+ ).then(results => results.filter(r => r !== null));
324
411
 
325
412
  return {
326
413
  hasUncommitted: uncommittedModules.length > 0,
@@ -335,7 +422,7 @@ function checkPackagesGitStatus(packagesDir) {
335
422
  * @param {string} packagesDir - packages 目录的绝对路径
336
423
  * @param {string} targetBranch - 目标分支名,默认为 'master'
337
424
  */
338
- function pullPackagesFromMaster(packagesDir, targetBranch = "master") {
425
+ async function pullPackagesFromMaster(packagesDir, targetBranch = "master") {
339
426
  const fs = require("fs");
340
427
 
341
428
  if (!fs.existsSync(packagesDir)) {
@@ -350,53 +437,38 @@ function pullPackagesFromMaster(packagesDir, targetBranch = "master") {
350
437
 
351
438
  // console.log(global.logColor.info, `🔄 切换 packages 下的模块到 ${targetBranch} 分支并拉取最新代码...`);
352
439
 
353
- const results = [];
354
-
355
- for (const module of modules) {
356
- const modulePath = path.join(packagesDir, module);
357
- const gitPath = path.join(modulePath, ".git");
358
-
359
- // 检查是否是 git 仓库
360
- if (!fs.existsSync(gitPath)) {
361
- console.log(global.logColor.warning, `⚠️ ${module}: 不是 git 仓库,跳过操作`);
362
- results.push({ module, success: false, reason: "不是 git 仓库" });
363
- continue;
364
- }
365
-
366
- try {
367
- // console.log(global.logColor.info, `🔄 处理模块: ${module}`);
368
-
369
- // 获取当前分支
370
- const currentBranch = execSync("git rev-parse --abbrev-ref HEAD", {
371
- cwd: modulePath,
372
- encoding: "utf8"
373
- }).trim();
374
-
375
- // console.log(global.logColor.info, ` 当前分支: ${currentBranch}`);
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");
376
444
 
377
- // 如果不在目标分支,则切换
378
- if (currentBranch !== targetBranch) {
379
- // console.log(global.logColor.info, ` 切换到 ${targetBranch} 分支...`);
380
- execSync(`git checkout ${targetBranch}`, {
381
- cwd: modulePath,
382
- stdio: "pipe"
383
- });
445
+ // 检查是否是 git 仓库
446
+ if (!fs.existsSync(gitPath)) {
447
+ console.log(global.logColor.warning, `⚠️ ${module}: 不是 git 仓库,跳过操作`);
448
+ return { module, success: false, reason: "不是 git 仓库" };
384
449
  }
385
450
 
386
- // 拉取最新代码
387
- // console.log(global.logColor.info, ` 拉取 ${targetBranch} 最新代码...`);
388
- execSync(`git pull origin ${targetBranch}`, {
389
- cwd: modulePath,
390
- stdio: "pipe"
391
- });
392
-
393
- console.log(global.logColor.success, `✅ ${module}: 成功更新到最新代码`);
394
- results.push({ module, success: true });
395
- } catch (error) {
396
- console.error(global.logColor.error, `❌ ${module}: 操作失败 - ${error.message}`);
397
- results.push({ module, success: false, reason: error.message });
398
- }
399
- }
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
+ );
400
472
 
401
473
  // 输出结果摘要
402
474
  const successful = results.filter((r) => r.success).length;
@@ -408,6 +480,66 @@ function pullPackagesFromMaster(packagesDir, targetBranch = "master") {
408
480
  }
409
481
 
410
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
+ execSync(`git fetch origin ${targetBranch}`, {
520
+ cwd: rootDir,
521
+ stdio: "inherit"
522
+ });
523
+
524
+ // 使用 merge-from-master.sh 脚本进行安全合并
525
+ const mergeScriptPath = path.join(rootDir, "script", "merge-from-master.sh");
526
+ if (fs.existsSync(mergeScriptPath)) {
527
+ execSync(`bash "${mergeScriptPath}" ${targetBranch}`, {
528
+ cwd: rootDir,
529
+ stdio: "inherit"
530
+ });
531
+ } else {
532
+ // 如果脚本不存在,使用基本的 merge 命令
533
+ execSync(`git merge origin/${targetBranch} --no-edit`, {
534
+ cwd: rootDir,
535
+ stdio: "inherit"
536
+ });
537
+ }
538
+
539
+ console.log(global.logColor.success, `✅ admin-monorepo[${currentBranch}]: 已合并 ${targetBranch} 的最新更改到 packages、docs 等目录`);
540
+ return [{ module: "admin-monorepo", success: true }];
541
+ }
542
+
411
543
  function findPackagesDir(currentDir) {
412
544
  let searchDir = currentDir || process.cwd();
413
545
 
@@ -425,6 +557,104 @@ function findPackagesDir(currentDir) {
425
557
  return null;
426
558
  }
427
559
 
560
+ /**
561
+ * 检查指定目录的 Git 状态
562
+ * @param {string} dirPath - 目录的绝对路径
563
+ * @param {string} dirName - 目录名称(用于日志显示)
564
+ */
565
+ function checkSingleDirGitStatus(dirPath, dirName) {
566
+ const fs = require("fs");
567
+
568
+ if (!fs.existsSync(dirPath)) {
569
+ console.log(global.logColor.warning, `⚠️ ${dirName} 目录不存在: ${dirPath}`);
570
+ return { hasUncommitted: false, changes: [] };
571
+ }
572
+
573
+ const gitPath = path.join(dirPath, ".git");
574
+ if (!fs.existsSync(gitPath)) {
575
+ console.log(global.logColor.warning, `⚠️ ${dirName} 不是 git 仓库,跳过检查`);
576
+ return { hasUncommitted: false, changes: [] };
577
+ }
578
+
579
+ try {
580
+ const statusOutput = execSync("git status --porcelain", {
581
+ cwd: dirPath,
582
+ encoding: "utf8"
583
+ }).trim();
584
+
585
+ if (statusOutput) {
586
+ console.log(global.logColor.error, `❌ ${dirName}: 存在未提交的更改`);
587
+ console.log(global.logColor.error, ` ${statusOutput.split("\n").join("\n ")}`);
588
+ return {
589
+ hasUncommitted: true,
590
+ changes: statusOutput.split("\n"),
591
+ path: dirPath
592
+ };
593
+ } else {
594
+ // console.log(global.logColor.success, `✅ ${dirName}: 工作区干净`);
595
+ return { hasUncommitted: false, changes: [] };
596
+ }
597
+ } catch (error) {
598
+ console.error(global.logColor.error, `❌ ${dirName}: 检查失败 - ${error.message}`);
599
+ return {
600
+ hasUncommitted: true,
601
+ error: error.message,
602
+ path: dirPath
603
+ };
604
+ }
605
+ }
606
+
607
+ /**
608
+ * 切换指定目录到目标分支并拉取最新代码
609
+ * @param {string} dirPath - 目录的绝对路径
610
+ * @param {string} dirName - 目录名称(用于日志显示)
611
+ * @param {string} targetBranch - 目标分支名
612
+ */
613
+ function pullSingleDirFromBranch(dirPath, dirName, targetBranch = "master") {
614
+ const fs = require("fs");
615
+
616
+ if (!fs.existsSync(dirPath)) {
617
+ console.log(global.logColor.warning, `⚠️ ${dirName} 目录不存在: ${dirPath}`);
618
+ return { success: false, reason: "目录不存在" };
619
+ }
620
+
621
+ const gitPath = path.join(dirPath, ".git");
622
+ if (!fs.existsSync(gitPath)) {
623
+ console.log(global.logColor.warning, `⚠️ ${dirName} 不是 git 仓库,跳过操作`);
624
+ return { success: false, reason: "不是 git 仓库" };
625
+ }
626
+
627
+ try {
628
+ // 获取当前分支
629
+ const currentBranch = execSync("git rev-parse --abbrev-ref HEAD", {
630
+ cwd: dirPath,
631
+ encoding: "utf8"
632
+ }).trim();
633
+
634
+ // 如果不在目标分支,则切换
635
+ if (currentBranch !== targetBranch) {
636
+ // console.log(global.logColor.info, ` ${dirName}: 切换到 ${targetBranch} 分支...`);
637
+ execSync(`git checkout ${targetBranch}`, {
638
+ cwd: dirPath,
639
+ stdio: "pipe"
640
+ });
641
+ }
642
+
643
+ // 拉取最新代码
644
+ // console.log(global.logColor.info, ` ${dirName}: 拉取 ${targetBranch} 最新代码...`);
645
+ execSync(`git pull origin ${targetBranch}`, {
646
+ cwd: dirPath,
647
+ stdio: "pipe"
648
+ });
649
+
650
+ console.log(global.logColor.success, `✅ \x1b[34m${dirName}\x1b[32m[${targetBranch}]: 更新到最新代码`);
651
+ return { success: true };
652
+ } catch (error) {
653
+ console.error(global.logColor.error, `❌ ${dirName}: 操作失败 - ${error.message}`);
654
+ return { success: false, reason: error.message };
655
+ }
656
+ }
657
+
428
658
  /**
429
659
  * 完整的 packages 检查和更新流程
430
660
  * @param {string} packagesDir - packages 目录的绝对路径
@@ -443,7 +673,11 @@ async function checkAndUpdatePackages(outPackagesDir, targetBranch = "master") {
443
673
  console.log(global.logColor.info, `\n🚀 开始检查和更新 packages 模块...`);
444
674
 
445
675
  // 1. 检查未提交的更改
446
- const checkResult = checkPackagesGitStatus(packagesDir);
676
+ if (!hasGitModules(packagesDir)) {
677
+ return checkAndUpdateRootPackages(packagesDir, targetBranch);
678
+ }
679
+
680
+ const checkResult = await checkPackagesGitStatus(packagesDir);
447
681
 
448
682
  if (checkResult.hasUncommitted) {
449
683
  // console.log(global.logColor.error, `\n❌ 发现 ${checkResult.modules.length} 个模块存在未提交的更改:`);
@@ -457,7 +691,7 @@ async function checkAndUpdatePackages(outPackagesDir, targetBranch = "master") {
457
691
  // console.log(global.logColor.success, `\n✅ 所有模块工作区都是干净的,继续执行...`);
458
692
 
459
693
  // 2. 切换到 master 并拉取最新代码
460
- const updateResults = pullPackagesFromMaster(packagesDir, targetBranch);
694
+ const updateResults = await pullPackagesFromMaster(packagesDir, targetBranch);
461
695
 
462
696
  // 3. 检查是否有失败的操作
463
697
  const failedUpdates = updateResults.filter((r) => !r.success);
@@ -465,6 +699,42 @@ async function checkAndUpdatePackages(outPackagesDir, targetBranch = "master") {
465
699
  console.log(global.logColor.warning, `\n⚠️ 有 ${failedUpdates.length} 个模块更新失败,但可以继续执行`);
466
700
  }
467
701
 
702
+ // 4. 检查和更新 postal-benefits-platform(如果当前是 postal-benefits-platform-level)
703
+ const currentProjectName = require(path.resolve(process.cwd(), "package.json")).name;
704
+ if (currentProjectName === "postal-benefits-platform-level") {
705
+ // console.log(global.logColor.info, `\n🚀 开始检查和更新 postal-benefits-platform...`);
706
+
707
+ // 获取 postal-benefits-platform 的路径
708
+ const appsDir = path.resolve(packagesDir, "..", "apps");
709
+ const postalPlatformDir = path.join(appsDir, "postal-benefits-platform");
710
+
711
+ // 获取 postal-benefits-platform-level 当前分支
712
+ const currentLevelBranch = execSync("git rev-parse --abbrev-ref HEAD", {
713
+ cwd: process.cwd(),
714
+ encoding: "utf8"
715
+ }).trim();
716
+
717
+ // 检查 postal-benefits-platform 是否有未提交的更改
718
+ const platformCheckResult = checkSingleDirGitStatus(postalPlatformDir, "postal-benefits-platform");
719
+
720
+ if (platformCheckResult.hasUncommitted) {
721
+ console.log(global.logColor.error, `\n❌ postal-benefits-platform 存在未提交的更改`);
722
+ console.log(global.logColor.error, `\n请先提交或储藏这些更改后再继续执行!`);
723
+ process.exit(1);
724
+ }
725
+
726
+ // 切换到与 postal-benefits-platform-level 相同的分支并拉取最新代码
727
+ const platformUpdateResult = pullSingleDirFromBranch(
728
+ postalPlatformDir,
729
+ "postal-benefits-platform",
730
+ currentLevelBranch
731
+ );
732
+
733
+ if (!platformUpdateResult.success) {
734
+ console.log(global.logColor.warning, `\n⚠️ postal-benefits-platform 更新失败,但可以继续执行`);
735
+ }
736
+ }
737
+
468
738
  // console.log(global.logColor.success, `\n🎉 packages 检查和更新流程完成!`);
469
739
  return updateResults;
470
740
  } catch (error) {
@@ -484,5 +754,7 @@ module.exports = {
484
754
  autoBranchProcess,
485
755
  checkPackagesGitStatus,
486
756
  pullPackagesFromMaster,
487
- checkAndUpdatePackages
757
+ checkAndUpdatePackages,
758
+ checkSingleDirGitStatus,
759
+ pullSingleDirFromBranch
488
760
  };
package/script/index.js CHANGED
@@ -2,25 +2,25 @@ module.exports = {
2
2
  previewList: [
3
3
  {
4
4
  key: "preview",
5
- value: "@infly/libs --preview"
5
+ value: "infly-libs --preview"
6
6
  },
7
7
  {
8
8
  key: "preview:no-build",
9
- value: "@infly/libs --preview --no-build"
9
+ value: "infly-libs --preview --no-build"
10
10
  }
11
11
  ],
12
12
  buildList: [
13
13
  {
14
- key: "infly:build:prod",
15
- value: "@infly/libs beforeBuild && vue-cli-service build && @infly/libs afterBuild"
14
+ key: "build:prod",
15
+ value: "infly-libs beforeBuild && vue-cli-service build && infly-libs afterBuild"
16
16
  },
17
17
  {
18
- key: "infly:build:stage",
19
- value: "@infly/libs beforeBuild && vue-cli-service build --mode staging && @infly/libs afterBuild"
18
+ key: "build:stage",
19
+ value: "infly-libs beforeBuild && vue-cli-service build --mode staging && infly-libs afterBuild"
20
20
  },
21
21
  {
22
- key: "infly:build:test",
23
- value: "@infly/libs beforeBuild && @infly/libs afterBuild"
22
+ key: "build:test",
23
+ value: "infly-libs beforeBuild && infly-libs afterBuild"
24
24
  }
25
25
  ]
26
26
  };