@infly/libs 2.2.11 → 2.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/adapters/webpack/auto-export.js +52 -52
- package/build/build-utils/command.js +57 -57
- package/build/build-utils/deps.js +28 -28
- package/build/build-utils/git.js +254 -254
- package/build/build-utils/preview.js +78 -78
- package/build/build-utils/webhook-single.js +136 -136
- package/build/docker/compose-release.js +79 -79
- package/build/project-files.js +47 -47
- package/core/async/concurrency.js +29 -29
- package/package.json +1 -1
- package/script/git-automation/git-utils.js +59 -59
- package/script/git-automation/index.js +72 -2
- package/script/git-automation/submodule-utils.js +16 -16
- package/workspace/config.js +144 -144
|
@@ -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) {
|
|
@@ -67,6 +67,72 @@ function createCommitTextMatcher(template = "") {
|
|
|
67
67
|
return new RegExp(`^${escapedParts.join(".+?")}$`);
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
function isAncestorCommit(ancestor, descendant, cwd) {
|
|
71
|
+
try {
|
|
72
|
+
execSync(`git merge-base --is-ancestor ${ancestor} ${descendant}`, { cwd, stdio: "ignore" });
|
|
73
|
+
return true;
|
|
74
|
+
} catch {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 将侧分支上的版本提交映射到它首次进入当前分支第一父链的合并提交。
|
|
81
|
+
* 否则 `versionCommit..HEAD` 会把当前分支上与侧分支互不为祖先的大量旧提交误算为本次更新。
|
|
82
|
+
*/
|
|
83
|
+
function resolveBuildSummaryBaseline(versionCommit, cwd) {
|
|
84
|
+
if (!/^[0-9a-f]{40}$/i.test(versionCommit)) return versionCommit;
|
|
85
|
+
|
|
86
|
+
const firstParentHistory = execSync("git rev-list --first-parent HEAD", { cwd, encoding: "utf-8" })
|
|
87
|
+
.trim()
|
|
88
|
+
.split(/\r?\n/)
|
|
89
|
+
.filter(Boolean);
|
|
90
|
+
|
|
91
|
+
if (firstParentHistory.includes(versionCommit)) return versionCommit;
|
|
92
|
+
|
|
93
|
+
// 第一父链从新到旧排列;“包含版本提交”在链上形成连续前缀,可以二分找到最早引入点。
|
|
94
|
+
let low = 0;
|
|
95
|
+
let high = firstParentHistory.length - 1;
|
|
96
|
+
let oldestContainingIndex = -1;
|
|
97
|
+
while (low <= high) {
|
|
98
|
+
const middle = Math.floor((low + high) / 2);
|
|
99
|
+
if (isAncestorCommit(versionCommit, firstParentHistory[middle], cwd)) {
|
|
100
|
+
oldestContainingIndex = middle;
|
|
101
|
+
low = middle + 1;
|
|
102
|
+
} else {
|
|
103
|
+
high = middle - 1;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return oldestContainingIndex >= 0 ? firstParentHistory[oldestContainingIndex] : versionCommit;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function getCommitSubjectKey(subject) {
|
|
111
|
+
const workItem = subject.match(/#[a-z][a-z0-9]*-\d+\b/i)?.[0];
|
|
112
|
+
if (workItem) return `work-item:${workItem.toUpperCase()}`;
|
|
113
|
+
|
|
114
|
+
return subject
|
|
115
|
+
.trim()
|
|
116
|
+
.replace(/\s+/g, " ")
|
|
117
|
+
.replace(/\s+([,.;:!?#])/g, "$1")
|
|
118
|
+
.toLowerCase();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function deduplicateCommitSubjects(subjects) {
|
|
122
|
+
const seen = new Set();
|
|
123
|
+
return subjects.filter((subject) => {
|
|
124
|
+
const key = getCommitSubjectKey(subject);
|
|
125
|
+
if (seen.has(key)) return false;
|
|
126
|
+
seen.add(key);
|
|
127
|
+
return true;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function getHeadParents(cwd) {
|
|
132
|
+
const headWithParents = execSync("git rev-list --parents -n 1 HEAD", { cwd, encoding: "utf-8" }).trim();
|
|
133
|
+
return headWithParents ? headWithParents.split(/\s+/).slice(1) : [];
|
|
134
|
+
}
|
|
135
|
+
|
|
70
136
|
/**
|
|
71
137
|
* 获取上一次自动版本提交之后的业务提交摘要。
|
|
72
138
|
* Merge 提交只描述 Git 操作,不作为企业微信中的更新内容。
|
|
@@ -95,13 +161,17 @@ function getBuildUpdateText(gitAutoCommitText, cwd = process.cwd(), limit = 10)
|
|
|
95
161
|
|
|
96
162
|
if (!previousBuild) return "";
|
|
97
163
|
|
|
98
|
-
const
|
|
164
|
+
const baseline = resolveBuildSummaryBaseline(previousBuild.hash, cwd);
|
|
165
|
+
// 构建正好发生在 Merge HEAD 时,更新内容应描述本次合入的分支,而不是第一父链上更早的待发布提交。
|
|
166
|
+
const headParents = getHeadParents(cwd);
|
|
167
|
+
const range = headParents.length > 1 ? `${headParents[0]}..HEAD` : `${baseline}..HEAD`;
|
|
168
|
+
const subjects = deduplicateCommitSubjects(execSync(`git log --no-merges --format=%s ${range}`, {
|
|
99
169
|
cwd,
|
|
100
170
|
encoding: "utf-8"
|
|
101
171
|
})
|
|
102
172
|
.trim()
|
|
103
173
|
.split(/\r?\n/)
|
|
104
|
-
.filter(Boolean);
|
|
174
|
+
.filter(Boolean));
|
|
105
175
|
|
|
106
176
|
if (subjects.length === 0) return "无源码提交变更";
|
|
107
177
|
|
|
@@ -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 };
|
package/workspace/config.js
CHANGED
|
@@ -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
|
+
};
|