UNPKG

8.48 kBJavaScriptView Raw
1"use strict";
2var semver_1 = require('semver');
3var chalk_1 = require('chalk');
4var common_tags_1 = require('common-tags');
5var fs_1 = require('fs');
6var path = require('path');
7var config_1 = require('../models/config');
8var resolve = require('resolve');
9function _findUp(name, from) {
10 var currentDir = from;
11 while (currentDir && currentDir !== path.parse(currentDir).root) {
12 var p = path.join(currentDir, name);
13 if (fs_1.existsSync(p)) {
14 return p;
15 }
16 currentDir = path.dirname(currentDir);
17 }
18 return null;
19}
20function _hasOldCliBuildFile() {
21 return fs_1.existsSync(_findUp('angular-cli-build.js', process.cwd()))
22 || fs_1.existsSync(_findUp('angular-cli-build.ts', process.cwd()))
23 || fs_1.existsSync(_findUp('ember-cli-build.js', process.cwd()))
24 || fs_1.existsSync(_findUp('angular-cli-build.js', __dirname))
25 || fs_1.existsSync(_findUp('angular-cli-build.ts', __dirname))
26 || fs_1.existsSync(_findUp('ember-cli-build.js', __dirname));
27}
28var Version = (function () {
29 function Version(_version) {
30 if (_version === void 0) { _version = null; }
31 this._version = _version;
32 this._semver = null;
33 this._semver = _version && new semver_1.SemVer(_version);
34 }
35 Version.prototype.isAlpha = function () { return this.qualifier == 'alpha'; };
36 Version.prototype.isBeta = function () { return this.qualifier == 'beta'; };
37 Version.prototype.isReleaseCandidate = function () { return this.qualifier == 'rc'; };
38 Version.prototype.isKnown = function () { return this._version !== null; };
39 Version.prototype.isLocal = function () { return this.isKnown() && path.isAbsolute(this._version); };
40 Version.prototype.isGreaterThanOrEqualTo = function (other) {
41 return this._semver.compare(other) >= 0;
42 };
43 Object.defineProperty(Version.prototype, "major", {
44 get: function () { return this._semver ? this._semver.major : 0; },
45 enumerable: true,
46 configurable: true
47 });
48 Object.defineProperty(Version.prototype, "minor", {
49 get: function () { return this._semver ? this._semver.minor : 0; },
50 enumerable: true,
51 configurable: true
52 });
53 Object.defineProperty(Version.prototype, "patch", {
54 get: function () { return this._semver ? this._semver.patch : 0; },
55 enumerable: true,
56 configurable: true
57 });
58 Object.defineProperty(Version.prototype, "qualifier", {
59 get: function () { return this._semver ? this._semver.prerelease[0] : ''; },
60 enumerable: true,
61 configurable: true
62 });
63 Object.defineProperty(Version.prototype, "extra", {
64 get: function () { return this._semver ? this._semver.prerelease[1] : ''; },
65 enumerable: true,
66 configurable: true
67 });
68 Version.prototype.toString = function () { return this._version; };
69 Version.fromProject = function () {
70 var packageJson = null;
71 try {
72 var angularCliPath = resolve.sync('angular-cli', {
73 basedir: process.cwd(),
74 packageFilter: function (pkg, pkgFile) {
75 packageJson = pkg;
76 }
77 });
78 if (angularCliPath && packageJson) {
79 try {
80 return new Version(packageJson.version);
81 }
82 catch (e) {
83 return new Version(null);
84 }
85 }
86 }
87 catch (e) {
88 }
89 var configPath = config_1.CliConfig.configFilePath();
90 if (configPath === null) {
91 return new Version(null);
92 }
93 var configJson = fs_1.readFileSync(configPath, 'utf8');
94 try {
95 var json = JSON.parse(configJson);
96 return new Version(json.project && json.project.version);
97 }
98 catch (e) {
99 return new Version(null);
100 }
101 };
102 Version.assertAngularVersionIs2_3_1OrHigher = function (projectRoot) {
103 var angularCorePath = path.join(projectRoot, 'node_modules/@angular/core');
104 var pkgJson = fs_1.existsSync(angularCorePath)
105 ? JSON.parse(fs_1.readFileSync(path.join(angularCorePath, 'package.json'), 'utf8'))
106 : null;
107 // Just check @angular/core.
108 if (pkgJson && pkgJson['version']) {
109 var v = new Version(pkgJson['version']);
110 if (v.isLocal()) {
111 console.warn(chalk_1.yellow('Using a local version of angular. Proceeding with care...'));
112 }
113 else {
114 if (!v.isGreaterThanOrEqualTo(new semver_1.SemVer('2.3.1'))) {
115 console.error(chalk_1.bold(chalk_1.red((_a = ["\n This version of CLI is only compatible with angular version 2.3.1 or better. Please\n upgrade your angular version, e.g. by running:\n \n npm install @angular/core@latest\n "], _a.raw = ["\n This version of CLI is only compatible with angular version 2.3.1 or better. Please\n upgrade your angular version, e.g. by running:\n \n npm install @angular/core@latest\n "], common_tags_1.stripIndents(_a)) + '\n')));
116 process.exit(3);
117 }
118 }
119 }
120 else {
121 console.error(chalk_1.bold(chalk_1.red((_b = ["\n You seem to not be dependending on \"@angular/core\". This is an error.\n "], _b.raw = ["\n You seem to not be dependending on \"@angular/core\". This is an error.\n "], common_tags_1.stripIndents(_b)))));
122 process.exit(2);
123 }
124 var _a, _b;
125 };
126 Version.assertPostWebpackVersion = function () {
127 if (this.isPreWebpack()) {
128 console.error(chalk_1.bold(chalk_1.red('\n' + (_a = ["\n It seems like you're using a project generated using an old version of the Angular CLI.\n The latest CLI now uses webpack and has a lot of improvements including a simpler\n workflow, a faster build, and smaller bundles.\n\n To get more info, including a step-by-step guide to upgrade the CLI, follow this link:\n https://github.com/angular/angular-cli/wiki/Upgrading-from-Beta.10-to-Beta.14\n "], _a.raw = ["\n It seems like you're using a project generated using an old version of the Angular CLI.\n The latest CLI now uses webpack and has a lot of improvements including a simpler\n workflow, a faster build, and smaller bundles.\n\n To get more info, including a step-by-step guide to upgrade the CLI, follow this link:\n https://github.com/angular/angular-cli/wiki/Upgrading-from-Beta.10-to-Beta.14\n "], common_tags_1.stripIndents(_a)) + '\n')));
129 process.exit(1);
130 }
131 else {
132 // Verify that there's no build file.
133 if (_hasOldCliBuildFile()) {
134 console.error(chalk_1.bold(chalk_1.yellow('\n' + (_b = ["\n It seems like you're using the newest version of the Angular CLI that uses webpack.\n This version does not require an angular-cli-build file, but your project has one.\n It will be ignored.\n "], _b.raw = ["\n It seems like you're using the newest version of the Angular CLI that uses webpack.\n This version does not require an angular-cli-build file, but your project has one.\n It will be ignored.\n "], common_tags_1.stripIndents(_b)) + '\n')));
135 }
136 }
137 var _a, _b;
138 };
139 Version.isPreWebpack = function () {
140 // CliConfig is a bit stricter with the schema, so we need to be a little looser with it.
141 var version = Version.fromProject();
142 if (version && version.isKnown()) {
143 if (version.major == 0) {
144 return true;
145 }
146 else if (version.minor != 0) {
147 return false;
148 }
149 else if (version.isBeta() && !version.toString().match(/webpack/)) {
150 var betaVersion = version.extra;
151 if (parseInt(betaVersion) < 12) {
152 return true;
153 }
154 }
155 }
156 else {
157 return _hasOldCliBuildFile();
158 }
159 return false;
160 };
161 return Version;
162}());
163exports.Version = Version;
164//# sourceMappingURL=/Users/hansl/Sources/angular-cli/packages/@angular/cli/upgrade/version.js.map
\No newline at end of file