UNPKG

6.08 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * @license
5 * Copyright Google Inc. All Rights Reserved.
6 *
7 * Use of this source code is governed by an MIT-style license that can be
8 * found in the LICENSE file at https://angular.io/license
9 */
10require("symbol-observable");
11// symbol polyfill must go first
12// tslint:disable-next-line:ordered-imports import-groups
13const core_1 = require("@angular-devkit/core");
14const fs = require("fs");
15const path = require("path");
16const semver_1 = require("semver");
17const stream_1 = require("stream");
18const color_1 = require("../utilities/color");
19const config_1 = require("../utilities/config");
20const packageJson = require('../package.json');
21function _fromPackageJson(cwd = process.cwd()) {
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 { version } = JSON.parse(content);
28 if (version) {
29 return new semver_1.SemVer(version);
30 }
31 }
32 }
33 // Check the parent.
34 cwd = path.dirname(cwd);
35 } while (cwd != path.dirname(cwd));
36 return null;
37}
38// Check if we need to profile this CLI run.
39if (process.env['NG_CLI_PROFILING']) {
40 let profiler;
41 try {
42 profiler = require('v8-profiler-node8'); // tslint:disable-line:no-implicit-dependencies
43 }
44 catch (err) {
45 throw new Error(`Could not require 'v8-profiler-node8'. You must install it separetely with ` +
46 `'npm install v8-profiler-node8 --no-save'.\n\nOriginal error:\n\n${err}`);
47 }
48 profiler.startProfiling();
49 const exitHandler = (options) => {
50 if (options.cleanup) {
51 const cpuProfile = profiler.stopProfiling();
52 fs.writeFileSync(path.resolve(process.cwd(), process.env.NG_CLI_PROFILING || '') + '.cpuprofile', JSON.stringify(cpuProfile));
53 }
54 if (options.exit) {
55 process.exit();
56 }
57 };
58 process.on('exit', () => exitHandler({ cleanup: true }));
59 process.on('SIGINT', () => exitHandler({ exit: true }));
60 process.on('uncaughtException', () => exitHandler({ exit: true }));
61}
62(async () => {
63 /**
64 * Disable Browserslist old data warning as otherwise with every release we'd need to update this dependency
65 * which is cumbersome considering we pin versions and the warning is not user actionable.
66 * `Browserslist: caniuse-lite is outdated. Please run next command `npm update`
67 * See: https://github.com/browserslist/browserslist/blob/819c4337456996d19db6ba953014579329e9c6e1/node.js#L324
68 */
69 process.env.BROWSERSLIST_IGNORE_OLD_DATA = '1';
70 const disableVersionCheckEnv = process.env['NG_DISABLE_VERSION_CHECK'];
71 /**
72 * Disable CLI version mismatch checks and forces usage of the invoked CLI
73 * instead of invoking the local installed version.
74 */
75 const disableVersionCheck = disableVersionCheckEnv !== undefined &&
76 disableVersionCheckEnv !== '0' &&
77 disableVersionCheckEnv.toLowerCase() !== 'false';
78 if (disableVersionCheck) {
79 return (await Promise.resolve().then(() => require('./cli'))).default;
80 }
81 let cli;
82 try {
83 const projectLocalCli = require.resolve('@angular/cli', { paths: [process.cwd()] });
84 // This was run from a global, check local version.
85 const globalVersion = new semver_1.SemVer(packageJson['version']);
86 let localVersion;
87 let shouldWarn = false;
88 try {
89 localVersion = _fromPackageJson();
90 shouldWarn = localVersion != null && globalVersion.compare(localVersion) > 0;
91 }
92 catch (e) {
93 // tslint:disable-next-line no-console
94 console.error(e);
95 shouldWarn = true;
96 }
97 if (shouldWarn && await config_1.isWarningEnabled('versionMismatch')) {
98 const warning = color_1.colors.yellow(core_1.tags.stripIndents `
99 Your global Angular CLI version (${globalVersion}) is greater than your local
100 version (${localVersion}). The local Angular CLI version is used.
101
102 To disable this warning use "ng config -g cli.warnings.versionMismatch false".
103 `);
104 // Don't show warning colorised on `ng completion`
105 if (process.argv[2] !== 'completion') {
106 // tslint:disable-next-line no-console
107 console.error(warning);
108 }
109 else {
110 // tslint:disable-next-line no-console
111 console.error(warning);
112 process.exit(1);
113 }
114 }
115 // No error implies a projectLocalCli, which will load whatever
116 // version of ng-cli you have installed in a local package.json
117 cli = await Promise.resolve().then(() => require(projectLocalCli));
118 }
119 catch (_a) {
120 // If there is an error, resolve could not find the ng-cli
121 // library from a package.json. Instead, include it from a relative
122 // path to this script file (which is likely a globally installed
123 // npm package). Most common cause for hitting this is `ng new`
124 cli = await Promise.resolve().then(() => require('./cli'));
125 }
126 if ('default' in cli) {
127 cli = cli['default'];
128 }
129 return cli;
130})().then(cli => {
131 // This is required to support 1.x local versions with a 6+ global
132 let standardInput;
133 try {
134 standardInput = process.stdin;
135 }
136 catch (e) {
137 delete process.stdin;
138 process.stdin = new stream_1.Duplex();
139 standardInput = process.stdin;
140 }
141 return cli({
142 cliArgs: process.argv.slice(2),
143 inputStream: standardInput,
144 outputStream: process.stdout,
145 });
146}).then((exitCode) => {
147 process.exit(exitCode);
148})
149 .catch((err) => {
150 // tslint:disable-next-line no-console
151 console.error('Unknown error: ' + err.toString());
152 process.exit(127);
153});