| 1 | #!/usr/bin/env node
|
|---|
| 2 | /* Based on webpack/bin/webpack.js */
|
|---|
| 3 | /* eslint-disable no-console */
|
|---|
| 4 |
|
|---|
| 5 | "use strict";
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * @param {string} command process to run
|
|---|
| 9 | * @param {string[]} args command line arguments
|
|---|
| 10 | * @returns {Promise<void>} promise
|
|---|
| 11 | */
|
|---|
| 12 | const runCommand = (command, args) => {
|
|---|
| 13 | const cp = require("child_process");
|
|---|
| 14 | return new Promise((resolve, reject) => {
|
|---|
| 15 | const executedCommand = cp.spawn(command, args, {
|
|---|
| 16 | stdio: "inherit",
|
|---|
| 17 | shell: true,
|
|---|
| 18 | });
|
|---|
| 19 |
|
|---|
| 20 | executedCommand.on("error", (error) => {
|
|---|
| 21 | reject(error);
|
|---|
| 22 | });
|
|---|
| 23 |
|
|---|
| 24 | executedCommand.on("exit", (code) => {
|
|---|
| 25 | if (code === 0) {
|
|---|
| 26 | resolve();
|
|---|
| 27 | } else {
|
|---|
| 28 | reject();
|
|---|
| 29 | }
|
|---|
| 30 | });
|
|---|
| 31 | });
|
|---|
| 32 | };
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * @param {string} packageName name of the package
|
|---|
| 36 | * @returns {boolean} is the package installed?
|
|---|
| 37 | */
|
|---|
| 38 | const isInstalled = (packageName) => {
|
|---|
| 39 | if (process.versions.pnp) {
|
|---|
| 40 | return true;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | const path = require("path");
|
|---|
| 44 | const fs = require("graceful-fs");
|
|---|
| 45 |
|
|---|
| 46 | let dir = __dirname;
|
|---|
| 47 |
|
|---|
| 48 | do {
|
|---|
| 49 | try {
|
|---|
| 50 | if (
|
|---|
| 51 | fs.statSync(path.join(dir, "node_modules", packageName)).isDirectory()
|
|---|
| 52 | ) {
|
|---|
| 53 | return true;
|
|---|
| 54 | }
|
|---|
| 55 | } catch (_error) {
|
|---|
| 56 | // Nothing
|
|---|
| 57 | }
|
|---|
| 58 | // eslint-disable-next-line no-cond-assign
|
|---|
| 59 | } while (dir !== (dir = path.dirname(dir)));
|
|---|
| 60 |
|
|---|
| 61 | // https://github.com/nodejs/node/blob/v18.9.1/lib/internal/modules/cjs/loader.js#L1274
|
|---|
| 62 | // @ts-ignore
|
|---|
| 63 | for (const internalPath of require("module").globalPaths) {
|
|---|
| 64 | try {
|
|---|
| 65 | if (fs.statSync(path.join(internalPath, packageName)).isDirectory()) {
|
|---|
| 66 | return true;
|
|---|
| 67 | }
|
|---|
| 68 | } catch (_error) {
|
|---|
| 69 | // Nothing
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | return false;
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | * @param {CliOption} cli options
|
|---|
| 78 | * @returns {void}
|
|---|
| 79 | */
|
|---|
| 80 | const runCli = (cli) => {
|
|---|
| 81 | if (cli.preprocess) {
|
|---|
| 82 | cli.preprocess();
|
|---|
| 83 | }
|
|---|
| 84 | const path = require("path");
|
|---|
| 85 | const pkgPath = require.resolve(`${cli.package}/package.json`);
|
|---|
| 86 | // eslint-disable-next-line import/no-dynamic-require
|
|---|
| 87 | const pkg = require(pkgPath);
|
|---|
| 88 |
|
|---|
| 89 | if (pkg.type === "module" || /\.mjs/i.test(pkg.bin[cli.binName])) {
|
|---|
| 90 | import(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName])).catch(
|
|---|
| 91 | (error) => {
|
|---|
| 92 | console.error(error);
|
|---|
| 93 | process.exitCode = 1;
|
|---|
| 94 | }
|
|---|
| 95 | );
|
|---|
| 96 | } else {
|
|---|
| 97 | // eslint-disable-next-line import/no-dynamic-require
|
|---|
| 98 | require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]));
|
|---|
| 99 | }
|
|---|
| 100 | };
|
|---|
| 101 |
|
|---|
| 102 | /**
|
|---|
| 103 | * @typedef {Object} CliOption
|
|---|
| 104 | * @property {string} name display name
|
|---|
| 105 | * @property {string} package npm package name
|
|---|
| 106 | * @property {string} binName name of the executable file
|
|---|
| 107 | * @property {boolean} installed currently installed?
|
|---|
| 108 | * @property {string} url homepage
|
|---|
| 109 | * @property {function} preprocess preprocessor
|
|---|
| 110 | */
|
|---|
| 111 |
|
|---|
| 112 | /** @type {CliOption} */
|
|---|
| 113 | const cli = {
|
|---|
| 114 | name: "webpack-cli",
|
|---|
| 115 | package: "webpack-cli",
|
|---|
| 116 | binName: "webpack-cli",
|
|---|
| 117 | installed: isInstalled("webpack-cli"),
|
|---|
| 118 | url: "https://github.com/webpack/webpack-cli",
|
|---|
| 119 | preprocess() {
|
|---|
| 120 | process.argv.splice(2, 0, "serve");
|
|---|
| 121 | },
|
|---|
| 122 | };
|
|---|
| 123 |
|
|---|
| 124 | if (!cli.installed) {
|
|---|
| 125 | const path = require("path");
|
|---|
| 126 | const fs = require("graceful-fs");
|
|---|
| 127 | const readLine = require("readline");
|
|---|
| 128 |
|
|---|
| 129 | const notify = `CLI for webpack must be installed.\n ${cli.name} (${cli.url})\n`;
|
|---|
| 130 |
|
|---|
| 131 | console.error(notify);
|
|---|
| 132 |
|
|---|
| 133 | /**
|
|---|
| 134 | * @type {string}
|
|---|
| 135 | */
|
|---|
| 136 | let packageManager;
|
|---|
| 137 |
|
|---|
| 138 | if (fs.existsSync(path.resolve(process.cwd(), "yarn.lock"))) {
|
|---|
| 139 | packageManager = "yarn";
|
|---|
| 140 | } else if (fs.existsSync(path.resolve(process.cwd(), "pnpm-lock.yaml"))) {
|
|---|
| 141 | packageManager = "pnpm";
|
|---|
| 142 | } else {
|
|---|
| 143 | packageManager = "npm";
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | const installOptions = [packageManager === "yarn" ? "add" : "install", "-D"];
|
|---|
| 147 |
|
|---|
| 148 | console.error(
|
|---|
| 149 | `We will use "${packageManager}" to install the CLI via "${packageManager} ${installOptions.join(
|
|---|
| 150 | " "
|
|---|
| 151 | )} ${cli.package}".`
|
|---|
| 152 | );
|
|---|
| 153 |
|
|---|
| 154 | const question = `Do you want to install 'webpack-cli' (yes/no): `;
|
|---|
| 155 |
|
|---|
| 156 | const questionInterface = readLine.createInterface({
|
|---|
| 157 | input: process.stdin,
|
|---|
| 158 | output: process.stderr,
|
|---|
| 159 | });
|
|---|
| 160 |
|
|---|
| 161 | // In certain scenarios (e.g. when STDIN is not in terminal mode), the callback function will not be
|
|---|
| 162 | // executed. Setting the exit code here to ensure the script exits correctly in those cases. The callback
|
|---|
| 163 | // function is responsible for clearing the exit code if the user wishes to install webpack-cli.
|
|---|
| 164 | process.exitCode = 1;
|
|---|
| 165 | questionInterface.question(question, (answer) => {
|
|---|
| 166 | questionInterface.close();
|
|---|
| 167 |
|
|---|
| 168 | const normalizedAnswer = answer.toLowerCase().startsWith("y");
|
|---|
| 169 |
|
|---|
| 170 | if (!normalizedAnswer) {
|
|---|
| 171 | console.error(
|
|---|
| 172 | "You need to install 'webpack-cli' to use webpack via CLI.\n" +
|
|---|
| 173 | "You can also install the CLI manually."
|
|---|
| 174 | );
|
|---|
| 175 |
|
|---|
| 176 | return;
|
|---|
| 177 | }
|
|---|
| 178 | process.exitCode = 0;
|
|---|
| 179 |
|
|---|
| 180 | console.log(
|
|---|
| 181 | `Installing '${
|
|---|
| 182 | cli.package
|
|---|
| 183 | }' (running '${packageManager} ${installOptions.join(" ")} ${
|
|---|
| 184 | cli.package
|
|---|
| 185 | }')...`
|
|---|
| 186 | );
|
|---|
| 187 |
|
|---|
| 188 | runCommand(packageManager, installOptions.concat(cli.package))
|
|---|
| 189 | .then(() => {
|
|---|
| 190 | runCli(cli);
|
|---|
| 191 | })
|
|---|
| 192 | .catch((error) => {
|
|---|
| 193 | console.error(error);
|
|---|
| 194 | process.exitCode = 1;
|
|---|
| 195 | });
|
|---|
| 196 | });
|
|---|
| 197 | } else {
|
|---|
| 198 | runCli(cli);
|
|---|
| 199 | }
|
|---|