@infly/libs 2.2.9 → 2.2.11

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.
@@ -1,63 +1,63 @@
1
- const fs = require("fs");
2
- const path = require("path");
3
- const { execFileSync } = require("child_process");
4
-
5
- function gitOutput(args, cwd = process.cwd()) {
6
- return execFileSync("git", args, {
7
- cwd,
8
- encoding: "utf8",
9
- shell: process.platform === "win32"
10
- }).trim();
11
- }
12
-
13
- function findGitRepo(cwd) {
14
- let currentDir = path.resolve(cwd || process.cwd());
15
-
16
- while (true) {
17
- const gitPath = path.join(currentDir, ".git");
18
-
19
- if (fs.existsSync(gitPath)) {
20
- const stat = fs.statSync(gitPath);
21
- const repo = {
22
- root: currentDir,
23
- commonDir: gitPath
24
- };
25
-
26
- if (stat.isFile()) {
27
- const content = fs.readFileSync(gitPath, "utf8");
28
- const match = content.match(/gitdir:\s*(.+)/i);
29
-
30
- if (match) {
31
- repo.commonDir = path.resolve(currentDir, match[1].trim());
32
- }
33
- }
34
-
35
- return repo;
36
- }
37
-
38
- const parentDir = path.dirname(currentDir);
39
-
40
- if (parentDir === currentDir) {
41
- return null;
42
- }
43
-
44
- currentDir = parentDir;
45
- }
46
- }
47
-
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const { execFileSync } = require("child_process");
4
+
5
+ function gitOutput(args, cwd = process.cwd()) {
6
+ return execFileSync("git", args, {
7
+ cwd,
8
+ encoding: "utf8",
9
+ shell: process.platform === "win32"
10
+ }).trim();
11
+ }
12
+
13
+ function findGitRepo(cwd) {
14
+ let currentDir = path.resolve(cwd || process.cwd());
15
+
16
+ while (true) {
17
+ const gitPath = path.join(currentDir, ".git");
18
+
19
+ if (fs.existsSync(gitPath)) {
20
+ const stat = fs.statSync(gitPath);
21
+ const repo = {
22
+ root: currentDir,
23
+ commonDir: gitPath
24
+ };
25
+
26
+ if (stat.isFile()) {
27
+ const content = fs.readFileSync(gitPath, "utf8");
28
+ const match = content.match(/gitdir:\s*(.+)/i);
29
+
30
+ if (match) {
31
+ repo.commonDir = path.resolve(currentDir, match[1].trim());
32
+ }
33
+ }
34
+
35
+ return repo;
36
+ }
37
+
38
+ const parentDir = path.dirname(currentDir);
39
+
40
+ if (parentDir === currentDir) {
41
+ return null;
42
+ }
43
+
44
+ currentDir = parentDir;
45
+ }
46
+ }
47
+
48
48
  function getGitCommonDir(cwd) {
49
- const repo = findGitRepo(cwd);
50
-
51
- if (repo) {
52
- return repo.commonDir;
53
- }
54
-
55
- try {
56
- const commonDir = gitOutput(["-C", cwd, "rev-parse", "--git-common-dir"]);
57
- return path.resolve(cwd, commonDir);
58
- } catch {
59
- return path.resolve(cwd || process.cwd());
60
- }
49
+ const repo = findGitRepo(cwd);
50
+
51
+ if (repo) {
52
+ return repo.commonDir;
53
+ }
54
+
55
+ try {
56
+ const commonDir = gitOutput(["-C", cwd, "rev-parse", "--git-common-dir"]);
57
+ return path.resolve(cwd, commonDir);
58
+ } catch {
59
+ return path.resolve(cwd || process.cwd());
60
+ }
61
61
  }
62
62
 
