1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const core_1 = require("@angular-devkit/core");
|
4 | const command_1 = require("../models/command");
|
5 | const fs = require("fs");
|
6 | const path = require("path");
|
7 | const child_process = require("child_process");
|
8 | const find_up_1 = require("../utilities/find-up");
|
9 | class VersionCommand extends command_1.Command {
|
10 | constructor() {
|
11 | super(...arguments);
|
12 | this.name = 'version';
|
13 | this.description = 'Outputs Angular CLI version.';
|
14 | this.arguments = [];
|
15 | this.options = [];
|
16 | }
|
17 | run(_options) {
|
18 | let angularCoreVersion = '';
|
19 | let angularSameAsCore = [];
|
20 | const pkg = require(path.resolve(__dirname, '..', 'package.json'));
|
21 | let projPkg;
|
22 | try {
|
23 | projPkg = require(path.resolve(this.project.root, 'package.json'));
|
24 | }
|
25 | catch (exception) {
|
26 | projPkg = undefined;
|
27 | }
|
28 | const patterns = [
|
29 | /^@angular\/.*/,
|
30 | /^@angular-devkit\/.*/,
|
31 | /^@ngtools\/.*/,
|
32 | /^@schematics\/.*/,
|
33 | /^rxjs$/,
|
34 | /^typescript$/,
|
35 | /^ng-packagr$/,
|
36 | /^webpack$/,
|
37 | ];
|
38 | const maybeNodeModules = find_up_1.findUp('node_modules', __dirname);
|
39 | const packageRoot = projPkg
|
40 | ? path.resolve(this.project.root, 'node_modules')
|
41 | : maybeNodeModules;
|
42 | const versions = [
|
43 | ...Object.keys(pkg && pkg['dependencies'] || {}),
|
44 | ...Object.keys(pkg && pkg['devDependencies'] || {}),
|
45 | ...Object.keys(projPkg && projPkg['dependencies'] || {}),
|
46 | ...Object.keys(projPkg && projPkg['devDependencies'] || {}),
|
47 |
|
48 | ...fs.readdirSync(packageRoot)
|
49 | .reduce((acc, name) => {
|
50 | if (name.startsWith('@')) {
|
51 | return acc.concat(fs.readdirSync(path.resolve(packageRoot, name))
|
52 | .map(subName => name + '/' + subName));
|
53 | }
|
54 | else {
|
55 | return acc.concat(name);
|
56 | }
|
57 | }, []),
|
58 | ]
|
59 | .filter(x => patterns.some(p => p.test(x)))
|
60 | .reduce((acc, name) => {
|
61 | if (name in acc) {
|
62 | return acc;
|
63 | }
|
64 | acc[name] = this.getVersion(name, packageRoot, maybeNodeModules);
|
65 | return acc;
|
66 | }, {});
|
67 | let ngCliVersion = pkg.version;
|
68 | if (!__dirname.match(/node_modules/)) {
|
69 | let gitBranch = '??';
|
70 | try {
|
71 | const gitRefName = '' + child_process.execSync('git symbolic-ref HEAD', { cwd: __dirname });
|
72 | gitBranch = path.basename(gitRefName.replace('\n', ''));
|
73 | }
|
74 | catch (e) {
|
75 | }
|
76 | ngCliVersion = `local (v${pkg.version}, branch: ${gitBranch})`;
|
77 | }
|
78 | if (projPkg) {
|
79 |
|
80 | angularCoreVersion = versions['@angular/core'];
|
81 | if (angularCoreVersion) {
|
82 | for (const angularPackage of Object.keys(versions)) {
|
83 | if (versions[angularPackage] == angularCoreVersion
|
84 | && angularPackage.startsWith('@angular/')) {
|
85 | angularSameAsCore.push(angularPackage.replace(/^@angular\//, ''));
|
86 | delete versions[angularPackage];
|
87 | }
|
88 | }
|
89 | }
|
90 | }
|
91 | const namePad = ' '.repeat(Object.keys(versions).sort((a, b) => b.length - a.length)[0].length + 3);
|
92 | const asciiArt = `
|
93 | _ _ ____ _ ___
|
94 | / \\ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
|
95 | / △ \\ | '_ \\ / _\` | | | | |/ _\` | '__| | | | | | |
|
96 | / ___ \\| | | | (_| | |_| | | (_| | | | |___| |___ | |
|
97 | /_/ \\_\\_| |_|\\__, |\\__,_|_|\\__,_|_| \\____|_____|___|
|
98 | |___/
|
99 | `.split('\n').map(x => core_1.terminal.red(x)).join('\n');
|
100 | this.logger.info(asciiArt);
|
101 | this.logger.info(`
|
102 | Angular CLI: ${ngCliVersion}
|
103 | Node: ${process.versions.node}
|
104 | OS: ${process.platform} ${process.arch}
|
105 | Angular: ${angularCoreVersion}
|
106 | ... ${angularSameAsCore.sort().reduce((acc, name) => {
|
107 | // Perform a simple word wrap around 60.
|
108 | if (acc.length == 0) {
|
109 | return [name];
|
110 | }
|
111 | const line = (acc[acc.length - 1] + ', ' + name);
|
112 | if (line.length > 60) {
|
113 | acc.push(name);
|
114 | }
|
115 | else {
|
116 | acc[acc.length - 1] = line;
|
117 | }
|
118 | return acc;
|
119 | }, []).join('\n... ')}
|
120 |
|
121 | Package${namePad.slice(7)}Version
|
122 | -------${namePad.replace(/ /g, '-')}------------------
|
123 | ${Object.keys(versions)
|
124 | .map(module => `${module}${namePad.slice(module.length)}${versions[module]}`)
|
125 | .sort()
|
126 | .join('\n')}
|
127 | `.replace(/^ {6}/gm, ''));
|
128 | }
|
129 | getVersion(moduleName, projectNodeModules, cliNodeModules) {
|
130 | try {
|
131 | if (projectNodeModules) {
|
132 | const modulePkg = require(path.resolve(projectNodeModules, moduleName, 'package.json'));
|
133 | return modulePkg.version;
|
134 | }
|
135 | }
|
136 | catch (_) {
|
137 | }
|
138 | try {
|
139 | if (cliNodeModules) {
|
140 | const modulePkg = require(path.resolve(cliNodeModules, moduleName, 'package.json'));
|
141 | return modulePkg.version + ' (cli-only)';
|
142 | }
|
143 | }
|
144 | catch (e) {
|
145 | }
|
146 | return '<error>';
|
147 | }
|
148 | }
|
149 | VersionCommand.aliases = ['v'];
|
150 | exports.default = VersionCommand;
|
151 |
|
\ | No newline at end of file |