|
1 | 1 | /* eslint-disable no-console */
|
2 |
| -import chalk from 'chalk'; |
3 |
| -import { spawn } from 'child_process'; |
4 |
| -import { Command } from 'commander'; |
5 |
| -import path from 'path'; |
6 |
| - |
7 | 2 | import { npmPkgJsonLint } from './npmPkgJsonLint';
|
8 | 3 |
|
9 |
| -const rootDir = process.cwd(); |
10 |
| -const eslintConfigPath = path.resolve(__dirname, '../config/eslint.config.js'); |
11 |
| -const prettierConfigPath = path.resolve( |
12 |
| - __dirname, |
13 |
| - '../config/prettier.config.js', |
14 |
| -); |
15 |
| - |
16 |
| -const esLintExtensions = ['js', 'ts', 'tsx']; |
17 |
| -const prettierExtensions = [...esLintExtensions, 'mjs', 'json', 'md', 'yml']; |
18 |
| - |
19 |
| -interface LintFlags { |
20 |
| - fix: boolean; |
21 |
| - eslintOnly: boolean; |
22 |
| - prettierOnly: boolean; |
23 |
| - pkgJsonOnly: boolean; |
24 |
| - verbose: boolean; |
25 |
| -} |
26 |
| - |
27 |
| -const cli = new Command() |
28 |
| - .option('-f, --fix', 'fix linting errors', false) |
29 |
| - .option('-e, --eslintOnly', 'run eslint only', false) |
30 |
| - .option('-p, --prettierOnly', 'run prettier only', false) |
31 |
| - .option('--pkgJsonOnly', 'run npmPackageJsonLint only', false) |
32 |
| - .option('--verbose', 'verbose mode', false) |
33 |
| - .parse(process.argv); |
| 4 | +import { eslint } from './eslint'; |
| 5 | +import { LintCommandOptions } from './lint.types'; |
| 6 | +import { prettier } from './prettier'; |
34 | 7 |
|
35 |
| -const { fix, prettierOnly, eslintOnly, pkgJsonOnly, verbose } = |
36 |
| - cli.opts() as LintFlags; |
| 8 | +export const lint = (options: LintCommandOptions) => { |
| 9 | + const { fix, prettierOnly, eslintOnly, pkgJsonOnly, verbose } = options; |
37 | 10 |
|
38 |
| -// If prettierOnly or eslintOnly is true, run only that linter |
39 |
| -if (prettierOnly || eslintOnly || pkgJsonOnly) { |
40 |
| - let lintPromise: Promise<unknown>; |
| 11 | + const linters: Array<Promise<unknown>> = []; |
41 | 12 |
|
42 |
| - if (eslintOnly) { |
43 |
| - lintPromise = eslint(); |
44 |
| - } else if (prettierOnly) { |
45 |
| - lintPromise = prettier(); |
46 |
| - } else { |
47 |
| - lintPromise = npmPkgJsonLint(fix); |
| 13 | + if (!prettierOnly && !pkgJsonOnly) { |
| 14 | + linters.push(eslint({ fix, verbose })); |
| 15 | + } |
| 16 | + if (!eslintOnly && !pkgJsonOnly) { |
| 17 | + linters.push(prettier({ fix, verbose })); |
| 18 | + } |
| 19 | + if (!prettierOnly && !eslintOnly) { |
| 20 | + linters.push(npmPkgJsonLint({ fix, verbose })); |
48 | 21 | }
|
49 |
| - lintPromise.then(() => process.exit(0)); |
50 |
| -} else { |
51 |
| - // Otherwise, run all linters |
52 |
| - (async () => { |
53 |
| - await eslint(); |
54 |
| - await prettier(); |
55 |
| - await npmPkgJsonLint(fix); |
56 |
| - })(); |
57 |
| -} |
58 |
| - |
59 |
| -/** Spawns an eslint job */ |
60 |
| -function eslint() { |
61 |
| - return new Promise<void>(resolve => { |
62 |
| - console.log(chalk.blue('Running ESLint...')); |
63 |
| - spawn( |
64 |
| - 'eslint', |
65 |
| - [ |
66 |
| - '--config', |
67 |
| - eslintConfigPath, |
68 |
| - `${rootDir}/**/*.{${esLintExtensions.join(',')}}`, |
69 |
| - fix ? '--fix' : '--no-fix', |
70 |
| - verbose ? '' : '--quiet', |
71 |
| - ], |
72 |
| - { |
73 |
| - stdio: 'inherit', |
74 |
| - }, |
75 |
| - ).on('close', resolve); |
76 |
| - }); |
77 |
| -} |
78 | 22 |
|
79 |
| -/** Spawns a prettier job */ |
80 |
| -function prettier() { |
81 |
| - return new Promise<void>(resolve => { |
82 |
| - console.log(chalk.magenta('Running Prettier...')); |
83 |
| - spawn( |
84 |
| - 'prettier', |
85 |
| - [ |
86 |
| - fix ? '--write' : '--check', |
87 |
| - '--config', |
88 |
| - prettierConfigPath, |
89 |
| - `${rootDir}/**/*.{${prettierExtensions.join(',')}}`, |
90 |
| - ], |
91 |
| - { |
92 |
| - stdio: 'inherit', |
93 |
| - }, |
94 |
| - ).on('close', resolve); |
95 |
| - }); |
96 |
| -} |
| 23 | + Promise.all(linters) |
| 24 | + .then(() => { |
| 25 | + process.exit(0); |
| 26 | + }) |
| 27 | + .catch(() => { |
| 28 | + process.exit(1); |
| 29 | + }); |
| 30 | +}; |
0 commit comments