forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.ts
More file actions
173 lines (150 loc) · 5.25 KB
/
version.ts
File metadata and controls
173 lines (150 loc) · 5.25 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
// tslint:disable:no-global-tslint-disable file-header
import { terminal } from '@angular-devkit/core';
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { Command, Option } from '../models/command';
import { findUp } from '../utilities/find-up';
export default class VersionCommand extends Command {
public readonly name = 'version';
public readonly description = 'Outputs Angular CLI version.';
public static aliases = ['v'];
public readonly arguments: string[] = [];
public readonly options: Option[] = [];
public run() {
let angularCoreVersion = '';
const angularSameAsCore: string[] = [];
const pkg = require(path.resolve(__dirname, '..', 'package.json'));
let projPkg;
try {
projPkg = require(path.resolve(this.project.root, 'package.json'));
} catch (exception) {
projPkg = undefined;
}
const patterns = [
/^@angular\/.*/,
/^@angular-devkit\/.*/,
/^@ngtools\/.*/,
/^@schematics\/.*/,
/^rxjs$/,
/^typescript$/,
/^ng-packagr$/,
/^webpack$/,
];
const maybeNodeModules = findUp('node_modules', __dirname);
const packageRoot = projPkg
? path.resolve(this.project.root, 'node_modules')
: maybeNodeModules;
const packageNames = [
...Object.keys(pkg && pkg['dependencies'] || {}),
...Object.keys(pkg && pkg['devDependencies'] || {}),
...Object.keys(projPkg && projPkg['dependencies'] || {}),
...Object.keys(projPkg && projPkg['devDependencies'] || {}),
];
if (packageRoot != null) {
// Add all node_modules and node_modules/@*/*
const nodePackageNames = fs.readdirSync(packageRoot)
.reduce<string[]>((acc, name) => {
if (name.startsWith('@')) {
return acc.concat(
fs.readdirSync(path.resolve(packageRoot, name))
.map(subName => name + '/' + subName),
);
} else {
return acc.concat(name);
}
}, []);
packageNames.push(...nodePackageNames);
}
const versions = packageNames
.filter(x => patterns.some(p => p.test(x)))
.reduce((acc, name) => {
if (name in acc) {
return acc;
}
acc[name] = this.getVersion(name, packageRoot, maybeNodeModules);
return acc;
}, {} as { [module: string]: string });
let ngCliVersion = pkg.version;
if (!__dirname.match(/node_modules/)) {
let gitBranch = '??';
try {
const gitRefName = '' + child_process.execSync('git symbolic-ref HEAD', {cwd: __dirname});
gitBranch = path.basename(gitRefName.replace('\n', ''));
} catch (e) {
}
ngCliVersion = `local (v${pkg.version}, branch: ${gitBranch})`;
}
if (projPkg) {
// Filter all angular versions that are the same as core.
angularCoreVersion = versions['@angular/core'];
if (angularCoreVersion) {
for (const angularPackage of Object.keys(versions)) {
if (versions[angularPackage] == angularCoreVersion
&& angularPackage.startsWith('@angular/')) {
angularSameAsCore.push(angularPackage.replace(/^@angular\//, ''));
delete versions[angularPackage];
}
}
}
}
const namePad = ' '.repeat(
Object.keys(versions).sort((a, b) => b.length - a.length)[0].length + 3,
);
const asciiArt = `
_ _ ____ _ ___
/ \\ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \\ | '_ \\ / _\` | | | | |/ _\` | '__| | | | | | |
/ ___ \\| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \\_\\_| |_|\\__, |\\__,_|_|\\__,_|_| \\____|_____|___|
|___/
`.split('\n').map(x => terminal.red(x)).join('\n');
this.logger.info(asciiArt);
this.logger.info(`
Angular CLI: ${ngCliVersion}
Node: ${process.versions.node}
OS: ${process.platform} ${process.arch}
Angular: ${angularCoreVersion}
... ${angularSameAsCore.sort().reduce<string[]>((acc, name) => {
// Perform a simple word wrap around 60.
if (acc.length == 0) {
return [name];
}
const line = (acc[acc.length - 1] + ', ' + name);
if (line.length > 60) {
acc.push(name);
} else {
acc[acc.length - 1] = line;
}
return acc;
}, []).join('\n... ')}
Package${namePad.slice(7)}Version
-------${namePad.replace(/ /g, '-')}------------------
${Object.keys(versions)
.map(module => `${module}${namePad.slice(module.length)}${versions[module]}`)
.sort()
.join('\n')}
`.replace(/^ {6}/gm, ''));
}
private getVersion(
moduleName: string,
projectNodeModules: string | null,
cliNodeModules: string | null,
): string {
try {
if (projectNodeModules) {
const modulePkg = require(path.resolve(projectNodeModules, moduleName, 'package.json'));
return modulePkg.version;
}
} catch (_) {
}
try {
if (cliNodeModules) {
const modulePkg = require(path.resolve(cliNodeModules, moduleName, 'package.json'));
return modulePkg.version + ' (cli-only)';
}
} catch (e) {
}
return '<error>';
}
}