-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathchanged.js
178 lines (150 loc) · 3.96 KB
/
changed.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
'use strict';
const { spawnSync } = require('child_process');
const prompts = require('prompts');
const { runInDir } = require('./run-in-dir');
const { withProgress } = require('@mongodb-js/monorepo-tools');
async function runCommandForPackages(command, packages) {
packages = [...packages];
const passed = [];
const failed = [];
while (packages.length) {
console.log();
const pkg = packages[0];
const { status, signal } = spawnSync(
'npx',
['lerna', 'run', command, '--scope', pkg.name, '--stream'],
{
cwd: process.cwd(),
stdio: 'inherit',
timeout: 1000 * 60 * 20,
}
);
if (status === 0) {
passed.push(packages.shift());
continue;
}
if (signal) {
const err = new Error(`Child process killed with signal ${signal}`);
err.signal = signal;
throw err;
}
console.log();
let canceled = false;
const { action } = await prompts(
[
{
type: 'select',
name: 'action',
message: `Running ${command} for the package ${pkg.name} failed`,
choices: [
{ title: 'Re-run', value: 'rerun' },
{ title: 'Skip', value: 'skip' },
],
initial: 0,
},
],
{
onCancel() {
canceled = true;
},
}
);
if (canceled) {
break;
}
if (action === 'skip') {
failed.push(packages.shift());
continue;
}
if (action === 'rerun') {
continue;
}
throw new Error(`Unsupported option "${action}"`);
}
return { passed, failed };
}
async function runUntilDone(command, packages) {
packages = [...packages];
// eslint-disable-next-line no-constant-condition
while (true) {
const { passed, failed } = await runCommandForPackages(command, packages);
console.log();
console.log(
`Finished running ${command} command for ${packages.length} package${
packages.length === 1 ? '' : 's'
}`
);
console.log();
if (passed.length === packages.length) {
console.log('✔ All scripts passed!');
console.log();
break;
}
console.log(
`✔ ${passed.length} package${passed.length === 1 ? '' : 's'} passed`
);
console.log(
`✖ ${failed.length} package${failed.length === 1 ? '' : 's'} failed`
);
console.log();
const { shouldRerun } = await prompts({
type: 'confirm',
name: 'shouldRerun',
message: 'Do you want to rerun failed scripts?',
initial: true,
});
if (shouldRerun === false) {
break;
}
packages = [...failed];
}
}
async function main() {
const args = process.argv.slice(2);
const interactive = args.includes('--interactive');
const command = args.find((arg) => !arg.startsWith('-'));
/** @type {{ name: string }[]} */
const changedPackages = await withProgress(
'Looking up packages changed since origin/HEAD',
async function () {
const spinner = this;
const { stdout } = await runInDir(
'npx lerna list --all --since origin/HEAD --json --exclude-dependents'
);
const result = JSON.parse(stdout);
spinner.text =
result.length > 0
? `Found ${result.length} package${
result.length === 1 ? '' : 's'
} changed since origin/HEAD`
: `No packages changed since origin/HEAD`;
return result;
}
);
console.log();
if (!command) {
throw new Error(
'Command is required: npm run changed test [--interactive]'
);
}
if (interactive) {
return await runUntilDone(command, changedPackages);
}
spawnSync(
'npx',
[
'lerna',
'run',
'--stream',
command,
...changedPackages.map((pkg) => ['--scope', pkg.name]).flat(),
],
{ cwd: process.cwd(), stdio: 'inherit' }
);
}
process.on('unhandledRejection', (err) => {
console.error();
console.error(err.stack || err.message || err);
process.exitCode = 1;
});
main();