npm.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.pkgFromRegistry = exports.pkgManagerArgs = void 0;
  4. const utils_subprocess_1 = require("@ionic/utils-subprocess");
  5. /**
  6. * Resolves pkg manager intent with command args.
  7. *
  8. * TODO: this is a weird function and should be split up
  9. *
  10. * @return Promise<args> If the args is an empty array, it means the pkg manager doesn't have that command.
  11. */
  12. async function pkgManagerArgs(npmClient, options) {
  13. let vocab;
  14. const cmd = options.command;
  15. if (cmd === 'dedupe') {
  16. delete options.pkg;
  17. }
  18. if (cmd === 'dedupe' || cmd === 'rebuild') {
  19. delete options.global;
  20. delete options.save;
  21. delete options.saveDev;
  22. }
  23. if (cmd === 'dedupe' || cmd === 'rebuild' || cmd === 'uninstall') {
  24. delete options.saveExact;
  25. }
  26. if (cmd === 'install' || cmd === 'uninstall') {
  27. if (options.global) { // Turn off all save flags for global context
  28. options.save = false;
  29. options.saveDev = false;
  30. options.saveExact = false;
  31. }
  32. else if (options.pkg && typeof options.save === 'undefined' && typeof options.saveDev === 'undefined') { // Prefer save flag
  33. options.save = true;
  34. }
  35. if (cmd === 'install' && options.pkg && typeof options.saveExact === 'undefined') { // For single package installs, prefer to save exact versions
  36. options.saveExact = true;
  37. }
  38. }
  39. const installerArgs = [];
  40. switch (npmClient) {
  41. case 'npm':
  42. vocab = { run: 'run', install: 'i', bareInstall: 'i', uninstall: 'uninstall', dedupe: 'dedupe', rebuild: 'rebuild', global: '-g', save: '--save', saveDev: '-D', saveExact: '-E', nonInteractive: '', lockFileOnly: '--package-lock-only' };
  43. break;
  44. case 'yarn':
  45. vocab = { run: 'run', install: 'add', bareInstall: 'install', uninstall: 'remove', dedupe: '', rebuild: 'install', global: '', save: '', saveDev: '--dev', saveExact: '--exact', nonInteractive: '--non-interactive', lockFileOnly: '' };
  46. if (options.global) { // yarn installs packages globally under the 'global' prefix, instead of having a flag
  47. installerArgs.push('global');
  48. }
  49. break;
  50. case 'pnpm':
  51. vocab = { run: 'run', install: 'add', bareInstall: 'install', uninstall: 'remove', dedupe: '', rebuild: 'rebuild', global: '--global', save: '', saveDev: '--save-dev', saveExact: '--save-exact', nonInteractive: '', lockFileOnly: '--lockfile-only' };
  52. break;
  53. default:
  54. throw new Error(`unknown installer: ${npmClient}`);
  55. }
  56. if (cmd === 'install') {
  57. if (options.pkg) {
  58. installerArgs.push(vocab.install);
  59. }
  60. else {
  61. installerArgs.push(vocab.bareInstall);
  62. }
  63. if (options.lockFileOnly) {
  64. installerArgs.push(vocab.lockFileOnly);
  65. }
  66. }
  67. else if (cmd === 'uninstall') {
  68. installerArgs.push(vocab.uninstall);
  69. }
  70. else if (cmd === 'dedupe') {
  71. if (vocab.dedupe) {
  72. installerArgs.push(vocab.dedupe);
  73. }
  74. else {
  75. return [];
  76. }
  77. }
  78. else if (cmd === 'rebuild') {
  79. installerArgs.push(vocab.rebuild);
  80. }
  81. else {
  82. installerArgs.push(cmd);
  83. }
  84. if (options.global && vocab.global) {
  85. installerArgs.push(vocab.global);
  86. }
  87. if (options.save && vocab.save) {
  88. installerArgs.push(vocab.save);
  89. }
  90. if (options.saveDev && vocab.saveDev) {
  91. installerArgs.push(vocab.saveDev);
  92. }
  93. if (options.saveExact && vocab.saveExact) {
  94. installerArgs.push(vocab.saveExact);
  95. }
  96. if (vocab.nonInteractive) { // Some CLIs offer a flag that disables all interactivity, which we want to opt-into
  97. installerArgs.push(vocab.nonInteractive);
  98. }
  99. if (options.pkg) {
  100. if (typeof options.pkg === 'string') {
  101. installerArgs.push(options.pkg);
  102. }
  103. if (Array.isArray(options.pkg)) {
  104. installerArgs.push(...options.pkg);
  105. }
  106. }
  107. if (cmd === 'run' && options.script) {
  108. installerArgs.push(options.script);
  109. }
  110. if (npmClient === 'yarn') {
  111. if (cmd === 'rebuild') {
  112. installerArgs.push('--force');
  113. }
  114. }
  115. if (cmd === 'run' && options.script && options.scriptArgs && options.scriptArgs.length > 0) {
  116. if (npmClient === 'npm' || npmClient === 'pnpm') {
  117. installerArgs.push('--');
  118. }
  119. for (const arg of options.scriptArgs) {
  120. installerArgs.push(arg);
  121. }
  122. }
  123. if (options.json) {
  124. installerArgs.push('--json');
  125. }
  126. return [npmClient, ...installerArgs];
  127. }
  128. exports.pkgManagerArgs = pkgManagerArgs;
  129. /**
  130. * @return Promise<package.json on registry or `undefined`>
  131. */
  132. async function pkgFromRegistry(npmClient, options) {
  133. const [manager, ...managerArgs] = await pkgManagerArgs(npmClient, { command: 'info', json: true, ...options });
  134. const cmd = new utils_subprocess_1.Subprocess(manager, managerArgs);
  135. const result = await cmd.output();
  136. if (result) {
  137. const json = JSON.parse(result);
  138. return manager === 'yarn' ? json.data : json;
  139. }
  140. }
  141. exports.pkgFromRegistry = pkgFromRegistry;