UNPKG

5.94 kBPlain TextView Raw
1#!/usr/bin/env node
2'use strict';
3
4// Provide a title to the process in `ps`
5process.title = '@angular/cli';
6
7const CliConfig = require('../models/config').CliConfig;
8const Version = require('../upgrade/version').Version;
9
10const fs = require('fs');
11const findUp = require('../utilities/find-up').findUp;
12const packageJson = require('../package.json');
13const path = require('path');
14const resolve = require('resolve');
15const stripIndents = require('common-tags').stripIndents;
16const yellow = require('chalk').yellow;
17const SemVer = require('semver').SemVer;
18const events = require('events');
19
20
21function _fromPackageJson(cwd) {
22 cwd = cwd || process.cwd();
23
24 do {
25 const packageJsonPath = path.join(cwd, 'node_modules/@angular/cli/package.json');
26 if (fs.existsSync(packageJsonPath)) {
27 const content = fs.readFileSync(packageJsonPath, 'utf-8');
28 if (content) {
29 const json = JSON.parse(content);
30 if (json['version']) {
31 return new SemVer(json['version']);
32 }
33 }
34 }
35
36 // Check the parent.
37 cwd = path.dirname(cwd);
38 } while (cwd != path.dirname(cwd));
39
40 return null;
41}
42
43
44// Check if we need to profile this CLI run.
45let profiler = null;
46if (process.env['NG_CLI_PROFILING']) {
47 profiler = require('v8-profiler');
48 profiler.startProfiling();
49 function exitHandler(options, err) {
50 if (options.cleanup) {
51 const cpuProfile = profiler.stopProfiling();
52 fs.writeFileSync(path.resolve(process.cwd(), process.env.NG_CLI_PROFILING) + '.cpuprofile',
53 JSON.stringify(cpuProfile));
54 }
55
56 if (options.exit) {
57 process.exit();
58 }
59 }
60
61 process.on('exit', exitHandler.bind(null, { cleanup: true }));
62 process.on('SIGINT', exitHandler.bind(null, { exit: true }));
63 process.on('uncaughtException', exitHandler.bind(null, { exit: true }));
64}
65
66
67// Show the warnings due to package and version deprecation.
68const version = new SemVer(process.version);
69if (version.compare(new SemVer('6.9.0')) < 0
70 && CliConfig.fromGlobal().get('warnings.nodeDeprecation')) {
71 process.stderr.write(yellow(stripIndents`
72 You are running version ${version.version} of Node, which will not be supported in future
73 versions of the CLI. The official Node version that will be supported is 6.9 and greater.
74
75 To disable this warning use "ng set --global warnings.nodeDeprecation=false".
76 `));
77}
78
79
80if (require('../package.json')['name'] == 'angular-cli'
81 && CliConfig.fromGlobal().get('warnings.packageDeprecation')) {
82 process.stderr.write(yellow(stripIndents`
83 As a forewarning, we are moving the CLI npm package to "@angular/cli" with the next release,
84 which will only support Node 6.9 and greater. This package will be officially deprecated
85 shortly after.
86
87 To disable this warning use "ng set --global warnings.packageDeprecation=false".
88 `));
89}
90
91const packageJsonProjectPath = findUp('package.json', process.cwd(), true);
92if (packageJsonProjectPath && fs.existsSync(packageJsonProjectPath)) {
93 const packageJsonProject = require(packageJsonProjectPath);
94 const hasOldDep = !!packageJsonProject.dependencies['angular-cli'];
95 const hasOldDevDep = !!packageJsonProject.devDependencies['angular-cli'];
96 const hasDevDep = !!packageJsonProject.devDependencies['@angular/cli'];
97
98 if (hasOldDep || hasOldDevDep || !hasDevDep) {
99 const warnings = [
100 'The package "angular-cli" has been renamed to "@angular/cli". The old package will be '
101 + 'deprecated soon.',
102 '',
103 'Please take the following steps to avoid issues:'
104 ];
105 if (hasOldDep) {
106 warnings.push('"npm uninstall --save angular-cli"');
107 }
108 if (hasOldDevDep) {
109 warnings.push('"npm uninstall --save-dev angular-cli"');
110 }
111 if (!hasDevDep) {
112 warnings.push('"npm install --save-dev @angular/cli@latest"');
113 }
114 process.stderr.write(yellow(warnings.join('\n'), '\n\n'));
115 }
116}
117
118resolve('@angular/cli', { basedir: process.cwd() },
119 function (error, projectLocalCli) {
120 var cli;
121 if (error) {
122 // If there is an error, resolve could not find the ng-cli
123 // library from a package.json. Instead, include it from a relative
124 // path to this script file (which is likely a globally installed
125 // npm package). Most common cause for hitting this is `ng new`
126 cli = require('../lib/cli');
127 } else {
128 // Verify that package's version.
129 Version.assertPostWebpackVersion();
130
131 // This was run from a global, check local version.
132 const globalVersion = new SemVer(packageJson['version']);
133 let localVersion;
134 let shouldWarn = false;
135
136 try {
137 localVersion = _fromPackageJson();
138 shouldWarn = localVersion && globalVersion.compare(localVersion) > 0;
139 } catch (e) {
140 console.error(e);
141 shouldWarn = true;
142 }
143
144 if (shouldWarn && CliConfig.fromGlobal().get('warnings.versionMismatch')) {
145 // eslint-disable no-console
146 console.log(yellow(stripIndents`
147 Your global Angular CLI version (${globalVersion}) is greater than your local
148 version (${localVersion}). The local Angular CLI version is used.
149
150 To disable this warning use "ng set --global warnings.versionMismatch=false".
151 `));
152 }
153
154 // No error implies a projectLocalCli, which will load whatever
155 // version of ng-cli you have installed in a local package.json
156 cli = require(projectLocalCli);
157 }
158
159 if ('default' in cli) {
160 cli = cli['default'];
161 }
162
163 let standardInput;
164 try {
165 standardInput = process.stdin;
166 } catch (e) {
167 delete process.stdin;
168 process.stdin = new events.EventEmitter();
169 standardInput = process.stdin;
170 }
171
172 cli({
173 cliArgs: process.argv.slice(2),
174 inputStream: standardInput,
175 outputStream: process.stdout
176 }).then(function (result) {
177 process.exit(typeof result === 'object' ? result.exitCode : result);
178 });
179 });