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