forked from vercel/vercel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.js
112 lines (98 loc) · 2.91 KB
/
run.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const { execSync, spawn } = require('child_process');
const { join, relative } = require('path');
const { readdirSync } = require('fs');
async function main() {
const script = process.argv[2];
const all = process.argv[3];
let matches = [];
if (!script) {
console.error('Please provide at least one argument');
process.exit(2);
}
if (all === 'all') {
matches = readdirSync(join(__dirname, 'packages'));
console.log(`Running script "${script}" for all packages`);
} else {
const branch = execSync('git branch | grep "*" | cut -d " " -f2')
.toString()
.trim();
const gitPath = branch === 'master' ? 'HEAD~1' : 'origin/canary...HEAD';
const diff = execSync(`git diff ${gitPath} --name-only`).toString();
const changed = diff
.split('\n')
.filter(item => Boolean(item) && item.includes('packages/'))
.map(item => relative('packages', item).split('/')[0]);
matches = Array.from(new Set(changed));
if (matches.length === 0) {
matches.push('now-cli');
matches.push('now-node');
console.log('No packages changed. Using default packages.');
}
console.log(
`Running "${script}" on branch "${branch}" with the following packages:`
);
}
// Sort the matches such that `utils` modules are compiled first,
// because other packages may rely on them
// We also need to ensure that `now-client` is built before the CLI
matches.sort((a, b) => {
if (a.endsWith('-utils') && !b.endsWith('-utils')) {
return -1;
}
if (b.endsWith('-utils') && !a.endsWith('-utils')) {
return 1;
}
if (a === 'now-cli' && b !== 'now-cli') {
return 1;
}
if (b === 'now-cli' && a !== 'now-cli') {
return -1;
}
return b - a;
});
console.log(matches.join('\n') + '\n');
for (const pkgName of matches) {
await runScript(pkgName, script);
}
}
function runScript(pkgName, script) {
return new Promise((resolve, reject) => {
const cwd = join(__dirname, 'packages', pkgName);
let pkgJson = null;
try {
pkgJson = require(join(cwd, 'package.json'));
} catch (e) {
pkgJson = null;
}
if (pkgJson && pkgJson.scripts && pkgJson.scripts[script]) {
console.log(`\n[${pkgName}] Running yarn ${script}`);
const child = spawn('yarn', [script], { cwd, stdio: 'inherit' });
child.on('error', reject);
child.on('close', (code, signal) => {
if (code === 0) {
return resolve();
}
reject(
new Error(
`[${pkgName}] Exited script "${script}" with code ${code ||
signal}.`
)
);
});
} else {
console.log(
`[${pkgName}] Skipping since script "${script}" is missing from package.json`
);
resolve();
}
});
}
main()
.then(() => {
console.log('Success!');
process.exit(0);
})
.catch(e => {
console.error(e);
process.exit(1);
});