1 | import { spawn, SpawnOptions } from 'child_process';
|
2 | import { join, resolve } from 'path';
|
3 | import { cwd, exit, platform } from 'process';
|
4 |
|
5 |
|
6 | const kProjectRoot = cwd();
|
7 | const kTSLintArguments = [
|
8 | '--config', resolve(__dirname, '..', 'tslint.json'),
|
9 | '--exclude', join(kProjectRoot, '**', 'node_modules', '**', '*'),
|
10 | '--exclude', join(kProjectRoot, '**', 'dist', '**', '*'),
|
11 | '--project', join(kProjectRoot, 'tsconfig.json'),
|
12 | join(kProjectRoot, '**', '*.{ts,tsx}')
|
13 | ];
|
14 | const kSpawnOptions: SpawnOptions = {
|
15 | stdio: 'inherit',
|
16 | shell: /^win/.test(platform)
|
17 | };
|
18 | const kPaths = [
|
19 | resolve(__dirname, '..', 'node_modules', '.bin', 'tslint'),
|
20 | 'tslint'
|
21 | ];
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | async function runTSLint(path: string): Promise<number> {
|
30 | return new Promise<number>((resolve, reject) => {
|
31 | let didFailToSpawn = false;
|
32 |
|
33 | return spawn(path, kTSLintArguments, kSpawnOptions)
|
34 | .on('error', err => {
|
35 | didFailToSpawn = true;
|
36 |
|
37 | return reject(err);
|
38 | })
|
39 | .on('exit', code => {
|
40 | if (!didFailToSpawn) {
|
41 | return resolve(code || 0);
|
42 | }
|
43 | });
|
44 | });
|
45 | }
|
46 |
|
47 | /**
|
48 | * Attempts to find and run tslint in an array of possible paths.
|
49 | *
|
50 | * @param {string[]} paths An array of possible paths to be executed in order.
|
51 | * @returns {Promise<never>} This function exits the process with the exit code of tslint or -1 if
|
52 | * tslint couldn't be found.
|
53 | */
|
54 | (async function findAndRunTSLint(paths: string[]): Promise<never> {
|
55 | for (const path of paths) {
|
56 | try {
|
57 | const code = await runTSLint(path);
|
58 |
|
59 | return exit(code);
|
60 | } catch (err) {
|
61 |
|
62 | continue;
|
63 | }
|
64 | }
|
65 |
|
66 |
|
67 | return exit(-1);
|
68 | })(kPaths).catch(() => exit(-1));
|
69 |
|
70 |
|
\ | No newline at end of file |