From 566fbc5a6df3b0028eb5888804a5e19d92f14e0e Mon Sep 17 00:00:00 2001 From: Hans Larsen Date: Wed, 21 Dec 2016 12:27:09 -0800 Subject: [PATCH] chore(version): compare local and global version and warn users. Added a new @angular-cli/version package for an encapsulation of SemVer. --- package.json | 3 +- packages/angular-cli/bin/ng | 49 ++++++++++++++++++++++++- packages/angular-cli/tsconfig.json | 1 + packages/angular-cli/upgrade/version.ts | 28 +++++++------- 4 files changed, 64 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 17cda0eb34e8..16e4e3ad6017 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/packages/angular-cli/bin/ng b/packages/angular-cli/bin/ng index 4001df144db1..6e9050da49fb 100755 --- a/packages/angular-cli/bin/ng +++ b/packages/angular-cli/bin/ng @@ -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() }, @@ -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); @@ -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); }); }); diff --git a/packages/angular-cli/tsconfig.json b/packages/angular-cli/tsconfig.json index f771c2461b8d..818030de11d5 100644 --- a/packages/angular-cli/tsconfig.json +++ b/packages/angular-cli/tsconfig.json @@ -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" ] } }, diff --git a/packages/angular-cli/upgrade/version.ts b/packages/angular-cli/upgrade/version.ts index 15decc70923f..39117b7ba0b6 100644 --- a/packages/angular-cli/upgrade/version.ts +++ b/packages/angular-cli/upgrade/version.ts @@ -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'); @@ -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'; } @@ -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; }