UNPKG

6.23 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 deps = packageJsonProject['dependencies'] || {};
95 const devDeps = packageJsonProject['devDependencies'] || {};
96 const hasOldDep = !!deps['angular-cli'];
97 const hasDep = !!deps['@angular/cli'];
98 const hasOldDevDep = !!devDeps['angular-cli'];
99 const hasDevDep = !!devDeps['@angular/cli'];
100
101 if (hasOldDep || hasOldDevDep || !(hasDevDep || hasDep)) {
102 const warnings = [
103 'Unable to find "@angular/cli" in devDependencies.',
104 ''
105 ];
106
107 if (hasOldDep || hasOldDevDep) {
108 warnings.push(
109 'The package "angular-cli" has been deprecated and renamed to "@angular/cli".',
110 '');
111 }
112
113 warnings.push('Please take the following steps to avoid issues:');
114
115 if (hasOldDep) {
116 warnings.push('"npm uninstall --save angular-cli"');
117 }
118 if (hasOldDevDep) {
119 warnings.push('"npm uninstall --save-dev angular-cli"');
120 }
121 if (hasDep) {
122 warnings.push('"npm uninstall --save @angular/cli"')
123 }
124 if (!hasDevDep) {
125 warnings.push('"npm install --save-dev @angular/cli@latest"');
126 }
127 process.stderr.write(yellow(warnings.join('\n'), '\n\n'));
128 }
129}
130
131resolve('@angular/cli', { basedir: process.cwd() },
132 function (error, projectLocalCli) {
133 var cli;
134 if (error) {
135 // If there is an error, resolve could not find the ng-cli
136 // library from a package.json. Instead, include it from a relative
137 // path to this script file (which is likely a globally installed
138 // npm package). Most common cause for hitting this is `ng new`
139 cli = require('../lib/cli');
140 } else {
141 // Verify that package's version.
142 Version.assertPostWebpackVersion();
143
144 // This was run from a global, check local version.
145 const globalVersion = new SemVer(packageJson['version']);
146 let localVersion;
147 let shouldWarn = false;
148
149 try {
150 localVersion = _fromPackageJson();
151 shouldWarn = localVersion && globalVersion.compare(localVersion) > 0;
152 } catch (e) {
153 console.error(e);
154 shouldWarn = true;
155 }
156
157 if (shouldWarn && CliConfig.fromGlobal().get('warnings.versionMismatch')) {
158 // eslint-disable no-console
159 console.log(yellow(stripIndents`
160 Your global Angular CLI version (${globalVersion}) is greater than your local
161 version (${localVersion}). The local Angular CLI version is used.
162
163 To disable this warning use "ng set --global warnings.versionMismatch=false".
164 `));
165 }
166
167 // No error implies a projectLocalCli, which will load whatever
168 // version of ng-cli you have installed in a local package.json
169 cli = require(projectLocalCli);
170 }
171
172 if ('default' in cli) {
173 cli = cli['default'];
174 }
175
176 let standardInput;
177 try {
178 standardInput = process.stdin;
179 } catch (e) {
180 delete process.stdin;
181 process.stdin = new events.EventEmitter();
182 standardInput = process.stdin;
183 }
184
185 cli({
186 cliArgs: process.argv.slice(2),
187 inputStream: standardInput,
188 outputStream: process.stdout
189 }).then(function (result) {
190 process.exit(typeof result === 'object' ? result.exitCode : result);
191 });
192 });