forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathversion.ts
174 lines (144 loc) · 5.39 KB
/
version.ts
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
import {SemVer} from 'semver';
import {bold, red, yellow} from 'chalk';
import {stripIndents} from 'common-tags';
import {readFileSync, existsSync} from 'fs';
import * as path from 'path';
import {CliConfig} from '../models/config';
const resolve = require('resolve');
function _findUp(name: string, from: string) {
let currentDir = from;
while (currentDir && currentDir !== path.parse(currentDir).root) {
const p = path.join(currentDir, name);
if (existsSync(p)) {
return p;
}
currentDir = path.dirname(currentDir);
}
return null;
}
function _hasOldCliBuildFile() {
return existsSync(_findUp('angular-cli-build.js', process.cwd()))
|| existsSync(_findUp('angular-cli-build.ts', process.cwd()))
|| existsSync(_findUp('ember-cli-build.js', process.cwd()))
|| existsSync(_findUp('angular-cli-build.js', __dirname))
|| existsSync(_findUp('angular-cli-build.ts', __dirname))
|| existsSync(_findUp('ember-cli-build.js', __dirname));
}
export class Version {
private _semver: SemVer = null;
constructor(private _version: string = null) {
this._semver = _version && new SemVer(_version);
}
isAlpha() { return this.qualifier == 'alpha'; }
isBeta() { return this.qualifier == 'beta'; }
isReleaseCandidate() { return this.qualifier == 'rc'; }
isKnown() { return this._version !== null; }
isLocal() { return this.isKnown() && path.isAbsolute(this._version); }
isGreaterThanOrEqualTo(other: SemVer) {
return this._semver.compare(other) >= 0;
}
get major() { return this._semver ? this._semver.major : 0; }
get minor() { return this._semver ? this._semver.minor : 0; }
get patch() { return this._semver ? this._semver.patch : 0; }
get qualifier() { return this._semver ? this._semver.prerelease[0] : ''; }
get extra() { return this._semver ? this._semver.prerelease[1] : ''; }
toString() { return this._version; }
static fromProject(): Version {
let packageJson: any = null;
try {
const angularCliPath = resolve.sync('@angular/cli', {
basedir: process.cwd(),
packageFilter: (pkg: any, pkgFile: string) => {
packageJson = pkg;
}
});
if (angularCliPath && packageJson) {
try {
return new Version(packageJson.version);
} catch (e) {
return new Version(null);
}
}
} catch (e) {
// Fallback to reading config.
}
const configPath = CliConfig.configFilePath();
if (configPath === null) {
return new Version(null);
}
const configJson = readFileSync(configPath, 'utf8');
try {
const json = JSON.parse(configJson);
return new Version(json.project && json.project.version);
} catch (e) {
return new Version(null);
}
}
static assertAngularVersionIs2_3_1OrHigher(projectRoot: string) {
const angularCorePath = path.join(projectRoot, 'node_modules/@angular/core');
const pkgJson = existsSync(angularCorePath)
? JSON.parse(readFileSync(path.join(angularCorePath, 'package.json'), 'utf8'))
: null;
// Just check @angular/core.
if (pkgJson && pkgJson['version']) {
const v = new Version(pkgJson['version']);
if (v.isLocal()) {
console.warn(yellow('Using a local version of angular. Proceeding with care...'));
} else {
if (!v.isGreaterThanOrEqualTo(new SemVer('2.3.1'))) {
console.error(bold(red(stripIndents`
This version of CLI is only compatible with angular version 2.3.1 or better. Please
upgrade your angular version, e.g. by running:
npm install @angular/core@latest
` + '\n')));
process.exit(3);
}
}
} else {
console.error(bold(red(stripIndents`
You seem to not be dependending on "@angular/core". This is an error.
`)));
process.exit(2);
}
}
static assertPostWebpackVersion() {
if (this.isPreWebpack()) {
console.error(bold(red('\n' + stripIndents`
It seems like you're using a project generated using an old version of the Angular CLI.
The latest CLI now uses webpack and has a lot of improvements including a simpler
workflow, a faster build, and smaller bundles.
To get more info, including a step-by-step guide to upgrade the CLI, follow this link:
https://github.com/angular/angular-cli/wiki/Upgrading-from-Beta.10-to-Beta.14
` + '\n')));
process.exit(1);
} else {
// Verify that there's no build file.
if (_hasOldCliBuildFile()) {
console.error(bold(yellow('\n' + stripIndents`
It seems like you're using the newest version of the Angular CLI that uses webpack.
This version does not require an angular-cli-build file, but your project has one.
It will be ignored.
` + '\n')));
}
}
}
static isPreWebpack(): boolean {
// CliConfig is a bit stricter with the schema, so we need to be a little looser with it.
const version = Version.fromProject();
if (version && version.isKnown()) {
if (version.major == 0) {
return true;
} else if (version.minor != 0) {
return false;
} else if (version.isBeta() && !version.toString().match(/webpack/)) {
const betaVersion = version.extra;
if (parseInt(betaVersion) < 12) {
return true;
}
}
} else {
return _hasOldCliBuildFile();
}
return false;
}
}