UNPKG

2.03 kBPlain TextView Raw
1import { spawn, SpawnOptions } from 'child_process';
2import { join, resolve } from 'path';
3import { cwd, exit, platform } from 'process';
4
5// npm/yarn set the working directory to the project root
6const kProjectRoot = cwd();
7const 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];
14const kSpawnOptions: SpawnOptions = {
15 stdio: 'inherit',
16 shell: /^win/.test(platform)
17};
18const kPaths = [
19 resolve(__dirname, '..', 'node_modules', '.bin', 'tslint'),
20 'tslint'
21];
22
23/**
24 * Attempts to run the tslint executable at the given path.
25 *
26 * @param {string} path The path to the tslint executable.
27 * @returns {Promise<number>} The exit code of tslint.
28 */
29async 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 // Couldn't find tslint at that path. Try the next.
62 continue;
63 }
64 }
65
66 // Execution should only get to this point if we couldn't find tslint in any of the paths
67 return exit(-1);
68})(kPaths).catch(() => exit(-1));
69
70
\No newline at end of file