From a49030a3fc7cbeca12b81b7859889f0cb4f19b8a Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 2 Dec 2024 01:51:31 +0100 Subject: [PATCH] cli: add support for --output-file option for lint commands Signed-off-by: Patrik Oldsberg --- .changeset/seven-bulldogs-count.md | 5 +++++ packages/cli/cli-report.md | 2 ++ packages/cli/src/commands/index.ts | 8 ++++++++ packages/cli/src/commands/lint.ts | 9 +++++++-- packages/cli/src/commands/repo/lint.ts | 15 +++++++++++++-- 5 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 .changeset/seven-bulldogs-count.md diff --git a/.changeset/seven-bulldogs-count.md b/.changeset/seven-bulldogs-count.md new file mode 100644 index 0000000000..9fa888767b --- /dev/null +++ b/.changeset/seven-bulldogs-count.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Add support for `--output-file` option from ESLint to `package lint` and `repo lint` commands. diff --git a/packages/cli/cli-report.md b/packages/cli/cli-report.md index 3fa7198eba..d9fec4db22 100644 --- a/packages/cli/cli-report.md +++ b/packages/cli/cli-report.md @@ -236,6 +236,7 @@ Usage: backstage-cli package lint [options] [directories...] Options: --format + --output-file --fix --max-warnings -h, --help @@ -446,6 +447,7 @@ Usage: backstage-cli repo lint [options] Options: --format + --output-file --since --successCache --successCacheDir diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts index f249563a34..0f38fc1e8a 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/index.ts @@ -53,6 +53,10 @@ export function registerRepoCommand(program: Command) { 'Lint report output format', 'eslint-formatter-friendly', ) + .option( + '--output-file ', + 'Write the lint report to a file instead of stdout', + ) .option( '--since ', 'Only lint packages that changed since the specified ref', @@ -168,6 +172,10 @@ export function registerScriptCommand(program: Command) { 'Lint report output format', 'eslint-formatter-friendly', ) + .option( + '--output-file ', + 'Write the lint report to a file instead of stdout', + ) .option('--fix', 'Attempt to automatically fix violations') .option( '--max-warnings ', diff --git a/packages/cli/src/commands/lint.ts b/packages/cli/src/commands/lint.ts index 0c67d84c86..8c8877c19c 100644 --- a/packages/cli/src/commands/lint.ts +++ b/packages/cli/src/commands/lint.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import fs from 'fs-extra'; import { OptionValues } from 'commander'; import { paths } from '../lib/paths'; import { ESLint } from 'eslint'; @@ -49,10 +50,14 @@ export default async (directories: string[], opts: OptionValues) => { process.chdir(paths.targetRoot); } - const resultText = formatter.format(results); + const resultText = await formatter.format(results); if (resultText) { - console.log(resultText); + if (opts.outputFile) { + await fs.writeFile(paths.resolveTarget(opts.outputFile), resultText); + } else { + console.log(resultText); + } } if (failed) { diff --git a/packages/cli/src/commands/repo/lint.ts b/packages/cli/src/commands/repo/lint.ts index 7be021f8dd..f78b51ea5d 100644 --- a/packages/cli/src/commands/repo/lint.ts +++ b/packages/cli/src/commands/repo/lint.ts @@ -15,6 +15,7 @@ */ import chalk from 'chalk'; +import fs from 'fs-extra'; import { Command, OptionValues } from 'commander'; import { createHash } from 'crypto'; import { relative as relativePath } from 'path'; @@ -220,6 +221,8 @@ export async function command(opts: OptionValues, cmd: Command): Promise { const outputSuccessCache = []; + let errorOutput = ''; + let failed = false; for (const { relativeDir, @@ -234,14 +237,22 @@ export async function command(opts: OptionValues, cmd: Command): Promise { // When doing repo lint, only list the results if the lint failed to avoid a log // dump of all warnings that might be irrelevant if (resultText) { - console.log(); - console.log(resultText.trimStart()); + if (opts.outputFile) { + errorOutput += `${resultText}\n`; + } else { + console.log(); + console.log(resultText.trimStart()); + } } } else if (sha) { outputSuccessCache.push(sha); } } + if (opts.outputFile && errorOutput) { + await fs.writeFile(paths.resolveTargetRoot(opts.outputFile), errorOutput); + } + if (cacheContext) { await cache.write(outputSuccessCache); }