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:
@@ -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.
|
||||
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user