Skip to content

Commit

Permalink
feat(version): compare local and global version and warn users. (#3693)
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl authored Dec 28, 2016
1 parent bb748f2 commit 8b47a90
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"rxjs": "^5.0.1",
"sass-loader": "^4.0.1",
"script-loader": "^0.7.0",
"semver": "^5.1.0",
"semver": "^5.3.0",
"silent-error": "^1.0.0",
"source-map": "^0.5.6",
"source-map-loader": "^0.1.5",
Expand Down Expand Up @@ -145,6 +145,7 @@
"@types/node": "^6.0.36",
"@types/request": "0.0.30",
"@types/rimraf": "0.0.25-alpha",
"@types/semver": "^5.3.30",
"@types/source-map": "^0.5.0",
"@types/webpack": "^1.12.22-alpha",
"chai": "^3.5.0",
Expand Down
49 changes: 47 additions & 2 deletions packages/angular-cli/bin/ng
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@ process.title = 'angular-cli';
const resolve = require('resolve');
const packageJson = require('../package.json');
const Version = require('../upgrade/version').Version;
const yellow = require('chalk').yellow;
const SemVer = require('semver').SemVer;
const fs = require('fs');
const path = require('path');


function _fromPackageJson(cwd) {
cwd = cwd || process.cwd();

do {
const packageJsonPath = path.join(cwd, 'node_modules/angular-cli/package.json');
if (fs.existsSync(packageJsonPath)) {
const content = fs.readFileSync(packageJsonPath, 'utf-8');
if (content) {
const json = JSON.parse(content);
if (json['version']) {
return new SemVer(json['version']);
}
}
}

// Check the parent.
cwd = path.dirname(cwd);
} while (cwd != path.dirname(cwd));

return null;
}


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

// This was run from a global, check local version.
const globalVersion = new SemVer(packageJson['version']);
let localVersion;
let shouldWarn = false;

try {
localVersion = _fromPackageJson();
shouldWarn = localVersion && globalVersion.compare(localVersion) < 0;
} catch (e) {
console.error(e);
shouldWarn = true;
}

if (shouldWarn) {
// eslint-disable no-console
console.log(yellow(`Your global Angular CLI version (${globalVersion}) is greater than `
+ `your local version (${localVersion}). The local Angular CLI version is used.`));
}

// No error implies a projectLocalCli, which will load whatever
// version of ng-cli you have installed in a local package.json
cli = require(projectLocalCli);
Expand All @@ -36,7 +82,6 @@ resolve('angular-cli', { basedir: process.cwd() },
inputStream: process.stdin,
outputStream: process.stdout
}).then(function (result) {
var exitCode = typeof result === 'object' ? result.exitCode : result;
process.exit(exitCode);
process.exit(typeof result === 'object' ? result.exitCode : result);
});
});
1 change: 1 addition & 0 deletions packages/angular-cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"paths": {
"@angular-cli/ast-tools": [ "../../dist/@angular-cli/ast-tools/src" ],
"@angular-cli/base-href-webpack": [ "../../dist/@angular-cli/base-href-webpack/src" ],
"@angular-cli/version": [ "../../dist/@angular-cli/version/src" ],
"@ngtools/webpack": [ "../../dist/@ngtools/webpack/src" ]
}
},
Expand Down
28 changes: 14 additions & 14 deletions packages/angular-cli/upgrade/version.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {CliConfig} from '../models/config';
import {readFileSync, existsSync} from 'fs';
import {stripIndents} from 'common-tags';
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');


Expand Down Expand Up @@ -32,12 +35,9 @@ function _hasOldCliBuildFile() {


export class Version {
constructor(private _version: string = null) {}

private _parse() {
return this.isKnown()
? this._version.match(/^(\d+)\.(\d+)(?:\.(\d+))?(?:-(alpha|beta|rc)\.(.*))?$/).slice(1)
: [];
private _semver: SemVer = null;
constructor(private _version: string = null) {
this._semver = _version && new SemVer(_version);
}

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

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

get major() { return this._parse()[0] || 0; }
get minor() { return this._parse()[1] || 0; }
get patch() { return this._parse()[2] || 0; }
get qualifier() { return this._parse()[3] || ''; }
get extra() { return this._parse()[4] || ''; }
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; }

Expand Down

0 comments on commit 8b47a90

Please sign in to comment.