1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
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 |
|
36 |
|
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 |
|
57 | }
|
58 |
|
59 | } while (dir !== (dir = path.dirname(dir)));
|
60 |
|
61 |
|
62 |
|
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 |
|
70 | }
|
71 | }
|
72 |
|
73 | return false;
|
74 | };
|
75 |
|
76 |
|
77 |
|
78 |
|
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 |
|
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 |
|
98 | require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]));
|
99 | }
|
100 | };
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
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 |
|
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 |
|
162 |
|
163 |
|
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 | }
|