executor.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * @license
  5. * Copyright Google Inc. All Rights Reserved.
  6. *
  7. * Use of this source code is governed by an MIT-style license that can be
  8. * found in the LICENSE file at https://angular.io/license
  9. */
  10. const core_1 = require("@angular-devkit/core");
  11. const child_process_1 = require("child_process");
  12. const path = require("path");
  13. const rxjs_1 = require("rxjs");
  14. const packageManagers = {
  15. 'npm': {
  16. quietArgument: '--quiet',
  17. commands: {
  18. installAll: 'install',
  19. installPackage: 'install',
  20. },
  21. },
  22. 'cnpm': {
  23. commands: {
  24. installAll: 'install',
  25. installPackage: 'install',
  26. },
  27. },
  28. 'yarn': {
  29. quietArgument: '--silent',
  30. commands: {
  31. installPackage: 'add',
  32. },
  33. },
  34. 'pnpm': {
  35. quietArgument: '--silent',
  36. commands: {
  37. installAll: 'install',
  38. installPackage: 'install',
  39. },
  40. },
  41. };
  42. class UnknownPackageManagerException extends core_1.BaseException {
  43. constructor(name) {
  44. super(`Unknown package manager "${name}".`);
  45. }
  46. }
  47. exports.UnknownPackageManagerException = UnknownPackageManagerException;
  48. function default_1(factoryOptions = {}) {
  49. const packageManagerName = factoryOptions.packageManager || 'npm';
  50. const packageManagerProfile = packageManagers[packageManagerName];
  51. if (!packageManagerProfile) {
  52. throw new UnknownPackageManagerException(packageManagerName);
  53. }
  54. const rootDirectory = factoryOptions.rootDirectory || process.cwd();
  55. return (options) => {
  56. let taskPackageManagerProfile = packageManagerProfile;
  57. let taskPackageManagerName = packageManagerName;
  58. if (factoryOptions.allowPackageManagerOverride && options.packageManager) {
  59. taskPackageManagerProfile = packageManagers[options.packageManager];
  60. if (!taskPackageManagerProfile) {
  61. throw new UnknownPackageManagerException(options.packageManager);
  62. }
  63. taskPackageManagerName = options.packageManager;
  64. }
  65. const outputStream = process.stdout;
  66. const errorStream = process.stderr;
  67. const spawnOptions = {
  68. stdio: [process.stdin, outputStream, errorStream],
  69. shell: true,
  70. cwd: path.join(rootDirectory, options.workingDirectory || ''),
  71. };
  72. const args = [];
  73. if (options.packageName) {
  74. if (options.command === 'install') {
  75. args.push(taskPackageManagerProfile.commands.installPackage);
  76. }
  77. args.push(options.packageName);
  78. }
  79. else if (options.command === 'install' && taskPackageManagerProfile.commands.installAll) {
  80. args.push(taskPackageManagerProfile.commands.installAll);
  81. }
  82. if (options.quiet && taskPackageManagerProfile.quietArgument) {
  83. args.push(taskPackageManagerProfile.quietArgument);
  84. }
  85. return new rxjs_1.Observable(obs => {
  86. child_process_1.spawn(taskPackageManagerName, args, spawnOptions)
  87. .on('close', (code) => {
  88. if (code === 0) {
  89. obs.next();
  90. obs.complete();
  91. }
  92. else {
  93. const message = 'Package install failed, see above.';
  94. obs.error(new Error(message));
  95. }
  96. });
  97. });
  98. };
  99. }
  100. exports.default = default_1;