-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfix.ts
More file actions
55 lines (48 loc) · 1.47 KB
/
fix.ts
File metadata and controls
55 lines (48 loc) · 1.47 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
import { spawnSync } from 'node:child_process';
import chalk from 'chalk';
export async function fix(opts: { check: boolean }): Promise<void> {
const failures = new Array<string>();
if (!format(opts.check)) {
failures.push('format');
}
if (!lint(opts.check)) {
failures.push('lint');
}
if (!typecheck()) {
failures.push('typecheck');
}
if (failures.length > 0) {
throw new Error(`fix failed: ${failures.join(', ')}`);
}
}
function format(check: boolean): boolean {
const args = ['prettier', '.', '--loglevel=warn', check ? '--check' : '--write'];
const ok = exec('npx', args);
console.log(`format: ${ok ? chalk.green('success') : chalk.red('failed')}`);
return ok;
}
function lint(check: boolean): boolean {
const args = ['eslint', '.'];
if (!check) {
args.push('--fix');
}
const ok = exec('npx', args);
console.log(`lint: ${ok ? chalk.green('success') : chalk.red('failed')}`);
return ok;
}
function typecheck(): boolean {
const ok = exec('npx', ['tsc', '--noEmit']);
console.log(`typecheck: ${ok ? chalk.green('success') : chalk.red('failed')}`);
return ok;
}
function exec(command: string, args: string[]): boolean {
console.log(chalk.cyan(`$ ${[command, ...args].join(' ')}`));
const result = spawnSync(command, args, { stdio: 'inherit' });
if (result.error) {
throw result.error;
}
if (result.signal) {
throw new Error(`${command} terminated with signal ${result.signal}`);
}
return result.status === 0;
}