webpack/bin/webpack.js

193 lines
4.4 KiB
JavaScript
Raw Normal View History

2012-03-10 20:11:23 +08:00
#!/usr/bin/env node
2018-05-15 22:56:15 +08:00
"use strict";
2018-05-15 22:56:15 +08:00
/**
* @param {string} command process to run
2020-03-13 00:51:26 +08:00
* @param {string[]} args command line arguments
2018-05-15 22:56:15 +08:00
* @returns {Promise<void>} promise
*/
const runCommand = (command, args) => {
const cp = require("child_process");
return new Promise((resolve, reject) => {
2018-05-15 22:56:15 +08:00
const executedCommand = cp.spawn(command, args, {
2018-03-29 23:06:10 +08:00
stdio: "inherit",
shell: true
});
executedCommand.on("error", error => {
reject(error);
});
executedCommand.on("exit", code => {
if (code === 0) {
2018-05-15 22:56:15 +08:00
resolve();
} else {
reject();
}
});
});
2018-05-15 22:56:15 +08:00
};
2012-03-10 20:11:23 +08:00
2018-05-15 22:56:15 +08:00
/**
* @param {string} packageName name of the package
* @returns {boolean} is the package installed?
*/
const isInstalled = packageName => {
2021-06-07 22:34:01 +08:00
if (process.versions.pnp) {
2018-05-09 20:00:15 +08:00
return true;
}
2021-06-07 22:34:01 +08:00
const path = require("path");
const fs = require("graceful-fs");
let dir = __dirname;
do {
try {
if (
fs.statSync(path.join(dir, "node_modules", packageName)).isDirectory()
) {
return true;
}
} catch (_error) {
// Nothing
}
} while (dir !== (dir = path.dirname(dir)));
2022-09-28 10:47:06 +08:00
// https://github.com/nodejs/node/blob/v18.9.1/lib/internal/modules/cjs/loader.js#L1274
// eslint-disable-next-line no-warning-comments
// @ts-ignore
for (const internalPath of require("module").globalPaths) {
try {
if (fs.statSync(path.join(internalPath, packageName)).isDirectory()) {
return true;
}
} catch (_error) {
// Nothing
}
}
2021-06-07 22:34:01 +08:00
return false;
2018-05-15 22:56:15 +08:00
};
2020-10-28 19:10:10 +08:00
/**
* @param {CliOption} cli options
* @returns {void}
*/
const runCli = cli => {
const path = require("path");
2020-10-28 19:10:10 +08:00
const pkgPath = require.resolve(`${cli.package}/package.json`);
2020-10-28 19:10:10 +08:00
const pkg = require(pkgPath);
2023-04-28 20:49:05 +08:00
if (pkg.type === "module" || /\.mjs/i.test(pkg.bin[cli.binName])) {
import(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName])).catch(
2024-07-31 15:37:05 +08:00
err => {
console.error(err);
2023-04-28 20:49:05 +08:00
process.exitCode = 1;
}
);
} else {
require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]));
}
2020-10-28 19:10:10 +08:00
};
2018-05-15 22:56:15 +08:00
/**
2024-06-11 21:09:50 +08:00
* @typedef {object} CliOption
2018-05-15 22:56:15 +08:00
* @property {string} name display name
* @property {string} package npm package name
* @property {string} binName name of the executable file
2018-05-15 22:56:15 +08:00
* @property {boolean} installed currently installed?
* @property {string} url homepage
*/
2019-03-29 05:57:38 +08:00
/** @type {CliOption} */
const cli = {
name: "webpack-cli",
package: "webpack-cli",
binName: "webpack-cli",
installed: isInstalled("webpack-cli"),
url: "https://github.com/webpack/webpack-cli"
};
2018-05-15 22:56:15 +08:00
2019-03-29 05:57:38 +08:00
if (!cli.installed) {
const path = require("path");
const fs = require("graceful-fs");
const readLine = require("readline");
2018-05-09 20:00:15 +08:00
2024-07-31 09:37:24 +08:00
const notify = `CLI for webpack must be installed.\n ${cli.name} (${cli.url})\n`;
2018-05-09 20:00:15 +08:00
console.error(notify);
2024-02-21 22:55:02 +08:00
/** @type {string | undefined} */
let packageManager;
if (fs.existsSync(path.resolve(process.cwd(), "yarn.lock"))) {
packageManager = "yarn";
} else if (fs.existsSync(path.resolve(process.cwd(), "pnpm-lock.yaml"))) {
packageManager = "pnpm";
} else {
packageManager = "npm";
}
const installOptions = [packageManager === "yarn" ? "add" : "install", "-D"];
2018-05-15 22:56:15 +08:00
console.error(
`We will use "${packageManager}" to install the CLI via "${packageManager} ${installOptions.join(
" "
2021-01-06 20:31:40 +08:00
)} ${cli.package}".`
2018-05-15 22:56:15 +08:00
);
2024-07-31 12:23:44 +08:00
const question = "Do you want to install 'webpack-cli' (yes/no): ";
const questionInterface = readLine.createInterface({
input: process.stdin,
2018-05-15 22:56:15 +08:00
output: process.stderr
});
// In certain scenarios (e.g. when STDIN is not in terminal mode), the callback function will not be
// executed. Setting the exit code here to ensure the script exits correctly in those cases. The callback
// function is responsible for clearing the exit code if the user wishes to install webpack-cli.
process.exitCode = 1;
questionInterface.question(question, answer => {
2018-03-13 16:36:10 +08:00
questionInterface.close();
2018-05-09 20:00:15 +08:00
const normalizedAnswer = answer.toLowerCase().startsWith("y");
2018-05-09 20:00:15 +08:00
2018-05-15 22:56:15 +08:00
if (!normalizedAnswer) {
2018-05-09 20:00:15 +08:00
console.error(
2018-09-05 20:01:44 +08:00
"You need to install 'webpack-cli' to use webpack via CLI.\n" +
"You can also install the CLI manually."
2018-05-09 20:00:15 +08:00
);
return;
}
process.exitCode = 0;
2018-05-09 20:00:15 +08:00
console.log(
2019-03-29 05:57:38 +08:00
`Installing '${
cli.package
}' (running '${packageManager} ${installOptions.join(" ")} ${
cli.package
}')...`
2018-05-09 20:00:15 +08:00
);
2024-02-21 22:55:02 +08:00
runCommand(
/** @type {string} */ (packageManager),
installOptions.concat(cli.package)
)
2018-05-15 22:56:15 +08:00
.then(() => {
2020-10-28 19:10:10 +08:00
runCli(cli);
2018-05-09 20:00:15 +08:00
})
2024-07-31 15:37:05 +08:00
.catch(err => {
console.error(err);
2018-05-09 20:00:15 +08:00
process.exitCode = 1;
});
});
2019-03-29 05:57:38 +08:00
} else {
2020-10-28 19:10:10 +08:00
runCli(cli);
}