init.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. const promises_1 = require("node:fs/promises");
  44. const node_module_1 = require("node:module");
  45. const path = __importStar(require("node:path"));
  46. const semver_1 = require("semver");
  47. const color_1 = require("../src/utilities/color");
  48. const config_1 = require("../src/utilities/config");
  49. const environment_options_1 = require("../src/utilities/environment-options");
  50. const version_1 = require("../src/utilities/version");
  51. /**
  52. * Angular CLI versions prior to v14 may not exit correctly if not forcibly exited
  53. * via `process.exit()`. When bootstrapping, `forceExit` will be set to `true`
  54. * if the local CLI version is less than v14 to prevent the CLI from hanging on
  55. * exit in those cases.
  56. */
  57. let forceExit = false;
  58. (async () => {
  59. /**
  60. * Disable Browserslist old data warning as otherwise with every release we'd need to update this dependency
  61. * which is cumbersome considering we pin versions and the warning is not user actionable.
  62. * `Browserslist: caniuse-lite is outdated. Please run next command `npm update`
  63. * See: https://github.com/browserslist/browserslist/blob/819c4337456996d19db6ba953014579329e9c6e1/node.js#L324
  64. */
  65. process.env.BROWSERSLIST_IGNORE_OLD_DATA = '1';
  66. const rawCommandName = process.argv[2];
  67. /**
  68. * Disable CLI version mismatch checks and forces usage of the invoked CLI
  69. * instead of invoking the local installed version.
  70. *
  71. * When running `ng new` always favor the global version. As in some
  72. * cases orphan `node_modules` would cause the non global CLI to be used.
  73. * @see: https://github.com/angular/angular-cli/issues/14603
  74. */
  75. if (environment_options_1.disableVersionCheck || rawCommandName === 'new') {
  76. return (await Promise.resolve().then(() => __importStar(require('./cli')))).default;
  77. }
  78. let cli;
  79. try {
  80. // No error implies a projectLocalCli, which will load whatever
  81. // version of ng-cli you have installed in a local package.json
  82. const cwdRequire = (0, node_module_1.createRequire)(process.cwd() + '/');
  83. const projectLocalCli = cwdRequire.resolve('@angular/cli');
  84. cli = await Promise.resolve(`${projectLocalCli}`).then(s => __importStar(require(s)));
  85. const globalVersion = new semver_1.SemVer(version_1.VERSION.full);
  86. // Older versions might not have the VERSION export
  87. let localVersion = cli.VERSION?.full;
  88. if (!localVersion) {
  89. try {
  90. const localPackageJson = await (0, promises_1.readFile)(path.join(path.dirname(projectLocalCli), '../../package.json'), 'utf-8');
  91. localVersion = JSON.parse(localPackageJson).version;
  92. }
  93. catch (error) {
  94. // eslint-disable-next-line no-console
  95. console.error('Version mismatch check skipped. Unable to retrieve local version: ' + error);
  96. }
  97. }
  98. // Ensure older versions of the CLI fully exit
  99. const localMajorVersion = (0, semver_1.major)(localVersion);
  100. if (localMajorVersion > 0 && localMajorVersion < 14) {
  101. forceExit = true;
  102. // Versions prior to 14 didn't implement completion command.
  103. if (rawCommandName === 'completion') {
  104. return null;
  105. }
  106. }
  107. let isGlobalGreater = false;
  108. try {
  109. isGlobalGreater = localVersion > 0 && globalVersion.compare(localVersion) > 0;
  110. }
  111. catch (error) {
  112. // eslint-disable-next-line no-console
  113. console.error('Version mismatch check skipped. Unable to compare local version: ' + error);
  114. }
  115. // When using the completion command, don't show the warning as otherwise this will break completion.
  116. if (isGlobalGreater &&
  117. rawCommandName !== '--get-yargs-completions' &&
  118. rawCommandName !== 'completion') {
  119. // If using the update command and the global version is greater, use the newer update command
  120. // This allows improvements in update to be used in older versions that do not have bootstrapping
  121. if (rawCommandName === 'update' &&
  122. cli.VERSION &&
  123. cli.VERSION.major - globalVersion.major <= 1) {
  124. cli = await Promise.resolve().then(() => __importStar(require('./cli')));
  125. }
  126. else if (await (0, config_1.isWarningEnabled)('versionMismatch')) {
  127. // Otherwise, use local version and warn if global is newer than local
  128. const warning = `Your global Angular CLI version (${globalVersion}) is greater than your local ` +
  129. `version (${localVersion}). The local Angular CLI version is used.\n\n` +
  130. 'To disable this warning use "ng config -g cli.warnings.versionMismatch false".';
  131. // eslint-disable-next-line no-console
  132. console.error(color_1.colors.yellow(warning));
  133. }
  134. }
  135. }
  136. catch {
  137. // If there is an error, resolve could not find the ng-cli
  138. // library from a package.json. Instead, include it from a relative
  139. // path to this script file (which is likely a globally installed
  140. // npm package). Most common cause for hitting this is `ng new`
  141. cli = await Promise.resolve().then(() => __importStar(require('./cli')));
  142. }
  143. if ('default' in cli) {
  144. cli = cli['default'];
  145. }
  146. return cli;
  147. })()
  148. .then((cli) => cli?.({
  149. cliArgs: process.argv.slice(2),
  150. }))
  151. .then((exitCode = 0) => {
  152. if (forceExit) {
  153. process.exit(exitCode);
  154. }
  155. process.exitCode = exitCode;
  156. })
  157. .catch((err) => {
  158. // eslint-disable-next-line no-console
  159. console.error('Unknown error: ' + err.toString());
  160. process.exit(127);
  161. });