UNPKG

6.91 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * @license
5 * Copyright Google Inc. All Rights Reserved.
6 *
7 * Use of this source code is governed by an MIT-style license that can be
8 * found in the LICENSE file at https://angular.io/license
9 */
10const core_1 = require("@angular-devkit/core");
11const child_process = require("child_process");
12const fs = require("fs");
13const path = require("path");
14const command_1 = require("../models/command");
15const color_1 = require("../utilities/color");
16const find_up_1 = require("../utilities/find-up");
17class VersionCommand extends command_1.Command {
18 async run() {
19 const pkg = require(path.resolve(__dirname, '..', 'package.json'));
20 let projPkg;
21 try {
22 projPkg = require(path.resolve(this.workspace.root, 'package.json'));
23 }
24 catch (_a) {
25 projPkg = undefined;
26 }
27 const patterns = [
28 /^@angular\/.*/,
29 /^@angular-devkit\/.*/,
30 /^@bazel\/.*/,
31 /^@ngtools\/.*/,
32 /^@nguniversal\/.*/,
33 /^@schematics\/.*/,
34 /^rxjs$/,
35 /^typescript$/,
36 /^ng-packagr$/,
37 /^webpack$/,
38 ];
39 const maybeNodeModules = find_up_1.findUp('node_modules', __dirname);
40 const packageRoot = projPkg
41 ? path.resolve(this.workspace.root, 'node_modules')
42 : maybeNodeModules;
43 const packageNames = [
44 ...Object.keys((pkg && pkg['dependencies']) || {}),
45 ...Object.keys((pkg && pkg['devDependencies']) || {}),
46 ...Object.keys((projPkg && projPkg['dependencies']) || {}),
47 ...Object.keys((projPkg && projPkg['devDependencies']) || {}),
48 ];
49 if (packageRoot != null) {
50 // Add all node_modules and node_modules/@*/*
51 const nodePackageNames = fs.readdirSync(packageRoot).reduce((acc, name) => {
52 if (name.startsWith('@')) {
53 return acc.concat(fs.readdirSync(path.resolve(packageRoot, name)).map(subName => name + '/' + subName));
54 }
55 else {
56 return acc.concat(name);
57 }
58 }, []);
59 packageNames.push(...nodePackageNames);
60 }
61 const versions = packageNames
62 .filter(x => patterns.some(p => p.test(x)))
63 .reduce((acc, name) => {
64 if (name in acc) {
65 return acc;
66 }
67 acc[name] = this.getVersion(name, packageRoot, maybeNodeModules);
68 return acc;
69 }, {});
70 let ngCliVersion = pkg.version;
71 if (!__dirname.match(/node_modules/)) {
72 let gitBranch = '??';
73 try {
74 const gitRefName = child_process.execSync('git rev-parse --abbrev-ref HEAD', {
75 cwd: __dirname,
76 encoding: 'utf8',
77 stdio: 'pipe',
78 });
79 gitBranch = gitRefName.replace('\n', '');
80 }
81 catch (_b) { }
82 ngCliVersion = `local (v${pkg.version}, branch: ${gitBranch})`;
83 }
84 let angularCoreVersion = '';
85 const angularSameAsCore = [];
86 if (projPkg) {
87 // Filter all angular versions that are the same as core.
88 angularCoreVersion = versions['@angular/core'];
89 if (angularCoreVersion) {
90 for (const angularPackage of Object.keys(versions)) {
91 if (versions[angularPackage] == angularCoreVersion &&
92 angularPackage.startsWith('@angular/')) {
93 angularSameAsCore.push(angularPackage.replace(/^@angular\//, ''));
94 delete versions[angularPackage];
95 }
96 }
97 // Make sure we list them in alphabetical order.
98 angularSameAsCore.sort();
99 }
100 }
101 const namePad = ' '.repeat(Object.keys(versions).sort((a, b) => b.length - a.length)[0].length + 3);
102 const asciiArt = `
103 _ _ ____ _ ___
104 / \\ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
105 / △ \\ | '_ \\ / _\` | | | | |/ _\` | '__| | | | | | |
106 / ___ \\| | | | (_| | |_| | | (_| | | | |___| |___ | |
107 /_/ \\_\\_| |_|\\__, |\\__,_|_|\\__,_|_| \\____|_____|___|
108 |___/
109 `
110 .split('\n')
111 .map(x => color_1.colors.red(x))
112 .join('\n');
113 this.logger.info(asciiArt);
114 this.logger.info(`
115 Angular CLI: ${ngCliVersion}
116 Node: ${process.versions.node}
117 OS: ${process.platform} ${process.arch}
118
119 Angular: ${angularCoreVersion}
120 ... ${angularSameAsCore
121 .reduce((acc, name) => {
122 // Perform a simple word wrap around 60.
123 if (acc.length == 0) {
124 return [name];
125 }
126 const line = acc[acc.length - 1] + ', ' + name;
127 if (line.length > 60) {
128 acc.push(name);
129 }
130 else {
131 acc[acc.length - 1] = line;
132 }
133 return acc;
134 }, [])
135 .join('\n... ')}
136 Ivy Workspace: ${projPkg ? this.getIvyWorkspace() : ''}
137
138 Package${namePad.slice(7)}Version
139 -------${namePad.replace(/ /g, '-')}------------------
140 ${Object.keys(versions)
141 .map(module => `${module}${namePad.slice(module.length)}${versions[module]}`)
142 .sort()
143 .join('\n')}
144 `.replace(/^ {6}/gm, ''));
145 }
146 getVersion(moduleName, projectNodeModules, cliNodeModules) {
147 try {
148 if (projectNodeModules) {
149 const modulePkg = require(path.resolve(projectNodeModules, moduleName, 'package.json'));
150 return modulePkg.version;
151 }
152 }
153 catch (_a) { }
154 try {
155 if (cliNodeModules) {
156 const modulePkg = require(path.resolve(cliNodeModules, moduleName, 'package.json'));
157 return modulePkg.version + ' (cli-only)';
158 }
159 }
160 catch (_b) { }
161 return '<error>';
162 }
163 getIvyWorkspace() {
164 try {
165 const content = fs.readFileSync(path.resolve(this.workspace.root, 'tsconfig.json'), 'utf-8');
166 const tsConfig = core_1.parseJson(content, core_1.JsonParseMode.Loose);
167 if (!core_1.isJsonObject(tsConfig)) {
168 return '<error>';
169 }
170 const { angularCompilerOptions } = tsConfig;
171 return core_1.isJsonObject(angularCompilerOptions) && angularCompilerOptions.enableIvy === false
172 ? 'No'
173 : 'Yes';
174 }
175 catch (_a) {
176 return '<error>';
177 }
178 }
179}
180exports.VersionCommand = VersionCommand;
181VersionCommand.aliases = ['v'];