executor.js 6.8 KB

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