UNPKG

4.6 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 packageJson = require('../package.json');
12const path = require('path');
13const resolve = require('resolve');
14const stripIndents = require('common-tags').stripIndents;
15const yellow = require('chalk').yellow;
16const SemVer = require('semver').SemVer;
17
18
19function _fromPackageJson(cwd) {
20 cwd = cwd || process.cwd();
21
22 do {
23 const packageJsonPath = path.join(cwd, 'node_modules/angular-cli/package.json');
24 if (fs.existsSync(packageJsonPath)) {
25 const content = fs.readFileSync(packageJsonPath, 'utf-8');
26 if (content) {
27 const json = JSON.parse(content);
28 if (json['version']) {
29 return new SemVer(json['version']);
30 }
31 }
32 }
33
34 // Check the parent.
35 cwd = path.dirname(cwd);
36 } while (cwd != path.dirname(cwd));
37
38 return null;
39}
40
41
42// Check if we need to profile this CLI run.
43let profiler = null;
44if (process.env['NG_CLI_PROFILING']) {
45 profiler = require('v8-profiler');
46 profiler.startProfiling();
47 function exitHandler(options, err) {
48 if (options.cleanup) {
49 const cpuProfile = profiler.stopProfiling();
50 fs.writeFileSync(path.resolve(process.cwd(), process.env.NG_CLI_PROFILING) + '.cpuprofile',
51 JSON.stringify(cpuProfile));
52 }
53
54 if (options.exit) {
55 process.exit();
56 }
57 }
58
59 process.on('exit', exitHandler.bind(null, { cleanup: true }));
60 process.on('SIGINT', exitHandler.bind(null, { exit: true }));
61 process.on('uncaughtException', exitHandler.bind(null, { exit: true }));
62}
63
64
65// Show the warnings due to package and version deprecation.
66const version = new SemVer(process.version);
67if (version.compare(new SemVer('6.9.0')) < 0
68 && CliConfig.fromGlobal().get('warnings.nodeDeprecation')) {
69 process.stderr.write(yellow(stripIndents`
70 You are running version ${version.version} of Node, which will not be supported in future
71 versions of the CLI. The official Node version that will be supported is 6.9 and greater.
72
73 To disable this warning use "ng set --global warnings.nodeDeprecation=false".
74 `));
75}
76
77
78if (require('../package.json')['name'] == 'angular-cli'
79 && CliConfig.fromGlobal().get('warnings.packageDeprecation')) {
80 process.stderr.write(yellow(stripIndents`
81 As a forewarning, we are moving the CLI npm package to "@angular/cli" with the next release,
82 which will only support Node 6.9 and greater. This package will be officially deprecated
83 shortly after.
84
85 To disable this warning use "ng set --global warnings.packageDeprecation=false".
86 `));
87}
88
89
90resolve('angular-cli', { basedir: process.cwd() },
91 function (error, projectLocalCli) {
92 var cli;
93 if (error) {
94 // If there is an error, resolve could not find the ng-cli
95 // library from a package.json. Instead, include it from a relative
96 // path to this script file (which is likely a globally installed
97 // npm package). Most common cause for hitting this is `ng new`
98 cli = require('../lib/cli/index');
99 } else {
100 // Verify that package's version.
101 Version.assertPostWebpackVersion();
102
103 // This was run from a global, check local version.
104 const globalVersion = new SemVer(packageJson['version']);
105 let localVersion;
106 let shouldWarn = false;
107
108 try {
109 localVersion = _fromPackageJson();
110 shouldWarn = localVersion && globalVersion.compare(localVersion) < 0;
111 } catch (e) {
112 console.error(e);
113 shouldWarn = true;
114 }
115
116 if (shouldWarn && CliConfig.fromGlobal().get('warnings.versionMismatch')) {
117 // eslint-disable no-console
118 console.log(yellow(stripIndents`
119 Your global Angular CLI version (${globalVersion}) is greater than your local
120 version (${localVersion}). The local Angular CLI version is used.
121
122 To disable this warning use "ng set --global warnings.versionMismatch=false".
123 `));
124 }
125
126 // No error implies a projectLocalCli, which will load whatever
127 // version of ng-cli you have installed in a local package.json
128 cli = require(projectLocalCli);
129 }
130
131 if ('default' in cli) {
132 cli = cli['default'];
133 }
134
135 cli({
136 cliArgs: process.argv.slice(2),
137 inputStream: process.stdin,
138 outputStream: process.stdout
139 }).then(function (result) {
140 process.exit(typeof result === 'object' ? result.exitCode : result);
141 });
142 });