-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathrun-workspace-scripts.mjs
More file actions
74 lines (61 loc) 路 1.96 KB
/
Copy pathrun-workspace-scripts.mjs
File metadata and controls
74 lines (61 loc) 路 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { spawnSync } from 'node:child_process';
import { existsSync, readdirSync, readFileSync } from 'node:fs';
import { join, resolve } from 'node:path';
const script = process.argv[2];
if (!script) {
console.error('Usage: node scripts/run-workspace-scripts.mjs <script>');
process.exit(1);
}
const root = resolve(import.meta.dirname, '..');
const workspaceFile = join(root, 'pnpm-workspace.yaml');
function readWorkspacePatterns() {
const patterns = [];
let inPackages = false;
for (const line of readFileSync(workspaceFile, 'utf8').split('\n')) {
if (line.trim() === 'packages:') {
inPackages = true;
continue;
}
if (inPackages && line.length > 0 && !line.startsWith(' ')) {
break;
}
const match = /^\s+-\s+(.+?)\s*$/.exec(line);
if (inPackages && match) {
patterns.push(match[1].replace(/^['"]|['"]$/g, ''));
}
}
return patterns;
}
function expandWorkspacePattern(pattern) {
if (!pattern.endsWith('/*')) {
return [join(root, pattern)];
}
const baseDir = join(root, pattern.slice(0, -2));
return readdirSync(baseDir, { withFileTypes: true })
.filter((entry) => entry.isDirectory())
.map((entry) => join(baseDir, entry.name));
}
let matched = 0;
const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm';
for (const workspaceDir of readWorkspacePatterns().flatMap(expandWorkspacePattern)) {
const packageJsonPath = join(workspaceDir, 'package.json');
if (!existsSync(packageJsonPath)) {
continue;
}
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
if (!packageJson.scripts?.[script]) {
continue;
}
matched++;
console.log(`> ${packageJson.name ?? workspaceDir} ${script}`);
const result = spawnSync(npmCommand, ['run', '--silent', script], {
cwd: workspaceDir,
stdio: 'inherit',
});
if (result.status !== 0) {
process.exit(result.status ?? 1);
}
}
if (matched === 0) {
console.log(`No workspace scripts found for "${script}"`);
}