-
Notifications
You must be signed in to change notification settings - Fork 10
/
lint.ts
74 lines (61 loc) · 2.42 KB
/
lint.ts
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 {readFileSync, readdirSync, writeFileSync} from 'node:fs';
import {cpus} from 'node:os';
import {basename, join} from 'node:path';
import runAll from 'npm-run-all';
import {setOutputGHActions} from '../src/utils';
const MAX_PARALLEL =
Number(process.env.GAPI_MAX_PARALLEL) || Math.max(cpus().length - 1, 1);
process.stdout.setMaxListeners(MAX_PARALLEL);
process.stderr.setMaxListeners(1 + MAX_PARALLEL);
process.on('unhandledRejection', reason => {
throw reason;
});
const path = process.argv[2];
console.log(`Reading project directories in ${path}...`);
const dtslintCommand = {start: 'dtslint "', end: '"'};
const scripts = readdirSync(path, {withFileTypes: true})
.filter(dir => dir.isDirectory())
.map(
dir => `${dtslintCommand.start}${join(path, dir.name)}${dtslintCommand.end}`
);
const options = {
maxParallel: MAX_PARALLEL,
parallel: true,
stdout: process.stdout,
stderr: process.stderr,
};
console.log(
`Linting ${scripts.length} projects in ${MAX_PARALLEL} parallel processes...`
);
// remove `types/` from .eslintignore file, otherwise new eslint-based dtslint won't be able to lint the files
const eslintIgnorePath = join(__dirname, '..', '.eslintignore');
const originalEslintIgnore = readFileSync(eslintIgnorePath, 'utf8');
const newEslintIgnore = originalEslintIgnore.replace('types/', '');
console.log(`Updating ${eslintIgnorePath}...`);
writeFileSync(eslintIgnorePath, newEslintIgnore);
runAll([scripts.shift()], options) // run first synchronously to install TypeScript
.then(() => runAll(scripts, options))
.catch(error => {
if (error.results) {
const results: Array<{name: string; code: number}> = error.results;
const failedTypeCommand = results.find(result => result.code === 1);
if (failedTypeCommand !== undefined) {
const failedTypeMatches = failedTypeCommand.name.match(
new RegExp(`${dtslintCommand.start}(.*)${dtslintCommand.end}`)
);
if (failedTypeMatches !== null) {
const failedType = failedTypeMatches[1];
setOutputGHActions('FAILED_TYPE', basename(failedType));
} else {
console.error('Unable to match failedType', {failedTypeMatches});
}
} else {
console.error('Unable to find failedTypeCommand', {results});
}
}
throw error;
})
.finally(() => {
console.log(`Restoring ${eslintIgnorePath}...`);
writeFileSync(eslintIgnorePath, originalEslintIgnore);
});