Skip to content

Commit 8b47a90

Browse files
authored
feat(version): compare local and global version and warn users. (#3693)
1 parent bb748f2 commit 8b47a90

File tree

4 files changed

+64
-17
lines changed

4 files changed

+64
-17
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
"rxjs": "^5.0.1",
104104
"sass-loader": "^4.0.1",
105105
"script-loader": "^0.7.0",
106-
"semver": "^5.1.0",
106+
"semver": "^5.3.0",
107107
"silent-error": "^1.0.0",
108108
"source-map": "^0.5.6",
109109
"source-map-loader": "^0.1.5",
@@ -145,6 +145,7 @@
145145
"@types/node": "^6.0.36",
146146
"@types/request": "0.0.30",
147147
"@types/rimraf": "0.0.25-alpha",
148+
"@types/semver": "^5.3.30",
148149
"@types/source-map": "^0.5.0",
149150
"@types/webpack": "^1.12.22-alpha",
150151
"chai": "^3.5.0",

packages/angular-cli/bin/ng

+47-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@ process.title = 'angular-cli';
77
const resolve = require('resolve');
88
const packageJson = require('../package.json');
99
const Version = require('../upgrade/version').Version;
10+
const yellow = require('chalk').yellow;
11+
const SemVer = require('semver').SemVer;
12+
const fs = require('fs');
13+
const path = require('path');
14+
15+
16+
function _fromPackageJson(cwd) {
17+
cwd = cwd || process.cwd();
18+
19+
do {
20+
const packageJsonPath = path.join(cwd, 'node_modules/angular-cli/package.json');
21+
if (fs.existsSync(packageJsonPath)) {
22+
const content = fs.readFileSync(packageJsonPath, 'utf-8');
23+
if (content) {
24+
const json = JSON.parse(content);
25+
if (json['version']) {
26+
return new SemVer(json['version']);
27+
}
28+
}
29+
}
30+
31+
// Check the parent.
32+
cwd = path.dirname(cwd);
33+
} while (cwd != path.dirname(cwd));
34+
35+
return null;
36+
}
1037

1138

1239
resolve('angular-cli', { basedir: process.cwd() },
@@ -22,6 +49,25 @@ resolve('angular-cli', { basedir: process.cwd() },
2249
// Verify that package's version.
2350
Version.assertPostWebpackVersion();
2451

52+
// This was run from a global, check local version.
53+
const globalVersion = new SemVer(packageJson['version']);
54+
let localVersion;
55+
let shouldWarn = false;
56+
57+
try {
58+
localVersion = _fromPackageJson();
59+
shouldWarn = localVersion && globalVersion.compare(localVersion) < 0;
60+
} catch (e) {
61+
console.error(e);
62+
shouldWarn = true;
63+
}
64+
65+
if (shouldWarn) {
66+
// eslint-disable no-console
67+
console.log(yellow(`Your global Angular CLI version (${globalVersion}) is greater than `
68+
+ `your local version (${localVersion}). The local Angular CLI version is used.`));
69+
}
70+
2571
// No error implies a projectLocalCli, which will load whatever
2672
// version of ng-cli you have installed in a local package.json
2773
cli = require(projectLocalCli);
@@ -36,7 +82,6 @@ resolve('angular-cli', { basedir: process.cwd() },
3682
inputStream: process.stdin,
3783
outputStream: process.stdout
3884
}).then(function (result) {
39-
var exitCode = typeof result === 'object' ? result.exitCode : result;
40-
process.exit(exitCode);
85+
process.exit(typeof result === 'object' ? result.exitCode : result);
4186
});
4287
});

packages/angular-cli/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"paths": {
2323
"@angular-cli/ast-tools": [ "../../dist/@angular-cli/ast-tools/src" ],
2424
"@angular-cli/base-href-webpack": [ "../../dist/@angular-cli/base-href-webpack/src" ],
25+
"@angular-cli/version": [ "../../dist/@angular-cli/version/src" ],
2526
"@ngtools/webpack": [ "../../dist/@ngtools/webpack/src" ]
2627
}
2728
},

packages/angular-cli/upgrade/version.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import {CliConfig} from '../models/config';
2-
import {readFileSync, existsSync} from 'fs';
3-
import {stripIndents} from 'common-tags';
1+
import {SemVer} from 'semver';
42
import {bold, red, yellow} from 'chalk';
3+
import {stripIndents} from 'common-tags';
4+
import {readFileSync, existsSync} from 'fs';
55
import * as path from 'path';
6+
7+
import {CliConfig} from '../models/config';
8+
69
const resolve = require('resolve');
710

811

@@ -32,12 +35,9 @@ function _hasOldCliBuildFile() {
3235

3336

3437
export class Version {
35-
constructor(private _version: string = null) {}
36-
37-
private _parse() {
38-
return this.isKnown()
39-
? this._version.match(/^(\d+)\.(\d+)(?:\.(\d+))?(?:-(alpha|beta|rc)\.(.*))?$/).slice(1)
40-
: [];
38+
private _semver: SemVer = null;
39+
constructor(private _version: string = null) {
40+
this._semver = _version && new SemVer(_version);
4141
}
4242

4343
isAlpha() { return this.qualifier == 'alpha'; }
@@ -47,11 +47,11 @@ export class Version {
4747

4848
isLocal() { return this.isKnown() && path.isAbsolute(this._version); }
4949

50-
get major() { return this._parse()[0] || 0; }
51-
get minor() { return this._parse()[1] || 0; }
52-
get patch() { return this._parse()[2] || 0; }
53-
get qualifier() { return this._parse()[3] || ''; }
54-
get extra() { return this._parse()[4] || ''; }
50+
get major() { return this._semver ? this._semver.major : 0; }
51+
get minor() { return this._semver ? this._semver.minor : 0; }
52+
get patch() { return this._semver ? this._semver.patch : 0; }
53+
get qualifier() { return this._semver ? this._semver.prerelease[0] : ''; }
54+
get extra() { return this._semver ? this._semver.prerelease[1] : ''; }
5555

5656
toString() { return this._version; }
5757

0 commit comments

Comments
 (0)