@infly/libs 2.1.1 → 2.1.3

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.
@@ -210,10 +210,12 @@ async function runTargetRelease(options = {}, dependencies = {}) {
210
210
  }
211
211
  if (!io.existsSync(dockerfile)) throw new Error(`缺少 Dockerfile: ${dockerfile}`);
212
212
  const short = commit.slice(0, 12);
213
- const image = `${item.repository}:qc-${short}`;
213
+ const image = `${item.repository}:${short}`;
214
+ const latestImage = `${item.repository}:latest`;
215
+ // --load 只把当前构建节点平台的镜像载入本地镜像存储;发布流程直接使用单平台镜像。
214
216
  run("docker", ["buildx", "build", "--load", "--file", dockerfile,
215
217
  "--label", `org.opencontainers.image.revision=${commit}`, "--label", `org.opencontainers.image.version=${short}`,
216
- "--label", `com.infly.platform=${target}`, "--tag", image, context], { cwd: projectDir });
218
+ "--label", `com.infly.platform=${target}`, "--tag", image, "--tag", latestImage, context], { cwd: projectDir });
217
219
  if (capture("docker", ["image", "inspect", "--format", '{{ index .Config.Labels "org.opencontainers.image.revision" }}', image]) !== commit) {
218
220
  throw new Error(`${item.name} 镜像 revision 校验失败`);
219
221
  }
@@ -221,10 +223,14 @@ async function runTargetRelease(options = {}, dependencies = {}) {
221
223
  const hosts = (item.check.addHosts || []).flatMap((host) => ["--add-host", host]);
222
224
  run("docker", ["run", "--rm", ...hosts, "--entrypoint", item.check.entrypoint, image, ...item.check.args], { cwd: projectDir });
223
225
  }
224
- if (options.push) run("docker", ["push", image], { cwd: projectDir });
226
+ if (options.push) {
227
+ run("docker", ["push", image], { cwd: projectDir });
228
+ run("docker", ["push", latestImage], { cwd: projectDir });
229
+ }
225
230
  log(`${item.name}_commit=${commit}`);
226
231
  log(`${item.name}=${image}`);
