Skip to content

Commit 6e0bd68

Browse files
authored
Add JSON output for eval inventory (google-gemini#28058)
1 parent d3ef6ac commit 6e0bd68

4 files changed

Lines changed: 752 additions & 30 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"docs:settings": "tsx ./scripts/generate-settings-doc.ts",
3434
"docs:keybindings": "tsx ./scripts/generate-keybindings-doc.ts",
3535
"eval:inventory": "tsx ./scripts/eval-inventory-cli.ts",
36+
"eval:inventory:json": "tsx ./scripts/eval-inventory-cli.ts --json",
3637
"build": "node scripts/build.js",
3738
"build-and-start": "npm run build && npm run start --",
3839
"build:vscode": "node scripts/build_vscode_companion.js",

scripts/eval-inventory-cli.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,40 @@
1010
* @fileoverview CLI entry point for the eval inventory command.
1111
*
1212
* Scans all eval source files, runs the static analyzer on each,
13-
* and prints a human-readable inventory report grouped by policy,
14-
* file, and suite.
13+
* and prints an inventory report grouped by policy, file, and suite.
1514
*
1615
* Usage:
1716
* npm run eval:inventory
17+
* npm run eval:inventory -- --json
1818
* npm run eval:inventory -- --root /path/to/repo
19+
* npm run eval:inventory -- --root /path/to/repo --json
1920
*/
2021

2122
import {
2223
collectInventory,
24+
formatInventoryJson,
2325
formatInventoryReport,
2426
} from './utils/eval-inventory.js';
2527

2628
async function main() {
2729
const rootFlagIndex = process.argv.indexOf('--root');
28-
const repoRoot =
29-
rootFlagIndex !== -1 && process.argv[rootFlagIndex + 1]
30-
? process.argv[rootFlagIndex + 1]
31-
: process.cwd();
30+
const rootFlagValue =
31+
rootFlagIndex !== -1 ? process.argv[rootFlagIndex + 1] : undefined;
32+
if (rootFlagIndex !== -1 && rootFlagValue === undefined) {
33+
console.error(
34+
'Error: --root requires a directory path argument but none was provided.',
35+
);
36+
process.exit(1);
37+
}
38+
if (rootFlagValue && rootFlagValue.startsWith('--')) {
39+
console.error(
40+
`Error: --root value "${rootFlagValue}" looks like a flag. Provide a valid directory path.`,
41+
);
42+
process.exit(1);
43+
}
44+
const repoRoot = rootFlagValue ?? process.cwd();
45+
46+
const jsonMode = process.argv.includes('--json');
3247

3348
const result = await collectInventory(repoRoot);
3449

@@ -37,7 +52,9 @@ async function main() {
3752
process.exit(1);
3853
}
3954

40-
console.log(formatInventoryReport(result));
55+
console.log(
56+
jsonMode ? formatInventoryJson(result) : formatInventoryReport(result),
57+
);
4158
}
4259

4360
main().catch((error) => {

0 commit comments

Comments
 (0)