install.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.install = install;
  6. var _chalk = _interopRequireDefault(require("next/dist/compiled/chalk"));
  7. var _crossSpawn = _interopRequireDefault(require("next/dist/compiled/cross-spawn"));
  8. function _interopRequireDefault(obj) {
  9. return obj && obj.__esModule ? obj : {
  10. default: obj
  11. };
  12. }
  13. function install(root, dependencies, { packageManager , isOnline , devDependencies }) {
  14. /**
  15. * (p)npm-specific command-line flags.
  16. */ const npmFlags = [];
  17. /**
  18. * Yarn-specific command-line flags.
  19. */ const yarnFlags = [];
  20. /**
  21. * Return a Promise that resolves once the installation is finished.
  22. */ return new Promise((resolve, reject)=>{
  23. let args;
  24. let command = packageManager;
  25. const useYarn = packageManager === "yarn";
  26. if (dependencies && dependencies.length) {
  27. /**
  28. * If there are dependencies, run a variation of `{packageManager} add`.
  29. */ if (useYarn) {
  30. /**
  31. * Call `yarn add --exact (--offline)? (-D)? ...`.
  32. */ args = [
  33. "add",
  34. "--exact"
  35. ];
  36. if (!isOnline) args.push("--offline");
  37. args.push("--cwd", root);
  38. if (devDependencies) args.push("--dev");
  39. args.push(...dependencies);
  40. } else {
  41. /**
  42. * Call `(p)npm install [--save|--save-dev] ...`.
  43. */ args = [
  44. "install",
  45. "--save-exact"
  46. ];
  47. args.push(devDependencies ? "--save-dev" : "--save");
  48. args.push(...dependencies);
  49. }
  50. } else {
  51. /**
  52. * If there are no dependencies, run a variation of `{packageManager}
  53. * install`.
  54. */ args = [
  55. "install"
  56. ];
  57. if (!isOnline) {
  58. console.log(_chalk.default.yellow("You appear to be offline."));
  59. if (useYarn) {
  60. console.log(_chalk.default.yellow("Falling back to the local Yarn cache."));
  61. console.log();
  62. args.push("--offline");
  63. } else {
  64. console.log();
  65. }
  66. }
  67. }
  68. /**
  69. * Add any package manager-specific flags.
  70. */ if (useYarn) {
  71. args.push(...yarnFlags);
  72. } else {
  73. args.push(...npmFlags);
  74. }
  75. /**
  76. * Spawn the installation process.
  77. */ const child = (0, _crossSpawn).default(command, args, {
  78. stdio: "inherit",
  79. env: {
  80. ...process.env,
  81. ADBLOCK: "1",
  82. // we set NODE_ENV to development as pnpm skips dev
  83. // dependencies when production
  84. NODE_ENV: "development",
  85. DISABLE_OPENCOLLECTIVE: "1"
  86. }
  87. });
  88. child.on("close", (code)=>{
  89. if (code !== 0) {
  90. reject({
  91. command: `${command} ${args.join(" ")}`
  92. });
  93. return;
  94. }
  95. resolve();
  96. });
  97. });
  98. }
  99. //# sourceMappingURL=install.js.map