63
63
  function resolveGitWorktreeRoot(cwd) {
@@ -65,9 +65,19 @@ function resolveGitWorktreeRoot(cwd) {
65
65
  return findGitRepo(resolvedCwd)?.root || resolvedCwd;
66
66
  }
67
67
 
68
+ function findOuterWorkspaceRoot(projectGitRoot) {
69
+ const outerRoot = findGitRepo(path.dirname(projectGitRoot))?.root;
70
+ if (!outerRoot || !fs.existsSync(path.join(outerRoot, ".gitmodules")) ||
71
+ !fs.existsSync(path.join(outerRoot, "pnpm-workspace.yaml"))) {
72
+ return "";
73
+ }
74
+ return outerRoot;
75
+ }
76
+
68
77
  module.exports = {
69
78
  gitOutput,
70
79
  findGitRepo,
71
80
  getGitCommonDir,
72
- resolveGitWorktreeRoot
81
+ resolveGitWorktreeRoot,
82
+ findOuterWorkspaceRoot
73
83
  };
@@ -549,7 +549,10 @@ function hasGitModules(packagesDir) {
549
549
  }
550
550
 
551
551
  function checkAndUpdateRootPackages(packagesDir, targetBranch = "master") {
552
- const rootDir = path.resolve(packagesDir, "..");
552
+ return checkAndUpdateRootRepository(path.resolve(packagesDir, ".."), targetBranch);
553
+ }
554
+
555
+ function checkAndUpdateRootRepository(rootDir, targetBranch = "master") {
553
556
  const gitPath = path.join(rootDir, ".git");
554
557
 
555
558
  if (!fs.existsSync(gitPath)) {
@@ -595,7 +598,7 @@ function checkAndUpdateRootPackages(packagesDir, targetBranch = "master") {
595
598
  function findPackagesDir(currentDir) {
596
599
  let searchDir = currentDir || process.cwd();
597
600
 
598
- // 向上查找 packages 目录,最多查找 3 层
601
+ // 旧版 packages/* 独立仓库模式保留近层目录查找。
599
602
  for (let i = 0; i < 3; i++) {
600
603
  const packagesPath = path.resolve(searchDir, "packages");
601
604
  if (fs.existsSync(packagesPath)) {
@@ -662,7 +665,7 @@ function checkSingleDirGitStatus(dirPath, dirName) {
662
665
  * @param {string} dirName - 目录名称(用于日志显示)
663
666
  * @param {string} targetBranch - 目标分支名
664
667
  */
665
- function pullSingleDirFromBranch(dirPath, dirName, targetBranch = "master") {
668
+ function pullSingleDirFromBranch(dirPath, dirName, targetBranch = "master", buildProjectName = "") {
666
669
  const fs = require("fs");
667
670
 
668
671
  if (!fs.existsSync(dirPath)) {
@@ -699,7 +702,8 @@ function pullSingleDirFromBranch(dirPath, dirName, targetBranch = "master") {
699
702
  stdio: "pipe"
700
703
  });
701
704
 
702
- console.log(global.logColor.success, `✅ \x1b[34m${dirName}\x1b[32m[${targetBranch}]: 更新到最新代码`);
705
+ const buildProjectText = buildProjectName ? `(构建项目:${buildProjectName})` : "";
706
+ console.log(global.logColor.success, `✅ \x1b[34m${dirName}\x1b[32m[${targetBranch}]: 更新到最新代码${buildProjectText}`);
703
707
  return { success: true };
704
708
  } catch (error) {
705
709
  console.error(global.logColor.error, `❌ ${dirName}: 操作失败 - ${error.message}`);
@@ -712,13 +716,20 @@ function pullSingleDirFromBranch(dirPath, dirName, targetBranch = "master") {
712
716
  * @param {string} packagesDir - packages 目录的绝对路径
713
717
  * @param {string} targetBranch - 目标分支名,默认为 'master'
714
718
  */
715
- async function checkAndUpdatePackages(outPackagesDir, targetBranch = "master") {
719
+ async function checkAndUpdatePackages(outPackagesDir, targetBranch = "master", buildProjectName = "", rootDir = "") {
716
720
  try {
721
+ if (rootDir) {
722
+ return checkAndUpdateRootRepository(rootDir, targetBranch);
723
+ }
724
+
717
725
  const packagesDir = outPackagesDir || findPackagesDir();
718
726
 
719
727
  // 检查 packages 目录是否存在
720
728
  if (!packagesDir) {
721
- console.log(global.logColor.warning, `⚠️ packages 目录不存在,跳过检查`);
729
+ const warning = buildProjectName
730
+ ? `⚠️ 内嵌项目 ${buildProjectName} 的构建目录未解析到 packages,跳过本次公共包源码更新`
731
+ : `⚠️ packages 目录不存在,跳过检查`;
732
+ console.log(global.logColor.warning, warning);
722
733
  return;
723
734
  }
724
735
 
@@ -3,20 +3,20 @@ const fs = require("fs");
3
3
  function normalizeRelativePath(value) {
4
4
  return String(value).replace(/\\/g, "/");
5
5
  }
6
-
7
- /**
8
- * 从 .gitmodules 中解析所有子模块路径
9
- */
10
- function parseGitmodulesPaths(rootDir) {
11
- const p = require("path").join(rootDir, ".gitmodules");
12
- if (!fs.existsSync(p)) return [];
13
- const content = fs.readFileSync(p, "utf8");
14
- const paths = [];
15
- content.split(/\[submodule/).forEach((block) => {
16
- const m = block.match(/path\s*=\s*(.+)/);
17
- if (m) paths.push(m[1].trim());
18
- });
19
- return paths;
20
- }
21
-
6
+
7
+ /**
8
+ * 从 .gitmodules 中解析所有子模块路径
9
+ */
10
+ function parseGitmodulesPaths(rootDir) {
11
+ const p = require("path").join(rootDir, ".gitmodules");
12
+ if (!fs.existsSync(p)) return [];
13
+ const content = fs.readFileSync(p, "utf8");
14
+ const paths = [];
15
+ content.split(/\[submodule/).forEach((block) => {
16
+ const m = block.match(/path\s*=\s*(.+)/);
17
+ if (m) paths.push(m[1].trim());
18
+ });
19
+ return paths;
20
+ }
21
+
22
22
  module.exports = { normalizeRelativePath, parseGitmodulesPaths };
@@ -1,146 +1,146 @@
1
1
  // workspace 配置读取与模式展开。
2
2
  const fs = require("fs");
3
- const path = require("path");
4
- const YAML = require("yaml");
5
-
6
- function readJson(filePath) {
7
- return JSON.parse(fs.readFileSync(filePath, "utf8"));
8
- }
9
-
10
- function readWorkspaceList(content, sectionName) {
11
- let workspaceConfig;
12
-
13
- try {
14
- workspaceConfig = YAML.parse(content);
15
- } catch (error) {
16
- throw new Error(`无法解析 pnpm-workspace.yaml: ${error.message}`, { cause: error });
17
- }
18
-
19
- if (workspaceConfig == null) {
20
- return [];
21
- }
22
-
23
- if (typeof workspaceConfig !== "object" || Array.isArray(workspaceConfig)) {
24
- throw new TypeError("pnpm-workspace.yaml 的根节点必须是对象");
25
- }
26
-
27
- const patterns = workspaceConfig[sectionName];
28
-
29
- if (patterns == null) {
30
- return [];
31
- }
32
-
33
- if (
34
- !Array.isArray(patterns)
35
- || patterns.some((pattern) => typeof pattern !== "string" || pattern.trim().length === 0)
36
- ) {
37
- throw new TypeError(`pnpm-workspace.yaml 的 ${sectionName} 必须是非空字符串数组`);
38
- }
39
-
40
- return patterns;
41
- }
42
-
43
- function readWorkspaceSection(workspaceFile, sectionName) {
44
- return readWorkspaceList(fs.readFileSync(workspaceFile, "utf8"), sectionName);
45
- }
46
-
47
- function readWorkspaceSectionWithFallback(workspaceFile, sectionName, fallbackSectionName) {
48
- const patterns = readWorkspaceSection(workspaceFile, sectionName);
49
-
50
- if (patterns.length > 0) {
51
- return patterns;
52
- }
53
-
54
- return readWorkspaceSection(workspaceFile, fallbackSectionName);
55
- }
56
-
57
- function expandWorkspacePattern(rootDir, pattern) {
58
- const normalizedPattern = pattern.replace(/\\/g, "/");
59
-
60
- if (!normalizedPattern.includes("*")) {
61
- return [path.join(rootDir, normalizedPattern)];
62
- }
63
-
64
- const starIndex = normalizedPattern.indexOf("*");
65
- const basePart = normalizedPattern.slice(0, starIndex);
66
- const suffixPart = normalizedPattern.slice(starIndex + 1);
67
- const baseDir = path.join(rootDir, basePart);
68
-
69
- if (!fs.existsSync(baseDir)) {
70
- return [];
71
- }
72
-
73
- const suffixIsNamePattern = suffixPart && !suffixPart.startsWith("/");
74
-
75
- return fs
76
- .readdirSync(baseDir, { withFileTypes: true })
77
- .filter((entry) => entry.isDirectory())
78
- .filter((entry) => !suffixIsNamePattern || entry.name.endsWith(suffixPart))
79
- .map((entry) => (suffixIsNamePattern
80
- ? path.join(baseDir, entry.name)
81
- : path.join(baseDir, entry.name, suffixPart)))
82
- .filter((packageDir) => fs.existsSync(path.join(packageDir, "package.json")));
83
- }
84
-
85
- function getWorkspacePackages(rootDir, workspaceFile, sectionName, fallbackSectionName, scriptName) {
86
- const patterns = fallbackSectionName
87
- ? readWorkspaceSectionWithFallback(workspaceFile, sectionName, fallbackSectionName)
88
- : readWorkspaceSection(workspaceFile, sectionName);
89
- const included = new Set();
90
- patterns.forEach((pattern) => {
91
- const excluded = pattern.startsWith("!");
92
- const resolvedPattern = excluded ? pattern.slice(1) : pattern;
93
- expandWorkspacePattern(rootDir, resolvedPattern).forEach((packageDir) => {
94
- const resolvedDir = path.resolve(packageDir);
95
- if (excluded) included.delete(resolvedDir);
96
- else included.add(resolvedDir);
97
- });
98
- });
99
- const packageDirs = [...included];
100
- const seen = new Set();
101
-
102
- return packageDirs
103
- .map((packageDir) => {
104
- const packageJsonPath = path.join(packageDir, "package.json");
105
-
106
- if (!fs.existsSync(packageJsonPath)) {
107
- return null;
108
- }
109
-
110
- const packageJson = readJson(packageJsonPath);
111
- const scripts = packageJson.scripts || {};
112
-
113
- if (!packageJson.name || seen.has(packageJson.name) || (scriptName && !scripts[scriptName])) {
114
- return null;
115
- }
116
-
117
- seen.add(packageJson.name);
118
-
119
- return {
120
- name: packageJson.name,
121
- dir: path.relative(rootDir, packageDir),
122
- packageJson,
123
- };
124
- })
125
- .filter(Boolean);
126
- }
127
-
128
- function toTurboFilter(value) {
129
- const normalized = value.replace(/\\/g, "/");
130
-
131
- if (normalized.includes("/") && !normalized.startsWith("./") && !normalized.startsWith("../")) {
132
- return `./${normalized}`;
133
- }
134
-
135
- return normalized;
136
- }
137
-
138
- module.exports = {
139
- expandWorkspacePattern,
140
- getWorkspacePackages,
141
- readJson,
142
- readWorkspaceList,
143
- readWorkspaceSection,
144
- readWorkspaceSectionWithFallback,
145
- toTurboFilter,
146
- };
3
+ const path = require("path");
4
+ const YAML = require("yaml");
5
+
6
+ function readJson(filePath) {
7
+ return JSON.parse(fs.readFileSync(filePath, "utf8"));
8
+ }
9
+
10
+ function readWorkspaceList(content, sectionName) {
11
+ let workspaceConfig;
12
+
13
+ try {
14
+ workspaceConfig = YAML.parse(content);
15
+ } catch (error) {
16
+ throw new Error(`无法解析 pnpm-workspace.yaml: ${error.message}`, { cause: error });
17
+ }
18
+
19
+ if (workspaceConfig == null) {
20
+ return [];
21
+ }
22
+
23
+ if (typeof workspaceConfig !== "object" || Array.isArray(workspaceConfig)) {
24
+ throw new TypeError("pnpm-workspace.yaml 的根节点必须是对象");
25
+ }
26
+
27
+ const patterns = workspaceConfig[sectionName];
28
+
29
+ if (patterns == null) {
30
+ return [];
31
+ }
32
+
33
+ if (
34
+ !Array.isArray(patterns)
35
+ || patterns.some((pattern) => typeof pattern !== "string" || pattern.trim().length === 0)
36
+ ) {
37
+ throw new TypeError(`pnpm-workspace.yaml 的 ${sectionName} 必须是非空字符串数组`);
38
+ }
39
+
40
+ return patterns;
41
+ }
42
+
43
+ function readWorkspaceSection(workspaceFile, sectionName) {
44
+ return readWorkspaceList(fs.readFileSync(workspaceFile, "utf8"), sectionName);
45
+ }
46
+
47
+ function readWorkspaceSectionWithFallback(workspaceFile, sectionName, fallbackSectionName) {
48
+ const patterns = readWorkspaceSection(workspaceFile, sectionName);
49
+
50
+ if (patterns.length > 0) {
51
+ return patterns;
52
+ }
53
+
54
+ return readWorkspaceSection(workspaceFile, fallbackSectionName);
55
+ }
56
+
57
+ function expandWorkspacePattern(rootDir, pattern) {
58
+ const normalizedPattern = pattern.replace(/\\/g, "/");
59
+
60
+ if (!normalizedPattern.includes("*")) {
61
+ return [path.join(rootDir, normalizedPattern)];
62
+ }
63
+
64
+ const starIndex = normalizedPattern.indexOf("*");
65
+ const basePart = normalizedPattern.slice(0, starIndex);
66
+ const suffixPart = normalizedPattern.slice(starIndex + 1);
67
+ const baseDir = path.join(rootDir, basePart);
68
+
69
+ if (!fs.existsSync(baseDir)) {
70
+ return [];
71
+ }
72
+
73
+ const suffixIsNamePattern = suffixPart && !suffixPart.startsWith("/");
74
+
75
+ return fs
76
+ .readdirSync(baseDir, { withFileTypes: true })
77
+ .filter((entry) => entry.isDirectory())
78
+ .filter((entry) => !suffixIsNamePattern || entry.name.endsWith(suffixPart))
79
+ .map((entry) => (suffixIsNamePattern
80
+ ? path.join(baseDir, entry.name)
81
+ : path.join(baseDir, entry.name, suffixPart)))
82
+ .filter((packageDir) => fs.existsSync(path.join(packageDir, "package.json")));
83
+ }
84
+
85
+ function getWorkspacePackages(rootDir, workspaceFile, sectionName, fallbackSectionName, scriptName) {
86
+ const patterns = fallbackSectionName
87
+ ? readWorkspaceSectionWithFallback(workspaceFile, sectionName, fallbackSectionName)
88
+ : readWorkspaceSection(workspaceFile, sectionName);
89
+ const included = new Set();
90
+ patterns.forEach((pattern) => {
91
+ const excluded = pattern.startsWith("!");
92
+ const resolvedPattern = excluded ? pattern.slice(1) : pattern;
93
+ expandWorkspacePattern(rootDir, resolvedPattern).forEach((packageDir) => {
94
+ const resolvedDir = path.resolve(packageDir);
95
+ if (excluded) included.delete(resolvedDir);
96
+ else included.add(resolvedDir);
97
+ });
98
+ });
99
+ const packageDirs = [...included];
100
+ const seen = new Set();
101
+
102
+ return packageDirs
103
+ .map((packageDir) => {
104
+ const packageJsonPath = path.join(packageDir, "package.json");
105
+
106
+ if (!fs.existsSync(packageJsonPath)) {
107
+ return null;
108
+ }
109
+
110
+ const packageJson = readJson(packageJsonPath);
111
+ const scripts = packageJson.scripts || {};
112
+
113
+ if (!packageJson.name || seen.has(packageJson.name) || (scriptName && !scripts[scriptName])) {
114
+ return null;
115
+ }
116
+
117
+ seen.add(packageJson.name);
118
+
119
+ return {
120
+ name: packageJson.name,
121
+ dir: path.relative(rootDir, packageDir),
122
+ packageJson,
123
+ };
124
+ })
125
+ .filter(Boolean);
126
+ }
127
+
128
+ function toTurboFilter(value) {
129
+ const normalized = value.replace(/\\/g, "/");
130
+
131
+ if (normalized.includes("/") && !normalized.startsWith("./") && !normalized.startsWith("../")) {
132
+ return `./${normalized}`;
133
+ }
134
+
135
+ return normalized;
136
+ }
137
+
138
+ module.exports = {
139
+ expandWorkspacePattern,
140
+ getWorkspacePackages,
141
+ readJson,
142
+ readWorkspaceList,
143
+ readWorkspaceSection,
144
+ readWorkspaceSectionWithFallback,
145
+ toTurboFilter,
146
+ };