cli: use node API from ESLint to invoke it rather than a subprocess

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-02-07 12:36:36 +01:00
parent fae2aee878
commit d0c71e2aa4
2 changed files with 25 additions and 10 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/cli': patch
---
Switched the `lint` command to invoke ESLint directly through its Node.js API rather than spawning a new process.
+20 -10
View File
@@ -15,19 +15,29 @@
*/
import { Command } from 'commander';
import { run } from '../lib/run';
import { paths } from '../lib/paths';
import { ESLint } from 'eslint';
export default async (cmd: Command) => {
const eslint = new ESLint({
cwd: paths.targetDir,
fix: cmd.fix,
extensions: ['js', 'jsx', 'ts', 'tsx', 'mjs', 'cjs'],
});
const results = await eslint.lintFiles(['.']);
export default async (cmd: Command, cmdArgs: string[]) => {
const args = [
'--ext=js,jsx,ts,tsx,mjs,cjs',
'--max-warnings=0',
`--format=${cmd.format}`,
...(cmdArgs ?? [paths.targetDir]),
];
if (cmd.fix) {
args.push('--fix');
await ESLint.outputFixes(results);
}
await run('eslint', args);
const formatter = await eslint.loadFormatter(cmd.format);
const resultText = formatter.format(results);
// If there is any feedback at all, we treat it as a lint failure. This should be
// consistent with our old behavior of passing `--max-warnings=0` when invoking eslint.
if (resultText) {
console.log(resultText);
process.exit(1);
}
};