forked from facebook/sapling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify-addons-folder.js
More file actions
executable file
·347 lines (302 loc) · 7.99 KB
/
Copy pathverify-addons-folder.js
File metadata and controls
executable file
·347 lines (302 loc) · 7.99 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env node
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// @ts-check
/* eslint-disable no-console */
const {spawn} = require('child_process');
const path = require('path');
const process = require('process');
const addons = __dirname;
const description = `Verifies the contents of this folder by running tests and linters.
Requirements:
- \`node\` and \`yarn\` are on the \`$PATH\`
- \`yarn install\` has already been run in the addons/ folder
- \`sl\` required to be on the PATH for isl integration tests`;
const TASK_LABEL_WIDTH = 14;
const COMMAND_LABEL_WIDTH = 34;
/**
* @typedef {{
* useVendoredGrammars: boolean;
* skipIntegrationTests: boolean;
* }} Args
*/
class CommandError extends Error {
/** @type {string} */
taskLabel;
/** @type {string} */
commandLabel;
/** @type {string} */
output;
/**
* @param {string} cwd
* @param {string[]} command
* @param {number | null} exitCode
* @param {string} output
*/
constructor(cwd, command, exitCode, output) {
super(`command failed (${exitCode ?? 'unknown'}): ${command.join(' ')}`);
this.name = 'CommandError';
this.taskLabel = deriveTaskLabel(cwd);
this.commandLabel = deriveCommandLabel(command);
this.output = output;
}
}
class Timer {
/** @type {number} */
#start;
constructor() {
this.#start = Date.now();
}
/**
* @returns {string}
*/
elapsed() {
const durationSeconds = (Date.now() - this.#start) / 1000;
return `${durationSeconds.toFixed(2)}s`;
}
}
/**
* @returns {Args}
*/
function parseArgs() {
const args = process.argv.slice(2);
/** @type {Args} */
const parsed = {
useVendoredGrammars: false,
skipIntegrationTests: false,
};
for (const arg of args) {
switch (arg) {
case '--use-vendored-grammars':
parsed.useVendoredGrammars = true;
break;
case '--skip-integration-tests':
parsed.skipIntegrationTests = true;
break;
case '-h':
case '--help':
printHelp(0);
break;
default:
printHelp(1, `Unknown argument: ${arg}`);
}
}
return parsed;
}
/**
* @param {number} exitCode
* @param {string=} errorMessage
* @returns {never}
*/
function printHelp(exitCode, errorMessage) {
const lines = [
'usage: verify-addons-folder.js [--use-vendored-grammars] [--skip-integration-tests]',
'',
description,
'',
'options:',
' --use-vendored-grammars No-op. Provided for compatibility.',
" --skip-integration-tests Don't run isl integrations tests",
' -h, --help show this help message and exit',
];
const output = lines.join('\n');
if (errorMessage != null) {
console.error(errorMessage);
console.error(output);
} else {
console.log(output);
}
process.exit(exitCode);
}
/**
* @param {Args} args
* @returns {Promise<void>}
*/
async function verify(args) {
await Promise.all([
verifyPrettier(),
verifyShared(),
verifyComponents(),
verifyTextmate(),
verifyIsl(args),
verifyInternal(),
]);
}
/** @returns {Promise<void>} */
async function verifyPrettier() {
await runTask(['yarn', 'run', 'prettier-check'], addons);
}
/** @returns {Promise<void>} */
async function verifyShared() {
await lintAndTest(path.join(addons, 'shared'));
}
/** @returns {Promise<void>} */
async function verifyComponents() {
await lintAndTest(path.join(addons, 'components'));
}
/** @returns {Promise<void>} */
async function verifyTextmate() {
const textmate = path.join(addons, 'textmate');
await Promise.all([
runTask(['yarn', 'run', 'tsc', '--noEmit'], textmate),
runTask(['yarn', 'run', 'lint'], textmate),
]);
}
/** @returns {Promise<void>} */
async function verifyInternal() {
await runTask(['yarn', 'run', 'verify-internal'], addons);
}
/**
* Verifies `isl/` and `isl-server/` and `vscode/` as the builds are interdependent.
* @param {Args} args
* @returns {Promise<void>}
*/
async function verifyIsl(args) {
const isl = path.join(addons, 'isl');
const islServer = path.join(addons, 'isl-server');
const vscode = path.join(addons, 'vscode');
await runTask(['yarn', 'codegen'], islServer);
await Promise.all([runTask(['yarn', 'build'], islServer), runTask(['yarn', 'build'], isl)]);
await Promise.all([
runTask(['yarn', 'build-extension'], vscode),
runTask(['yarn', 'build-webview'], vscode),
]);
await Promise.all([lintAndTest(isl), lintAndTest(islServer), lintAndTest(vscode)]);
if (!args.skipIntegrationTests) {
await runTask(['yarn', 'integration', '--watchAll=false'], isl);
}
}
/**
* @param {string} cwd
* @returns {Promise<void>}
*/
async function lintAndTest(cwd) {
await Promise.all([
runTask(['yarn', 'run', 'lint'], cwd),
runTask(['yarn', 'run', 'tsc', '--noEmit'], cwd),
runTask(['yarn', 'test', '--watchAll=false'], cwd),
]);
}
/**
* @param {string[]} command
* @param {string} cwd
* @returns {Promise<void>}
*/
async function runTask(command, cwd) {
const timer = new Timer();
const taskLabel = formatTask(deriveTaskLabel(cwd));
const commandLabel = formatCommand(deriveCommandLabel(command));
await run(command, cwd);
console.log(`${ok(taskLabel, commandLabel)} in ${timer.elapsed()}`);
}
/**
* @param {string[]} command
* @param {string} cwd
* @returns {Promise<void>}
*/
function run(command, cwd) {
return new Promise((resolve, reject) => {
const child = spawn(command[0], command.slice(1), {
cwd,
env: process.env,
stdio: ['ignore', 'pipe', 'pipe'],
});
/** @type {Buffer[]} */
const stdoutChunks = [];
/** @type {Buffer[]} */
const stderrChunks = [];
child.stdout.on('data', chunk => {
stdoutChunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
});
child.stderr.on('data', chunk => {
stderrChunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
});
child.on('error', error => {
reject(new CommandError(cwd, command, null, String(error)));
});
child.on('close', code => {
if (code === 0) {
resolve();
return;
}
const stdout = Buffer.concat(stdoutChunks).toString('utf8').trimEnd();
const stderr = Buffer.concat(stderrChunks).toString('utf8').trimEnd();
const output = [stdout, stderr].filter(part => part !== '').join('\n');
reject(new CommandError(cwd, command, code, output));
});
});
}
/**
* @param {string} cwd
* @returns {string}
*/
function deriveTaskLabel(cwd) {
if (cwd === addons) {
return '';
}
return `${path.basename(cwd)}/`;
}
/**
* @param {string[]} command
* @returns {string}
*/
function deriveCommandLabel(command) {
return command.join(' ');
}
/**
* @param {string} value
* @returns {string}
*/
function formatTask(value) {
return value === '' ? ''.padEnd(TASK_LABEL_WIDTH) : `[${value}]`.padEnd(TASK_LABEL_WIDTH);
}
/**
* @param {string} value
* @returns {string}
*/
function formatCommand(value) {
return value.padEnd(COMMAND_LABEL_WIDTH);
}
/**
* @param {unknown} error
*/
function reportFailure(error) {
if (error instanceof CommandError) {
console.error(`\n${fail(formatTask(error.taskLabel), formatCommand(error.commandLabel))}:`);
if (error.output !== '') {
console.error(error.output);
} else {
console.error(error.message);
}
return;
}
console.error('\nFAIL unexpected error:');
console.error(error);
}
const RED = '\u001b[0;31m';
const GREEN = '\u001b[0;32m';
const RESET = '\u001b[0m';
/**
* @param {string} taskLabel
* @param {string} commandLabel
* @returns {string}
*/
function ok(taskLabel, commandLabel) {
return `${GREEN}OK${RESET} ${taskLabel} ${commandLabel}`;
}
/**
* @param {string} taskLabel
* @param {string} commandLabel
* @returns {string}
*/
function fail(taskLabel, commandLabel) {
return `${RED}FAIL${RESET} ${taskLabel} ${commandLabel}`;
}
verify(parseArgs()).catch(error => {
reportFailure(error);
process.exit(1);
});