227
- results.push({ component: item.name, commit, image, pushed: Boolean(options.push) });
232
+ log(`${item.name}_latest=${latestImage}`);
233
+ results.push({ component: item.name, commit, image, latestImage, pushed: Boolean(options.push) });
228
234
  } finally {
229
235
  // 只移除本次创建的隔离 worktree;清理失败保留目录供人工恢复,绝不清空开发工作区。
230
236
  if (worktree) run("git", ["-C", item.source.directory, "worktree", "remove", "--force", worktree], { cwd: projectDir });
@@ -234,23 +240,19 @@ async function runTargetRelease(options = {}, dependencies = {}) {
234
240
 
235
241
  // 构建结果汇总(排版与 build:docker 保持一致:✅ 完成 + 可用标签 + 可选发布系统链接)。
236
242
  // 构建时已逐组件打印 frontend_commit= 等机器行,供失败前人工留档;这里汇总为可直接复制到
237
- // Jenkins 发布 Job 的参数区块。
243
+ // 输出同时面向本地核对与 Jenkins 下拉部署,避免再手工复制提交参数。
238
244
  if (results.length) {
239
245
  const pushNote = options.push
240
- ? "本次新建 qc 隔离镜像;正式标签由 Jenkins 校验后晋升"
241
- : "本次新建 qc 隔离镜像(仅本地,未推送)";
246
+ ? "已推送不可变提交标签和 latest 最新构建标签,可直接在 Jenkins 选择部署"
247
+ : "仅本地生成,未推送镜像";
242
248
  log("");
243
249
  log("\x1b[0;34m==========================================");
244
250
  log("✅ 完成");
245
251
  log("==========================================\x1b[0m");
246
252
  log(`可用标签(${pushNote}):`);
247
253
  for (const r of results) {
248
- log(` ${r.component}=${r.image} (commit 精确版本)`);
249
- }
250
- log("");
251
- log("发布参数(复制到 Jenkins 发布 Job 对应输入框):");
252
- for (const r of results) {
253
- log(` ${r.component}_commit=${r.commit}`);
254
+ log(` ${r.component}=${r.image} (不可变提交版本)`);
255
+ log(` ${r.component}_latest=${r.latestImage} (最新构建镜像)`);
254
256
  }
255
257
  if (cicdLink) {
256
258
  log("");
@@ -3,53 +3,53 @@ const path = require("path");
3
3
 
4
4
  // 兼容旧 tools/file-process.js 的 __dirname 路径语义。
5
5
  const legacyToolsDir = path.resolve(__dirname, "../tools");
6
-
7
- /**
8
- * 覆盖写入构建脚本
9
- * @param {String} filePath - 覆盖文件路径
10
- * @param {Array | Object} scriptsList - 写入脚本配置
11
- * @param {String} 更新字段值
12
- */
13
- function scriptWrite(filePath, scriptsList, updateKey = "scripts") {
14
- const pkg = JSON.parse(fs.readFileSync(filePath));
15
- pkg[updateKey] = pkg[updateKey] || {};
16
- if (Array.isArray(scriptsList)) {
17
- scriptsList.forEach(item => {
18
- if (pkg[updateKey][item.key]) {
19
- pkg[updateKey][`origin:${item.key}`] = pkg[updateKey][item.key];
20
- }
21
- pkg[updateKey][item.key] = item.value;
22
- });
23
- } else if (typeof scriptsList === "object") {
24
- pkg[updateKey] = scriptsList;
25
- }
26
-
27
- fs.writeFileSync(filePath, JSON.stringify(pkg, null, 2));
28
- }
29
-
30
- /**
31
- * 获取文件绝对路径
32
- * @param {String} filePath - 文件路径
33
- * @returns
34
- */
35
- function targetProjectFileResolve(filePath) {
36
- // 初始脚本自动执行路径会自动读取node_modules
37
- const projectRoot = process.cwd();
38
- const absoluteProjectRoot = projectRoot.replace(/\\node_modules.*$/g, "");
39
- return path.resolve(absoluteProjectRoot, filePath);
40
- }
41
-
42
- /**
43
- * 获取文件绝对路径
44
- * @param {String} filePath - 文件路径
45
- * @returns
46
- */
6
+
7
+ /**
8
+ * 覆盖写入构建脚本
9
+ * @param {String} filePath - 覆盖文件路径
10
+ * @param {Array | Object} scriptsList - 写入脚本配置
11
+ * @param {String} 更新字段值
12
+ */
13
+ function scriptWrite(filePath, scriptsList, updateKey = "scripts") {
14
+ const pkg = JSON.parse(fs.readFileSync(filePath));
15
+ pkg[updateKey] = pkg[updateKey] || {};
16
+ if (Array.isArray(scriptsList)) {
17
+ scriptsList.forEach(item => {
18
+ if (pkg[updateKey][item.key]) {
19
+ pkg[updateKey][`origin:${item.key}`] = pkg[updateKey][item.key];
20
+ }
21
+ pkg[updateKey][item.key] = item.value;
22
+ });
23
+ } else if (typeof scriptsList === "object") {
24
+ pkg[updateKey] = scriptsList;
25
+ }
26
+
27
+ fs.writeFileSync(filePath, JSON.stringify(pkg, null, 2));
28
+ }
29
+
30
+ /**
31
+ * 获取文件绝对路径
32
+ * @param {String} filePath - 文件路径
33
+ * @returns
34
+ */
35
+ function targetProjectFileResolve(filePath) {
36
+ // 初始脚本自动执行路径会自动读取node_modules
37
+ const projectRoot = process.cwd();
38
+ const absoluteProjectRoot = projectRoot.replace(/\\node_modules.*$/g, "");
39
+ return path.resolve(absoluteProjectRoot, filePath);
40
+ }
41
+
42
+ /**
43
+ * 获取文件绝对路径
44
+ * @param {String} filePath - 文件路径
45
+ * @returns
46
+ */
47
47
  function currentProjectFileResolve(filePath) {
48
48
  return path.join(legacyToolsDir, filePath);
49
49
  }
50
-
51
- module.exports = {
52
- scriptWrite,
53
- targetProjectFileResolve,
54
- currentProjectFileResolve
55
- };
50
+
51
+ module.exports = {
52
+ scriptWrite,
53
+ targetProjectFileResolve,
54
+ currentProjectFileResolve
55
+ };
@@ -1,57 +1,57 @@
1
1
  // CLI 交互选择适配,业务选项由调用方注入。
2
2
  const EXIT_SELECTION = "__exit__";
3
-
4
- function isReadlineClosedError(error) {
5
- return error?.code === "ERR_USE_AFTER_CLOSE";
6
- }
7
-
8
- function makePromptCancellationSafe(prompt) {
9
- const unsafeStop = prompt.stop;
10
- if (typeof unsafeStop !== "function") return;
11
-
12
- prompt.removeListener("close", unsafeStop);
13
- const safeStop = () => {
14
- try {
15
- unsafeStop();
16
- } catch (error) {
17
- if (!isReadlineClosedError(error)) throw error;
18
- }
19
- };
20
- prompt.stop = safeStop;
21
- prompt.once("close", safeStop);
22
- }
23
-
3
+
4
+ function isReadlineClosedError(error) {
5
+ return error?.code === "ERR_USE_AFTER_CLOSE";
6
+ }
7
+
8
+ function makePromptCancellationSafe(prompt) {
9
+ const unsafeStop = prompt.stop;
10
+ if (typeof unsafeStop !== "function") return;
11
+
12
+ prompt.removeListener("close", unsafeStop);
13
+ const safeStop = () => {
14
+ try {
15
+ unsafeStop();
16
+ } catch (error) {
17
+ if (!isReadlineClosedError(error)) throw error;
18
+ }
19
+ };
20
+ prompt.stop = safeStop;
21
+ prompt.once("close", safeStop);
22
+ }
23
+
24
24
  async function selectOption(message, choices, dependencies = {}) {
25
- const stdin = dependencies.stdin || process.stdin;
26
- const stdout = dependencies.stdout || process.stdout;
27
- if (!stdin.isTTY || !stdout.isTTY) {
28
- throw new Error(`${message}: interactive selection requires a TTY; pass an explicit option.`);
29
- }
30
-
31
- const Select = dependencies.Select || require("enquirer").Select;
32
- const prompt = new Select({
33
- name: "selection",
34
- message,
35
- stdin,
36
- stdout,
37
- choices: [
38
- ...choices.map((choice) => ({
39
- name: choice.value,
40
- message: choice.label,
41
- })),
42
- { name: EXIT_SELECTION, message: "退出" },
43
- ],
44
- });
45
-
46
- prompt.once("start", makePromptCancellationSafe);
47
- try {
48
- return await prompt.run();
49
- } catch (error) {
50
- if (!error || error.name === "CancelPromptError" || isReadlineClosedError(error)) {
51
- return EXIT_SELECTION;
52
- }
53
- throw error;
54
- }
25
+ const stdin = dependencies.stdin || process.stdin;
26
+ const stdout = dependencies.stdout || process.stdout;
27
+ if (!stdin.isTTY || !stdout.isTTY) {
28
+ throw new Error(`${message}: interactive selection requires a TTY; pass an explicit option.`);
29
+ }
30
+
31
+ const Select = dependencies.Select || require("enquirer").Select;
32
+ const prompt = new Select({
33
+ name: "selection",
34
+ message,
35
+ stdin,
36
+ stdout,
37
+ choices: [
38
+ ...choices.map((choice) => ({
39
+ name: choice.value,
40
+ message: choice.label,
41
+ })),
42
+ { name: EXIT_SELECTION, message: "退出" },
43
+ ],
44
+ });
45
+
46
+ prompt.once("start", makePromptCancellationSafe);
47
+ try {
48
+ return await prompt.run();
49
+ } catch (error) {
50
+ if (!error || error.name === "CancelPromptError" || isReadlineClosedError(error)) {
51
+ return EXIT_SELECTION;
52
+ }
53
+ throw error;
54
+ }
55
55
  }
56
56
 
57
57
  async function selectOptions(message, choices, dependencies = {}) {
@@ -1,31 +1,31 @@
1
1
  /**
2
2
  * 框架无关的异步并发控制。
3
- * 异步并发限流器。
4
- * 对 items 数组中的每一项执行 worker,同时最多运行 concurrency 个 worker。
5
- *
6
- * @param {Array} items - 待处理项
7
- * @param {number} concurrency - 最大并发数
8
- * @param {Function} worker - 处理函数 (item, index) => Promise
9
- * @returns {Promise<Array>} 结果数组,索引与 items 一一对应
10
- */
11
- async function runWithConcurrency(items, concurrency, worker) {
12
- if (!Number.isInteger(concurrency) || concurrency < 1) {
13
- throw new TypeError("concurrency must be a positive integer");
14
- }
15
-
16
- const results = new Array(items.length);
17
- let nextIndex = 0;
18
- const runWorker = async () => {
19
- while (nextIndex < items.length) {
20
- const index = nextIndex;
21
- nextIndex += 1;
22
- results[index] = await worker(items[index], index);
23
- }
24
- };
25
-
26
- const workerCount = Math.min(concurrency, items.length);
27
- await Promise.all(Array.from({ length: workerCount }, runWorker));
28
- return results;
29
- }
30
-
31
- module.exports = { runWithConcurrency };
3
+ * 异步并发限流器。
4
+ * 对 items 数组中的每一项执行 worker,同时最多运行 concurrency 个 worker。
5
+ *
6
+ * @param {Array} items - 待处理项
7
+ * @param {number} concurrency - 最大并发数
8
+ * @param {Function} worker - 处理函数 (item, index) => Promise
9
+ * @returns {Promise<Array>} 结果数组,索引与 items 一一对应
10
+ */
11
+ async function runWithConcurrency(items, concurrency, worker) {
12
+ if (!Number.isInteger(concurrency) || concurrency < 1) {
13
+ throw new TypeError("concurrency must be a positive integer");
14
+ }
15
+
16
+ const results = new Array(items.length);
17
+ let nextIndex = 0;
18
+ const runWorker = async () => {
19
+ while (nextIndex < items.length) {
20
+ const index = nextIndex;
21
+ nextIndex += 1;
22
+ results[index] = await worker(items[index], index);
23
+ }
24
+ };
25
+
26
+ const workerCount = Math.min(concurrency, items.length);
27
+ await Promise.all(Array.from({ length: workerCount }, runWorker));
28
+ return results;
29
+ }
30
+
31
+ module.exports = { runWithConcurrency };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infly/libs",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "description": "不受前端框架限制的独立工具库",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -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
+ };