cli: add support for --output-file option for lint commands

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-12-02 01:51:31 +01:00
parent f19c16fad2
commit a49030a3fc
5 changed files with 35 additions and 4 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/cli': patch
---
Add support for `--output-file` option from ESLint to `package lint` and `repo lint` commands.
+2
View File
@@ -236,6 +236,7 @@ Usage: backstage-cli package lint [options] [directories...]
Options:
--format <format>
--output-file <path>
--fix
--max-warnings <number>
-h, --help
@@ -446,6 +447,7 @@ Usage: backstage-cli repo lint [options]
Options:
--format <format>
--output-file <path>
--since <ref>
--successCache
--successCacheDir <path>
+8
View File
@@ -53,6 +53,10 @@ export function registerRepoCommand(program: Command) {
'Lint report output format',
'eslint-formatter-friendly',
)
.option(
'--output-file <path>',
'Write the lint report to a file instead of stdout',
)
.option(
'--since <ref>',
'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 <path>',
'Write the lint report to a file instead of stdout',
)
.option('--fix', 'Attempt to automatically fix violations')
.option(
'--max-warnings <number>',
+7 -2
View File
@@ -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) {
+13 -2
View File
@@ -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<void> {
const outputSuccessCache = [];
let errorOutput = '';
let failed = false;
for (const {
relativeDir,
@@ -234,14 +237,22 @@ export async function command(opts: OptionValues, cmd: Command): Promise<void> {
// 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);
}