executor.js 6.4 KB

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