From d0c71e2aa4f9482b8a210588353391fa2057ab9b Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 7 Feb 2022 12:36:36 +0100 Subject: [PATCH] cli: use node API from ESLint to invoke it rather than a subprocess Signed-off-by: Patrik Oldsberg --- .changeset/three-dolls-fly.md | 5 +++++ packages/cli/src/commands/lint.ts | 30 ++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 .changeset/three-dolls-fly.md diff --git a/.changeset/three-dolls-fly.md b/.changeset/three-dolls-fly.md new file mode 100644 index 0000000000..df711c547d --- /dev/null +++ b/.changeset/three-dolls-fly.md @@ -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. diff --git a/packages/cli/src/commands/lint.ts b/packages/cli/src/commands/lint.ts index 5c3e0a88e3..6db17dbfc4 100644 --- a/packages/cli/src/commands/lint.ts +++ b/packages/cli/src/commands/lint.ts @@ -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); + } };