-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Expand file tree
/
Copy pathcli.ts
More file actions
175 lines (151 loc) · 5.61 KB
/
cli.ts
File metadata and controls
175 lines (151 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import type { Argv } from 'yargs';
import { CommandModule, CommandModuleImplementation } from '../../command-builder/command-module';
import { colors } from '../../utilities/color';
import { RootCommands } from '../command-config';
import { PackageVersionInfo, gatherVersionInfo } from './version-info';
/**
* The Angular CLI logo, displayed as ASCII art.
*/
const ASCII_ART = `
_ _ ____ _ ___
/ \\ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \\ | '_ \\ / _\` | | | | |/ _\` | '__| | | | | | |
/ ___ \\| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \\_\\_| |_|\\__, |\\__,_|_|\\__,_|_| \\____|_____|___|
|___/
`
.split('\n')
.map((x) => colors.red(x))
.join('\n');
/**
* The command-line module for the `ng version` command.
*/
export default class VersionCommandModule
extends CommandModule
implements CommandModuleImplementation
{
command = 'version';
aliases = RootCommands['version'].aliases;
describe = 'Outputs Angular CLI version.';
longDescriptionPath?: string | undefined;
/**
* Builds the command-line options for the `ng version` command.
* @param localYargs The `yargs` instance to configure.
* @returns The configured `yargs` instance.
*/
builder(localYargs: Argv): Argv {
return localYargs.option('json', {
describe: 'Outputs version information in JSON format.',
type: 'boolean',
});
}
/**
* The main execution logic for the `ng version` command.
*/
async run(options: { json?: boolean }): Promise<void> {
const { logger } = this.context;
const versionInfo = await gatherVersionInfo(this.context);
if (options.json) {
// eslint-disable-next-line no-console
console.log(JSON.stringify(versionInfo, null, 2));
return;
}
const {
cli: { version: ngCliVersion },
framework,
system: {
node: { version: nodeVersion, unsupported: unsupportedNodeVersion },
os: { platform: os, architecture: arch },
packageManager: { name: packageManagerName, version: packageManagerVersion },
},
packages,
} = versionInfo;
const headerInfo = [{ label: 'Angular CLI', value: ngCliVersion }];
if (framework.version) {
headerInfo.push({ label: 'Angular', value: framework.version });
}
headerInfo.push(
{
label: 'Node.js',
value: `${nodeVersion}${unsupportedNodeVersion ? colors.yellow(' (Unsupported)') : ''}`,
},
{
label: 'Package Manager',
value: `${packageManagerName} ${packageManagerVersion ?? '<error>'}`,
},
{ label: 'Operating System', value: `${os} ${arch}` },
);
const maxHeaderLabelLength = Math.max(...headerInfo.map((l) => l.label.length));
const header = headerInfo
.map(
({ label, value }) =>
colors.bold(label.padEnd(maxHeaderLabelLength + 2)) + `: ${colors.cyan(value)}`,
)
.join('\n');
const packageTable = this.formatPackageTable(packages);
logger.info([ASCII_ART, header, packageTable].join('\n\n'));
if (unsupportedNodeVersion) {
logger.warn(
`Warning: The current version of Node (${nodeVersion}) is not supported by Angular.`,
);
}
}
/**
* Formats the package table section of the version output.
* @param versions A map of package names to their versions.
* @returns A string containing the formatted package table.
*/
private formatPackageTable(versions: Record<string, PackageVersionInfo>): string {
const versionKeys = Object.keys(versions);
if (versionKeys.length === 0) {
return '';
}
const headers = {
name: 'Package',
installed: 'Installed Version',
requested: 'Requested Version',
};
const maxNameLength = Math.max(headers.name.length, ...versionKeys.map((key) => key.length));
const maxInstalledLength = Math.max(
headers.installed.length,
...versionKeys.map((key) => versions[key].installed.length),
);
const maxRequestedLength = Math.max(
headers.requested.length,
...versionKeys.map((key) => versions[key].requested.length),
);
const tableRows = versionKeys
.map((module) => {
const { requested, installed } = versions[module];
const name = module.padEnd(maxNameLength);
const coloredInstalled =
installed === '<error>' ? colors.red(installed) : colors.cyan(installed);
const installedPadding = ' '.repeat(maxInstalledLength - installed.length);
return `│ ${name} │ ${coloredInstalled}${installedPadding} │ ${requested.padEnd(
maxRequestedLength,
)} │`;
})
.sort();
const top = `┌─${'─'.repeat(maxNameLength)}─┬─${'─'.repeat(
maxInstalledLength,
)}─┬─${'─'.repeat(maxRequestedLength)}─┐`;
const header =
`│ ${headers.name.padEnd(maxNameLength)} │ ` +
`${headers.installed.padEnd(maxInstalledLength)} │ ` +
`${headers.requested.padEnd(maxRequestedLength)} │`;
const separator = `├─${'─'.repeat(maxNameLength)}─┼─${'─'.repeat(
maxInstalledLength,
)}─┼─${'─'.repeat(maxRequestedLength)}─┤`;
const bottom = `└─${'─'.repeat(maxNameLength)}─┴─${'─'.repeat(
maxInstalledLength,
)}─┴─${'─'.repeat(maxRequestedLength)}─┘`;
return [top, header, separator, ...tableRows, bottom].join('\n');
}
}