Skip to content

Commit 83ef4af

Browse files
clydinhansl
authored andcommitted
feat(@angular/cli): make node minimum check a fatal error
1 parent 1a01b1e commit 83ef4af

File tree

2 files changed

+148
-140
lines changed

2 files changed

+148
-140
lines changed

packages/@angular/cli/bin/ng

Lines changed: 9 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -5,148 +5,17 @@
55
// Due to an obscure Mac bug, do not start this title with any symbol.
66
process.title = 'ng';
77

8-
const isWarningEnabled = require('../utilities/config').isWarningEnabled;
9-
const Version = require('../upgrade/version').Version;
8+
var red = require('chalk').red;
109

11-
const fs = require('fs');
12-
const findUp = require('../utilities/find-up').findUp;
13-
const packageJson = require('../package.json');
14-
const path = require('path');
15-
const resolve = require('resolve');
16-
const stripIndents = require('common-tags').stripIndents;
17-
const yellow = require('chalk').yellow;
18-
const SemVer = require('semver').SemVer;
19-
const semver = require('semver');
20-
const events = require('events');
21-
22-
23-
function _fromPackageJson(cwd) {
24-
cwd = cwd || process.cwd();
25-
26-
do {
27-
const packageJsonPath = path.join(cwd, 'node_modules/@angular/cli/package.json');
28-
if (fs.existsSync(packageJsonPath)) {
29-
const content = fs.readFileSync(packageJsonPath, 'utf-8');
30-
if (content) {
31-
const json = JSON.parse(content);
32-
if (json['version']) {
33-
return new SemVer(json['version']);
34-
}
35-
}
36-
}
37-
38-
// Check the parent.
39-
cwd = path.dirname(cwd);
40-
} while (cwd != path.dirname(cwd));
41-
42-
return null;
43-
}
44-
45-
46-
// Check if we need to profile this CLI run.
47-
let profiler = null;
48-
if (process.env['NG_CLI_PROFILING']) {
49-
profiler = require('v8-profiler');
50-
profiler.startProfiling();
51-
function exitHandler(options, err) {
52-
if (options.cleanup) {
53-
const cpuProfile = profiler.stopProfiling();
54-
fs.writeFileSync(path.resolve(process.cwd(), process.env.NG_CLI_PROFILING) + '.cpuprofile',
55-
JSON.stringify(cpuProfile));
56-
}
57-
58-
if (options.exit) {
59-
process.exit();
60-
}
61-
}
62-
63-
process.on('exit', exitHandler.bind(null, { cleanup: true }));
64-
process.on('SIGINT', exitHandler.bind(null, { exit: true }));
65-
process.on('uncaughtException', exitHandler.bind(null, { exit: true }));
66-
}
67-
68-
69-
// Show the warnings/errors due to package and version deprecation.
70-
const version = new SemVer(process.version);
71-
if (version.compare(new SemVer('8.9.0')) < 0 && isWarningEnabled('nodeDeprecation')) {
72-
process.stderr.write(yellow(stripIndents`
73-
You are running version ${version.version} of Node, which is not supported by Angular CLI v6.
74-
The official Node version that is supported is 8.9 and greater.
75-
76-
Please visit https://nodejs.org/en/ to find instructions on how to update Node.
77-
`));
10+
var version = process.version.substr(1).split('.');
11+
if (Number(version[0]) < 8 || (Number(version[0]) === 8 && Number(version[1]) < 9)) {
12+
process.stderr.write(red(
13+
'You are running version ' + process.version + ' of Node.js, which is not supported by Angular CLI v6.\n' +
14+
'The official Node.js version that is supported is 8.9 and greater.\n\n' +
15+
'Please visit https://nodejs.org/en/ to find instructions on how to update Node.js.\n'
16+
));
7817

7918
process.exit(3);
8019
}
8120

82-
resolve('@angular/cli', { basedir: process.cwd() },
83-
function (error, projectLocalCli) {
84-
var cli;
85-
if (error) {
86-
// If there is an error, resolve could not find the ng-cli
87-
// library from a package.json. Instead, include it from a relative
88-
// path to this script file (which is likely a globally installed
89-
// npm package). Most common cause for hitting this is `ng new`
90-
cli = require('../lib/cli');
91-
} else {
92-
// This was run from a global, check local version.
93-
const globalVersion = new SemVer(packageJson['version']);
94-
let localVersion;
95-
let shouldWarn = false;
96-
97-
try {
98-
localVersion = _fromPackageJson();
99-
shouldWarn = localVersion && globalVersion.compare(localVersion) > 0;
100-
} catch (e) {
101-
// eslint-disable-next-line no-console
102-
console.error(e);
103-
shouldWarn = true;
104-
}
105-
106-
if (shouldWarn && isWarningEnabled('versionMismatch')) {
107-
let warning = yellow(stripIndents`
108-
Your global Angular CLI version (${globalVersion}) is greater than your local
109-
version (${localVersion}). The local Angular CLI version is used.
110-
111-
To disable this warning use "ng set --global warnings.versionMismatch=false".
112-
`);
113-
// Don't show warning colorised on `ng completion`
114-
if (process.argv[2] !== 'completion') {
115-
// eslint-disable-next-line no-console
116-
console.log(warning);
117-
} else {
118-
// eslint-disable-next-line no-console
119-
console.error(warning);
120-
process.exit(1);
121-
}
122-
}
123-
124-
// No error implies a projectLocalCli, which will load whatever
125-
// version of ng-cli you have installed in a local package.json
126-
cli = require(projectLocalCli);
127-
}
128-
129-
if ('default' in cli) {
130-
cli = cli['default'];
131-
}
132-
133-
let standardInput;
134-
try {
135-
standardInput = process.stdin;
136-
} catch (e) {
137-
delete process.stdin;
138-
process.stdin = new events.EventEmitter();
139-
standardInput = process.stdin;
140-
}
141-
142-
cli({
143-
cliArgs: process.argv.slice(2),
144-
inputStream: standardInput,
145-
outputStream: process.stdout
146-
}).then(function (exitCode) {
147-
process.exit(exitCode);
148-
}).catch(function(err) {
149-
console.log('Unknown error: ' + err.toString());
150-
process.exit(127);
151-
});
152-
});
21+
require('../lib/init');

packages/@angular/cli/lib/init.ts

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
// @ignoreDep v8-profiler
9+
const isWarningEnabled = require('../utilities/config').isWarningEnabled;
10+
const Version = require('../upgrade/version').Version;
11+
12+
const fs = require('fs');
13+
const findUp = require('../utilities/find-up').findUp;
14+
const packageJson = require('../package.json');
15+
const path = require('path');
16+
const resolve = require('resolve');
17+
const stripIndents = require('common-tags').stripIndents;
18+
const yellow = require('chalk').yellow;
19+
const SemVer = require('semver').SemVer;
20+
const semver = require('semver');
21+
const events = require('events');
22+
23+
24+
function _fromPackageJson(cwd?: string) {
25+
cwd = cwd || process.cwd();
26+
27+
do {
28+
const packageJsonPath = path.join(cwd, 'node_modules/@angular/cli/package.json');
29+
if (fs.existsSync(packageJsonPath)) {
30+
const content = fs.readFileSync(packageJsonPath, 'utf-8');
31+
if (content) {
32+
const json = JSON.parse(content);
33+
if (json['version']) {
34+
return new SemVer(json['version']);
35+
}
36+
}
37+
}
38+
39+
// Check the parent.
40+
cwd = path.dirname(cwd);
41+
} while (cwd != path.dirname(cwd));
42+
43+
return null;
44+
}
45+
46+
47+
// Check if we need to profile this CLI run.
48+
if (process.env['NG_CLI_PROFILING']) {
49+
const profiler = require('v8-profiler');
50+
profiler.startProfiling();
51+
function exitHandler(options: any, _err: Error) {
52+
if (options.cleanup) {
53+
const cpuProfile = profiler.stopProfiling();
54+
fs.writeFileSync(path.resolve(process.cwd(), process.env.NG_CLI_PROFILING) + '.cpuprofile',
55+
JSON.stringify(cpuProfile));
56+
}
57+
58+
if (options.exit) {
59+
process.exit();
60+
}
61+
}
62+
63+
process.on('exit', exitHandler.bind(null, { cleanup: true }));
64+
process.on('SIGINT', exitHandler.bind(null, { exit: true }));
65+
process.on('uncaughtException', exitHandler.bind(null, { exit: true }));
66+
}
67+
68+
resolve('@angular/cli', { basedir: process.cwd() },
69+
function (error: Error, projectLocalCli: string) {
70+
let cli;
71+
if (error) {
72+
// If there is an error, resolve could not find the ng-cli
73+
// library from a package.json. Instead, include it from a relative
74+
// path to this script file (which is likely a globally installed
75+
// npm package). Most common cause for hitting this is `ng new`
76+
cli = require('./cli');
77+
} else {
78+
// This was run from a global, check local version.
79+
const globalVersion = new SemVer(packageJson['version']);
80+
let localVersion;
81+
let shouldWarn = false;
82+
83+
try {
84+
localVersion = _fromPackageJson();
85+
shouldWarn = localVersion && globalVersion.compare(localVersion) > 0;
86+
} catch (e) {
87+
// eslint-disable-next-line no-console
88+
console.error(e);
89+
shouldWarn = true;
90+
}
91+
92+
if (shouldWarn && isWarningEnabled('versionMismatch')) {
93+
let warning = yellow(stripIndents`
94+
Your global Angular CLI version (${globalVersion}) is greater than your local
95+
version (${localVersion}). The local Angular CLI version is used.
96+
97+
To disable this warning use "ng set --global warnings.versionMismatch=false".
98+
`);
99+
// Don't show warning colorised on `ng completion`
100+
if (process.argv[2] !== 'completion') {
101+
// eslint-disable-next-line no-console
102+
console.log(warning);
103+
} else {
104+
// eslint-disable-next-line no-console
105+
console.error(warning);
106+
process.exit(1);
107+
}
108+
}
109+
110+
// No error implies a projectLocalCli, which will load whatever
111+
// version of ng-cli you have installed in a local package.json
112+
cli = require(projectLocalCli);
113+
}
114+
115+
if ('default' in cli) {
116+
cli = cli['default'];
117+
}
118+
119+
let standardInput;
120+
try {
121+
standardInput = process.stdin;
122+
} catch (e) {
123+
delete process.stdin;
124+
process.stdin = new events.EventEmitter();
125+
standardInput = process.stdin;
126+
}
127+
128+
cli({
129+
cliArgs: process.argv.slice(2),
130+
inputStream: standardInput,
131+
outputStream: process.stdout
132+
}).then(function (exitCode: number) {
133+
process.exit(exitCode);
134+
}).catch(function(err: Error) {
135+
console.log('Unknown error: ' + err.toString());
136+
process.exit(127);
137+
});
138+
}
139+
);

0 commit comments

Comments
 (0)