diff --git a/.changeset/fruity-tires-fail.md b/.changeset/fruity-tires-fail.md index 65d3ff26e8..a0afd0ed7b 100644 --- a/.changeset/fruity-tires-fail.md +++ b/.changeset/fruity-tires-fail.md @@ -2,4 +2,4 @@ '@backstage/cli': patch --- -Added `--include` and `--output-file` options to `backstage-cli info` command for including additional packages via glob patterns and exporting output to JSON. +Added `--include` and `--format` options to `backstage-cli info` command for including additional packages via glob patterns and outputting as JSON or Text. diff --git a/docs/tooling/cli/03-commands.md b/docs/tooling/cli/03-commands.md index 3ab2c678d2..07f6dee879 100644 --- a/docs/tooling/cli/03-commands.md +++ b/docs/tooling/cli/03-commands.md @@ -444,7 +444,7 @@ Usage: backstage-cli info [options] Options: --include Glob patterns for additional packages to include (e.g., @mycompany/backstage-*) - --output-file Write the info output to a JSON file instead of stdout + --format Output format (default: text) -h, --help display help for command ``` @@ -462,21 +462,33 @@ Include additional packages matching a glob pattern: yarn backstage-cli info --include "@mycompany/*" ``` -Export information to a JSON file for further processing: +Output as JSON: ```bash -yarn backstage-cli info --output-file backstage-info.json +yarn backstage-cli info --format json +``` + +Export JSON to a file for further processing: + +```bash +yarn backstage-cli info --format json > backstage-info.json ``` Combine options to include custom packages and export to JSON: ```bash -yarn backstage-cli info --include "@mycompany/backstage-*" --include "@internal/*" --output-file debug-info.json +yarn backstage-cli info --include "@mycompany/backstage-*" --include "@internal/*" --format json > debug-info.json +``` + +Export text output to a file: + +```bash +yarn backstage-cli info --format text > backstage-info.txt ``` ### JSON Output Format -When using `--output-file`, the output is structured as follows: +When using `--format json`, the output is structured as follows: ```json { diff --git a/packages/cli/cli-report.md b/packages/cli/cli-report.md index 7dd3de73ca..f3e077b37e 100644 --- a/packages/cli/cli-report.md +++ b/packages/cli/cli-report.md @@ -148,9 +148,9 @@ Options: Usage: Options: + --format --help --include - --output-file --version ``` diff --git a/packages/cli/src/modules/info/commands/info.ts b/packages/cli/src/modules/info/commands/info.ts index 94d6c00250..4e2bda29a7 100644 --- a/packages/cli/src/modules/info/commands/info.ts +++ b/packages/cli/src/modules/info/commands/info.ts @@ -25,7 +25,7 @@ import fs from 'fs-extra'; interface InfoOptions { include: string[]; - outputFile?: string; + format: 'text' | 'json'; } /** @@ -65,7 +65,7 @@ export default async (options: InfoOptions) => { const backstageJson = await fs.readJSON(backstageFile); backstageVersion = backstageJson.version ?? 'N/A'; } catch (error) { - if (!options.outputFile) { + if (options.format !== 'json') { console.warn( 'The "backstage.json" file is not in the expected format', ); @@ -160,8 +160,8 @@ export default async (options: InfoOptions) => { const sortedInstalled = [...installedDeps].sort(); const sortedLocal = [...localDeps].sort(); - // If outputFile is specified, write JSON to file - if (options.outputFile) { + // If format is json, output JSON to stdout + if (options.format === 'json') { const output = { system: systemInfo, dependencies: Object.fromEntries( @@ -175,9 +175,7 @@ export default async (options: InfoOptions) => { ), }; - const outputPath = paths.resolveTargetRoot(options.outputFile); - await fs.writeFile(outputPath, JSON.stringify(output, null, 2)); - console.log(`Info exported to ${outputPath}`); + process.stdout.write(`${JSON.stringify(output, null, 2)}\n`); return; } diff --git a/packages/cli/src/modules/info/index.ts b/packages/cli/src/modules/info/index.ts index df858cf72d..c14926df85 100644 --- a/packages/cli/src/modules/info/index.ts +++ b/packages/cli/src/modules/info/index.ts @@ -33,10 +33,11 @@ export default createCliPlugin({ description: 'Glob patterns for additional packages to include (e.g., @spotify/backstage*)', }, - 'output-file': { + format: { type: 'string', - description: - 'Write the info output to a JSON file instead of stdout', + choices: ['text', 'json'], + default: 'text', + description: 'Output format (text or json)', }, }) .help()