webpack.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env node
  2. /**
  3. * @param {string} command process to run
  4. * @param {string[]} args command line arguments
  5. * @returns {Promise<void>} promise
  6. */
  7. const runCommand = (command, args) => {
  8. const cp = require("child_process");
  9. return new Promise((resolve, reject) => {
  10. const executedCommand = cp.spawn(command, args, {
  11. stdio: "inherit",
  12. shell: true
  13. });
  14. executedCommand.on("error", error => {
  15. reject(error);
  16. });
  17. executedCommand.on("exit", code => {
  18. if (code === 0) {
  19. resolve();
  20. } else {
  21. reject();
  22. }
  23. });
  24. });
  25. };
  26. /**
  27. * @param {string} packageName name of the package
  28. * @returns {boolean} is the package installed?
  29. */
  30. const isInstalled = packageName => {
  31. if (process.versions.pnp) {
  32. return true;
  33. }
  34. const path = require("path");
  35. const fs = require("graceful-fs");
  36. let dir = __dirname;
  37. do {
  38. try {
  39. if (
  40. fs.statSync(path.join(dir, "node_modules", packageName)).isDirectory()
  41. ) {
  42. return true;
  43. }
  44. } catch (_error) {
  45. // Nothing
  46. }
  47. } while (dir !== (dir = path.dirname(dir)));
  48. return false;
  49. };
  50. /**
  51. * @param {CliOption} cli options
  52. * @returns {void}
  53. */
  54. const runCli = cli => {
  55. const path = require("path");
  56. const pkgPath = require.resolve(`${cli.package}/package.json`);
  57. // eslint-disable-next-line node/no-missing-require
  58. const pkg = require(pkgPath);
  59. // eslint-disable-next-line node/no-missing-require
  60. require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]));
  61. };
  62. /**
  63. * @typedef {Object} CliOption
  64. * @property {string} name display name
  65. * @property {string} package npm package name
  66. * @property {string} binName name of the executable file
  67. * @property {boolean} installed currently installed?
  68. * @property {string} url homepage
  69. */
  70. /** @type {CliOption} */
  71. const cli = {
  72. name: "webpack-cli",
  73. package: "webpack-cli",
  74. binName: "webpack-cli",
  75. installed: isInstalled("webpack-cli"),
  76. url: "https://github.com/webpack/webpack-cli"
  77. };
  78. if (!cli.installed) {
  79. const path = require("path");
  80. const fs = require("graceful-fs");
  81. const readLine = require("readline");
  82. const notify =
  83. "CLI for webpack must be installed.\n" + ` ${cli.name} (${cli.url})\n`;
  84. console.error(notify);
  85. let packageManager;
  86. if (fs.existsSync(path.resolve(process.cwd(), "yarn.lock"))) {
  87. packageManager = "yarn";
  88. } else if (fs.existsSync(path.resolve(process.cwd(), "pnpm-lock.yaml"))) {
  89. packageManager = "pnpm";
  90. } else {
  91. packageManager = "npm";
  92. }
  93. const installOptions = [packageManager === "yarn" ? "add" : "install", "-D"];
  94. console.error(
  95. `We will use "${packageManager}" to install the CLI via "${packageManager} ${installOptions.join(
  96. " "
  97. )} ${cli.package}".`
  98. );
  99. const question = `Do you want to install 'webpack-cli' (yes/no): `;
  100. const questionInterface = readLine.createInterface({
  101. input: process.stdin,
  102. output: process.stderr
  103. });
  104. // In certain scenarios (e.g. when STDIN is not in terminal mode), the callback function will not be
  105. // executed. Setting the exit code here to ensure the script exits correctly in those cases. The callback
  106. // function is responsible for clearing the exit code if the user wishes to install webpack-cli.
  107. process.exitCode = 1;
  108. questionInterface.question(question, answer => {
  109. questionInterface.close();
  110. const normalizedAnswer = answer.toLowerCase().startsWith("y");
  111. if (!normalizedAnswer) {
  112. console.error(
  113. "You need to install 'webpack-cli' to use webpack via CLI.\n" +
  114. "You can also install the CLI manually."
  115. );
  116. return;
  117. }
  118. process.exitCode = 0;
  119. console.log(
  120. `Installing '${
  121. cli.package
  122. }' (running '${packageManager} ${installOptions.join(" ")} ${
  123. cli.package
  124. }')...`
  125. );
  126. runCommand(packageManager, installOptions.concat(cli.package))
  127. .then(() => {
  128. runCli(cli);
  129. })
  130. .catch(error => {
  131. console.error(error);
  132. process.exitCode = 1;
  133. });
  134. });
  135. } else {
  136. runCli(cli);
  137. }