Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 58d101d

Browse filesBrowse files
committed
feat(@angular/cli): add --json output to ng version
This commit introduces a `--json` option to the `ng version` command. When this flag is used, the command will output all version information in a machine-readable JSON format. The JSON structure is designed to be a stable, well-organized public API, with information grouped into `cli`, `system`, and `packages` categories. This is useful for scripting, tool integration, and creating detailed, parsable bug reports. The standard, human-readable output is still the default.
1 parent 68b7d48 commit 58d101d
Copy full SHA for 58d101d

File tree

Expand file treeCollapse file tree

2 files changed

+59
-29
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+59
-29
lines changed
Open diff view settings
Collapse file

‎packages/angular/cli/src/commands/version/cli.ts‎

Copy file name to clipboardExpand all lines: packages/angular/cli/src/commands/version/cli.ts
+21-11Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,34 @@ export default class VersionCommandModule
4545
* @returns The configured `yargs` instance.
4646
*/
4747
builder(localYargs: Argv): Argv {
48-
return localYargs;
48+
return localYargs.option('json', {
49+
describe: 'Outputs version information in JSON format.',
50+
type: 'boolean',
51+
});
4952
}
5053

5154
/**
5255
* The main execution logic for the `ng version` command.
5356
*/
54-
async run(): Promise<void> {
57+
async run(options: { json?: boolean }): Promise<void> {
5558
const { logger } = this.context;
5659
const versionInfo = gatherVersionInfo(this.context);
60+
61+
if (options.json) {
62+
// eslint-disable-next-line no-console
63+
console.log(JSON.stringify(versionInfo, null, 2));
64+
65+
return;
66+
}
67+
5768
const {
58-
ngCliVersion,
59-
nodeVersion,
60-
unsupportedNodeVersion,
61-
packageManagerName,
62-
packageManagerVersion,
63-
os,
64-
arch,
65-
versions,
69+
cli: { version: ngCliVersion },
70+
system: {
71+
node: { version: nodeVersion, unsupported: unsupportedNodeVersion },
72+
os: { platform: os, architecture: arch },
73+
packageManager: { name: packageManagerName, version: packageManagerVersion },
74+
},
75+
packages,
6676
} = versionInfo;
6777

6878
const headerInfo = [
@@ -87,7 +97,7 @@ export default class VersionCommandModule
8797
)
8898
.join('\n');
8999

90-
const packageTable = this.formatPackageTable(versions);
100+
const packageTable = this.formatPackageTable(packages);
91101

92102
logger.info([ASCII_ART, header, packageTable].join('\n\n'));
93103

Collapse file

‎packages/angular/cli/src/commands/version/version-info.ts‎

Copy file name to clipboardExpand all lines: packages/angular/cli/src/commands/version/version-info.ts
+38-18Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,24 @@ interface PartialPackageInfo {
2323
* An object containing all the version information that will be displayed by the command.
2424
*/
2525
export interface VersionInfo {
26-
ngCliVersion: string;
27-
versions: Record<string, string>;
28-
unsupportedNodeVersion: boolean;
29-
nodeVersion: string;
30-
packageManagerName: string;
31-
packageManagerVersion: string | undefined;
32-
os: string;
33-
arch: string;
26+
cli: {
27+
version: string;
28+
};
29+
system: {
30+
node: {
31+
version: string;
32+
unsupported: boolean;
33+
};
34+
os: {
35+
platform: string;
36+
architecture: string;
37+
};
38+
packageManager: {
39+
name: string;
40+
version: string | undefined;
41+
};
42+
};
43+
packages: Record<string, string>;
3444
}
3545

3646
/**
@@ -81,22 +91,32 @@ export function gatherVersionInfo(context: {
8191
}),
8292
);
8393

84-
const versions: Record<string, string> = {};
94+
const packages: Record<string, string> = {};
8595
for (const name of packageNames) {
8696
if (PACKAGE_PATTERNS.some((p) => p.test(name))) {
87-
versions[name] = getVersion(name, workspaceRequire);
97+
packages[name] = getVersion(name, workspaceRequire);
8898
}
8999
}
90100

91101
return {
92-
ngCliVersion: VERSION.full,
93-
versions,
94-
unsupportedNodeVersion,
95-
nodeVersion: process.versions.node,
96-
packageManagerName: context.packageManager.name,
97-
packageManagerVersion: context.packageManager.version,
98-
os: process.platform,
99-
arch: process.arch,
102+
cli: {
103+
version: VERSION.full,
104+
},
105+
system: {
106+
node: {
107+
version: process.versions.node,
108+
unsupported: unsupportedNodeVersion,
109+
},
110+
os: {
111+
platform: process.platform,
112+
architecture: process.arch,
113+
},
114+
packageManager: {
115+
name: context.packageManager.name,
116+
version: context.packageManager.version,
117+
},
118+
},
119+
packages,
100120
};
101121
}
102122

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.