packageManager.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.init = init;
  6. exports.install = install;
  7. exports.installDev = installDev;
  8. exports.uninstall = uninstall;
  9. exports.installAll = installAll;
  10. function _execa() {
  11. const data = _interopRequireDefault(require("execa"));
  12. _execa = function () {
  13. return data;
  14. };
  15. return data;
  16. }
  17. function _cliTools() {
  18. const data = require("@react-native-community/cli-tools");
  19. _cliTools = function () {
  20. return data;
  21. };
  22. return data;
  23. }
  24. var _yarn = require("./yarn");
  25. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  26. const packageManagers = {
  27. yarn: {
  28. init: ['init', '-y'],
  29. install: ['add'],
  30. installDev: ['add', '-D'],
  31. uninstall: ['remove'],
  32. installAll: ['install']
  33. },
  34. npm: {
  35. init: ['init', '-y'],
  36. install: ['install', '--save', '--save-exact'],
  37. installDev: ['install', '--save-dev', '--save-exact'],
  38. uninstall: ['uninstall', '--save'],
  39. installAll: ['install']
  40. }
  41. };
  42. function configurePackageManager(packageNames, action, options) {
  43. const pm = shouldUseYarn(options) ? 'yarn' : 'npm';
  44. const [executable, ...flags] = packageManagers[pm][action];
  45. const args = [executable, ...flags, ...packageNames];
  46. return executeCommand(pm, args, options);
  47. }
  48. function executeCommand(command, args, options) {
  49. return (0, _execa().default)(command, args, {
  50. stdio: options.silent && !_cliTools().logger.isVerbose() ? 'pipe' : 'inherit',
  51. cwd: options.root
  52. });
  53. }
  54. function shouldUseYarn(options) {
  55. if (options && options.preferYarn !== undefined) {
  56. return options.preferYarn && (0, _yarn.getYarnVersionIfAvailable)();
  57. }
  58. return (0, _yarn.isProjectUsingYarn)(options.root) && (0, _yarn.getYarnVersionIfAvailable)();
  59. }
  60. function init(options) {
  61. return configurePackageManager([], 'init', options);
  62. }
  63. function install(packageNames, options) {
  64. return configurePackageManager(packageNames, 'install', options);
  65. }
  66. function installDev(packageNames, options) {
  67. return configurePackageManager(packageNames, 'installDev', options);
  68. }
  69. function uninstall(packageNames, options) {
  70. return configurePackageManager(packageNames, 'uninstall', options);
  71. }
  72. function installAll(options) {
  73. return configurePackageManager([], 'installAll', options);
  74. }
  75. //# sourceMappingURL=packageManager.js.map