executor.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. var __importDefault = (this && this.__importDefault) || function (mod) {
  43. return (mod && mod.__esModule) ? mod : { "default": mod };
  44. };
  45. Object.defineProperty(exports, "__esModule", { value: true });
  46. exports.UnknownPackageManagerException = void 0;
  47. exports.default = default_1;
  48. const core_1 = require("@angular-devkit/core");
  49. const node_child_process_1 = require("node:child_process");
  50. const path = __importStar(require("node:path"));
  51. const ora_1 = __importDefault(require("ora"));
  52. const src_1 = require("../../src");
  53. const packageManagers = {
  54. 'npm': {
  55. commands: {
  56. installAll: 'install',
  57. installPackage: 'install',
  58. },
  59. },
  60. 'cnpm': {
  61. commands: {
  62. installAll: 'install',
  63. installPackage: 'install',
  64. },
  65. },
  66. 'yarn': {
  67. commands: {
  68. installAll: 'install',
  69. installPackage: 'add',
  70. },
  71. },
  72. 'bun': {
  73. commands: {
  74. installAll: 'install',
  75. installPackage: 'add',
  76. },
  77. },
  78. 'pnpm': {
  79. commands: {
  80. installAll: 'install',
  81. installPackage: 'install',
  82. },
  83. },
  84. };
  85. class UnknownPackageManagerException extends core_1.BaseException {
  86. constructor(name) {
  87. super(`Unknown package manager "${name}".`);
  88. }
  89. }
  90. exports.UnknownPackageManagerException = UnknownPackageManagerException;
  91. function default_1(factoryOptions = {}) {
  92. const packageManagerName = factoryOptions.packageManager || 'npm';
  93. const packageManagerProfile = packageManagers[packageManagerName];
  94. if (!packageManagerProfile) {
  95. throw new UnknownPackageManagerException(packageManagerName);
  96. }
  97. const rootDirectory = factoryOptions.rootDirectory || process.cwd();
  98. return (options = { command: 'install' }) => {
  99. let taskPackageManagerProfile = packageManagerProfile;
  100. let taskPackageManagerName = packageManagerName;
  101. if (factoryOptions.allowPackageManagerOverride && options.packageManager) {
  102. taskPackageManagerProfile = packageManagers[options.packageManager];
  103. if (!taskPackageManagerProfile) {
  104. throw new UnknownPackageManagerException(options.packageManager);
  105. }
  106. taskPackageManagerName = options.packageManager;
  107. }
  108. const bufferedOutput = [];
  109. const spawnOptions = {
  110. shell: true,
  111. cwd: path.join(rootDirectory, options.workingDirectory || ''),
  112. };
  113. if (options.hideOutput) {
  114. spawnOptions.stdio = options.quiet ? ['ignore', 'ignore', 'pipe'] : 'pipe';
  115. }
  116. else {
  117. spawnOptions.stdio = options.quiet ? ['ignore', 'ignore', 'inherit'] : 'inherit';
  118. }
  119. const args = [];
  120. if (options.packageName) {
  121. if (options.command === 'install') {
  122. args.push(taskPackageManagerProfile.commands.installPackage);
  123. }
  124. args.push(options.packageName);
  125. }
  126. else if (options.command === 'install' && taskPackageManagerProfile.commands.installAll) {
  127. args.push(taskPackageManagerProfile.commands.installAll);
  128. }
  129. if (!options.allowScripts) {
  130. // Yarn requires special handling since Yarn 2+ no longer has the `--ignore-scripts` flag
  131. if (taskPackageManagerName === 'yarn') {
  132. spawnOptions.env = {
  133. ...process.env,
  134. // Supported with yarn 1
  135. 'npm_config_ignore_scripts': 'true',
  136. // Supported with yarn 2+
  137. 'YARN_ENABLE_SCRIPTS': 'false',
  138. };
  139. }
  140. else {
  141. args.push('--ignore-scripts');
  142. }
  143. }
  144. if (factoryOptions.registry) {
  145. args.push(`--registry="${factoryOptions.registry}"`);
  146. }
  147. if (factoryOptions.force) {
  148. args.push('--force');
  149. }
  150. return new Promise((resolve, reject) => {
  151. const spinner = (0, ora_1.default)({
  152. text: `Installing packages (${taskPackageManagerName})...`,
  153. // Workaround for https://github.com/sindresorhus/ora/issues/136.
  154. discardStdin: process.platform != 'win32',
  155. }).start();
  156. const childProcess = (0, node_child_process_1.spawn)(taskPackageManagerName, args, spawnOptions).on('close', (code) => {
  157. if (code === 0) {
  158. spinner.succeed('Packages installed successfully.');
  159. spinner.stop();
  160. resolve();
  161. }
  162. else {
  163. if (options.hideOutput) {
  164. bufferedOutput.forEach(({ stream, data }) => stream.write(data));
  165. }
  166. spinner.fail('Package install failed, see above.');
  167. reject(new src_1.UnsuccessfulWorkflowExecution());
  168. }
  169. });
  170. if (options.hideOutput) {
  171. childProcess.stdout?.on('data', (data) => bufferedOutput.push({ stream: process.stdout, data: data }));
  172. childProcess.stderr?.on('data', (data) => bufferedOutput.push({ stream: process.stderr, data: data }));
  173. }
  174. });
  175. };
  176. }