init.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.dev/license
  8. */
  9. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. var desc = Object.getOwnPropertyDescriptor(m, k);
  12. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  13. desc = { enumerable: true, get: function() { return m[k]; } };
  14. }
  15. Object.defineProperty(o, k2, desc);
  16. }) : (function(o, m, k, k2) {
  17. if (k2 === undefined) k2 = k;
  18. o[k2] = m[k];
  19. }));
  20. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  21. Object.defineProperty(o, "default", { enumerable: true, value: v });
  22. }) : function(o, v) {
  23. o["default"] = v;
  24. });
  25. var __importStar = (this && this.__importStar) || (function () {
  26. var ownKeys = function(o) {
  27. ownKeys = Object.getOwnPropertyNames || function (o) {
  28. var ar = [];
  29. for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
  30. return ar;
  31. };
  32. return ownKeys(o);
  33. };
  34. return function (mod) {
  35. if (mod && mod.__esModule) return mod;
  36. var result = {};
  37. if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
  38. __setModuleDefault(result, mod);
  39. return result;
  40. };
  41. })();
  42. Object.defineProperty(exports, "__esModule", { value: true });
  43. require("symbol-observable");
  44. // symbol polyfill must go first
  45. const node_fs_1 = require("node:fs");
  46. const node_module_1 = require("node:module");
  47. const path = __importStar(require("node:path"));
  48. const semver_1 = require("semver");
  49. const color_1 = require("../src/utilities/color");
  50. const config_1 = require("../src/utilities/config");
  51. const environment_options_1 = require("../src/utilities/environment-options");
  52. const version_1 = require("../src/utilities/version");
  53. /**
  54. * Angular CLI versions prior to v14 may not exit correctly if not forcibly exited
  55. * via `process.exit()`. When bootstrapping, `forceExit` will be set to `true`
  56. * if the local CLI version is less than v14 to prevent the CLI from hanging on
  57. * exit in those cases.
  58. */
  59. let forceExit = false;
  60. (async () => {
  61. /**
  62. * Disable Browserslist old data warning as otherwise with every release we'd need to update this dependency
  63. * which is cumbersome considering we pin versions and the warning is not user actionable.
  64. * `Browserslist: caniuse-lite is outdated. Please run next command `npm update`
  65. * See: https://github.com/browserslist/browserslist/blob/819c4337456996d19db6ba953014579329e9c6e1/node.js#L324
  66. */
  67. process.env.BROWSERSLIST_IGNORE_OLD_DATA = '1';
  68. const rawCommandName = process.argv[2];
  69. /**
  70. * Disable CLI version mismatch checks and forces usage of the invoked CLI
  71. * instead of invoking the local installed version.
  72. *
  73. * When running `ng new` always favor the global version. As in some
  74. * cases orphan `node_modules` would cause the non global CLI to be used.
  75. * @see: https://github.com/angular/angular-cli/issues/14603
  76. */
  77. if (environment_options_1.disableVersionCheck || rawCommandName === 'new') {
  78. return (await Promise.resolve().then(() => __importStar(require('./cli')))).default;
  79. }
  80. let cli;
  81. try {
  82. // No error implies a projectLocalCli, which will load whatever
  83. // version of ng-cli you have installed in a local package.json
  84. const cwdRequire = (0, node_module_1.createRequire)(process.cwd() + '/');
  85. const projectLocalCli = cwdRequire.resolve('@angular/cli');
  86. cli = await Promise.resolve(`${projectLocalCli}`).then(s => __importStar(require(s)));
  87. const globalVersion = new semver_1.SemVer(version_1.VERSION.full);
  88. // Older versions might not have the VERSION export
  89. let localVersion = cli.VERSION?.full;
  90. if (!localVersion) {
  91. try {
  92. const localPackageJson = await node_fs_1.promises.readFile(path.join(path.dirname(projectLocalCli), '../../package.json'), 'utf-8');
  93. localVersion = JSON.parse(localPackageJson).version;
  94. }
  95. catch (error) {
  96. // eslint-disable-next-line no-console
  97. console.error('Version mismatch check skipped. Unable to retrieve local version: ' + error);
  98. }
  99. }
  100. // Ensure older versions of the CLI fully exit
  101. const localMajorVersion = (0, semver_1.major)(localVersion);
  102. if (localMajorVersion > 0 && localMajorVersion < 14) {
  103. forceExit = true;
  104. // Versions prior to 14 didn't implement completion command.
  105. if (rawCommandName === 'completion') {
  106. return null;
  107. }
  108. }
  109. let isGlobalGreater = false;
  110. try {
  111. isGlobalGreater = localVersion > 0 && globalVersion.compare(localVersion) > 0;
  112. }
  113. catch (error) {
  114. // eslint-disable-next-line no-console
  115. console.error('Version mismatch check skipped. Unable to compare local version: ' + error);
  116. }
  117. // When using the completion command, don't show the warning as otherwise this will break completion.
  118. if (isGlobalGreater &&
  119. rawCommandName !== '--get-yargs-completions' &&
  120. rawCommandName !== 'completion') {
  121. // If using the update command and the global version is greater, use the newer update command
  122. // This allows improvements in update to be used in older versions that do not have bootstrapping
  123. if (rawCommandName === 'update' &&
  124. cli.VERSION &&
  125. cli.VERSION.major - globalVersion.major <= 1) {
  126. cli = await Promise.resolve().then(() => __importStar(require('./cli')));
  127. }
  128. else if (await (0, config_1.isWarningEnabled)('versionMismatch')) {
  129. // Otherwise, use local version and warn if global is newer than local
  130. const warning = `Your global Angular CLI version (${globalVersion}) is greater than your local ` +
  131. `version (${localVersion}). The local Angular CLI version is used.\n\n` +
  132. 'To disable this warning use "ng config -g cli.warnings.versionMismatch false".';
  133. // eslint-disable-next-line no-console
  134. console.error(color_1.colors.yellow(warning));
  135. }
  136. }
  137. }
  138. catch {
  139. // If there is an error, resolve could not find the ng-cli
  140. // library from a package.json. Instead, include it from a relative
  141. // path to this script file (which is likely a globally installed
  142. // npm package). Most common cause for hitting this is `ng new`
  143. cli = await Promise.resolve().then(() => __importStar(require('./cli')));
  144. }
  145. if ('default' in cli) {
  146. cli = cli['default'];
  147. }
  148. return cli;
  149. })()
  150. .then((cli) => cli?.({
  151. cliArgs: process.argv.slice(2),
  152. }))
  153. .then((exitCode = 0) => {
  154. if (forceExit) {
  155. process.exit(exitCode);
  156. }
  157. process.exitCode = exitCode;
  158. })
  159. .catch((err) => {
  160. // eslint-disable-next-line no-console
  161. console.error('Unknown error: ' + err.toString());
  162. process.exit(127);
  163. });