init.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. "use strict";
  2. Object.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. */
  10. require("symbol-observable");
  11. // symbol polyfill must go first
  12. // tslint:disable-next-line:ordered-imports import-groups
  13. const core_1 = require("@angular-devkit/core");
  14. const fs = require("fs");
  15. const path = require("path");
  16. const semver_1 = require("semver");
  17. const stream_1 = require("stream");
  18. const color_1 = require("../utilities/color");
  19. const config_1 = require("../utilities/config");
  20. const packageJson = require('../package.json');
  21. function _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. /**
  39. * Disable Browserslist old data warning as otherwise with every release we'd need to update this dependency
  40. * which is cumbersome considering we pin versions and the warning is not user actionable.
  41. * `Browserslist: caniuse-lite is outdated. Please run next command `npm update`
  42. * See: https://github.com/browserslist/browserslist/blob/819c4337456996d19db6ba953014579329e9c6e1/node.js#L324
  43. */
  44. process.env.BROWSERSLIST_IGNORE_OLD_DATA = '1';
  45. // Check if we need to profile this CLI run.
  46. if (process.env['NG_CLI_PROFILING']) {
  47. let profiler;
  48. try {
  49. profiler = require('v8-profiler-node8'); // tslint:disable-line:no-implicit-dependencies
  50. }
  51. catch (err) {
  52. throw new Error(`Could not require 'v8-profiler-node8'. You must install it separetely with ` +
  53. `'npm install v8-profiler-node8 --no-save'.\n\nOriginal error:\n\n${err}`);
  54. }
  55. profiler.startProfiling();
  56. const exitHandler = (options) => {
  57. if (options.cleanup) {
  58. const cpuProfile = profiler.stopProfiling();
  59. fs.writeFileSync(path.resolve(process.cwd(), process.env.NG_CLI_PROFILING || '') + '.cpuprofile', JSON.stringify(cpuProfile));
  60. }
  61. if (options.exit) {
  62. process.exit();
  63. }
  64. };
  65. process.on('exit', () => exitHandler({ cleanup: true }));
  66. process.on('SIGINT', () => exitHandler({ exit: true }));
  67. process.on('uncaughtException', () => exitHandler({ exit: true }));
  68. }
  69. let cli;
  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. cli = require('./cli');
  80. }
  81. else {
  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 && 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. To disable this warning use "ng config -g cli.warnings.versionMismatch false".
  102. `);
  103. // Don't show warning colorised on `ng completion`
  104. if (process.argv[2] !== 'completion') {
  105. // tslint:disable-next-line no-console
  106. console.error(warning);
  107. }
  108. else {
  109. // tslint:disable-next-line no-console
  110. console.error(warning);
  111. process.exit(1);
  112. }
  113. }
  114. // No error implies a projectLocalCli, which will load whatever
  115. // version of ng-cli you have installed in a local package.json
  116. cli = require(projectLocalCli);
  117. }
  118. catch (_a) {
  119. // If there is an error, resolve could not find the ng-cli
  120. // library from a package.json. Instead, include it from a relative
  121. // path to this script file (which is likely a globally installed
  122. // npm package). Most common cause for hitting this is `ng new`
  123. cli = require('./cli');
  124. }
  125. }
  126. if ('default' in cli) {
  127. cli = cli['default'];
  128. }
  129. // This is required to support 1.x local versions with a 6+ global
  130. let standardInput;
  131. try {
  132. standardInput = process.stdin;
  133. }
  134. catch (e) {
  135. delete process.stdin;
  136. process.stdin = new stream_1.Duplex();
  137. standardInput = process.stdin;
  138. }
  139. cli({
  140. cliArgs: process.argv.slice(2),
  141. inputStream: standardInput,
  142. outputStream: process.stdout,
  143. })
  144. .then((exitCode) => {
  145. process.exit(exitCode);
  146. })
  147. .catch((err) => {
  148. // tslint:disable-next-line no-console
  149. console.error('Unknown error: ' + err.toString());
  150. process.exit(127);
  151. });