Skip to content

Commit f92627e

Browse files
committed
chore: use commander built-ins over custom implementations
1 parent fc9d9f9 commit f92627e

15 files changed

Lines changed: 732 additions & 3517 deletions

File tree

.cspell.json

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,12 @@
107107
"watchpack"
108108
],
109109
"dictionaries": ["npm", "software-terms"],
110+
"useGitignore": true,
110111
"ignorePaths": [
111112
"**/CHANGELOG.md",
112-
"**/package.json",
113-
"**/dist/**",
114113
"**/__snapshots__/**",
115-
"**/*.tsbuildinfo",
116114
"**/*.png.tpl",
117-
"**/package-lock.json",
118-
"packages/*/lib/**",
119-
"node_modules",
120-
"coverage",
121-
"*.log"
115+
"**/package.json",
116+
"**/package-lock.json"
122117
]
123118
}

package-lock.json

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/configtest/src/index.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ const WEBPACK_PACKAGE = process.env.WEBPACK_PACKAGE || "webpack";
44

55
class ConfigTestCommand {
66
async apply(cli: IWebpackCLI): Promise<void> {
7-
await cli.makeCommand(
8-
{
9-
name: "configtest [config-path]",
10-
alias: "t",
11-
description: "Validate a webpack configuration.",
12-
pkg: "@webpack-cli/configtest",
13-
dependencies: [WEBPACK_PACKAGE],
14-
},
15-
[],
16-
async (configPath: string | undefined): Promise<void> => {
7+
await cli.makeCommand({
8+
name: "configtest [config-path]",
9+
alias: ["t"],
10+
description: "Validate a webpack configuration.",
11+
dependencies: [WEBPACK_PACKAGE],
12+
async action(configPath: string | undefined): Promise<void> {
1713
cli.webpack = await cli.loadWebpack();
1814

1915
const config = await cli.loadConfig(configPath ? { config: [configPath] } : {});
@@ -56,7 +52,7 @@ class ConfigTestCommand {
5652

5753
cli.logger.success("There are no validation errors in the given webpack configuration.");
5854
},
59-
);
55+
});
6056
}
6157
}
6258

packages/info/src/index.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@ import { type IWebpackCLI } from "webpack-cli";
22

33
class InfoCommand {
44
async apply(cli: IWebpackCLI): Promise<void> {
5-
await cli.makeCommand(
6-
{
7-
name: "info",
8-
alias: "i",
9-
description: "Outputs information about your system.",
10-
usage: "[options]",
11-
pkg: "@webpack-cli/info",
12-
},
13-
cli.getInfoOptions(),
14-
async (options: { output: string; additionalPackage: string[] }) => {
5+
await cli.makeCommand({
6+
name: "info",
7+
alias: ["i", "version", "v"],
8+
description: "Outputs information about your system.",
9+
options: cli.getInfoOptions().flatMap(cli.makeOption.bind(cli)),
10+
async action(options: { output: string; additionalPackage: string[] }) {
1511
const info = await cli.getInfoOutput(options);
1612

1713
cli.logger.raw(info);
1814
},
19-
);
15+
});
2016
}
2117
}
2218

packages/serve/src/index.ts

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,34 @@
11
import { type Compiler, type cli } from "webpack";
2-
import { type IWebpackCLI, type StringsKeys, type WebpackDevServerOptions } from "webpack-cli";
2+
import {
3+
type IWebpackCLI,
4+
type StringsKeys,
5+
WebpackCLICommandOption,
6+
type WebpackDevServerOptions,
7+
} from "webpack-cli";
38

49
const WEBPACK_PACKAGE = process.env.WEBPACK_PACKAGE || "webpack";
510
const WEBPACK_DEV_SERVER_PACKAGE = process.env.WEBPACK_DEV_SERVER_PACKAGE || "webpack-dev-server";
611

712
type Problem = NonNullable<ReturnType<(typeof cli)["processArguments"]>>[0];
813

914
class ServeCommand {
10-
async apply(cli: IWebpackCLI): Promise<void> {
11-
const loadDevServerOptions = () => {
12-
const devServer = require(WEBPACK_DEV_SERVER_PACKAGE);
15+
async apply(cli: IWebpackCLI, options: WebpackCLICommandOption[]): Promise<void> {
16+
const devServer = require(WEBPACK_DEV_SERVER_PACKAGE);
1317

18+
const devServerFlags = Object.entries(
1419
// eslint-disable-next-line @typescript-eslint/no-explicit-any
15-
const options: Record<string, any> = cli.webpack.cli.getArguments(devServer.schema);
16-
// New options format
17-
// { flag1: {}, flag2: {} }
18-
return Object.keys(options).map((key) => {
19-
options[key].name = key;
20-
21-
return options[key];
22-
});
23-
};
24-
25-
await cli.makeCommand(
26-
{
27-
name: "serve [entries...]",
28-
alias: ["server", "s"],
29-
description: "Run the webpack dev server and watch for source file changes while serving.",
30-
usage: "[entries...] [options]",
31-
pkg: "@webpack-cli/serve",
32-
dependencies: [WEBPACK_PACKAGE, WEBPACK_DEV_SERVER_PACKAGE],
33-
},
34-
async () => {
35-
let devServerFlags = [];
36-
37-
cli.webpack = await cli.loadWebpack();
38-
39-
try {
40-
devServerFlags = loadDevServerOptions();
41-
} catch (error) {
42-
cli.logger.error(
43-
`You need to install 'webpack-dev-server' for running 'webpack serve'.\n${error}`,
44-
);
45-
process.exit(2);
46-
}
47-
48-
const builtInOptions = cli.getBuiltInOptions();
49-
50-
return [...builtInOptions, ...devServerFlags];
51-
},
20+
cli.webpack.cli.getArguments(devServer.schema) as Record<string, any>,
21+
).map(([name, option]) => ({ name, ...option, group: "core", hidden: true }));
22+
23+
await cli.makeCommand({
24+
name: "serve [entries...]",
25+
alias: ["server", "s"],
26+
description: "Run the webpack dev server and watch for source file changes while serving.",
27+
options: [...options, ...devServerFlags.flatMap(cli.makeOption.bind(cli))],
28+
dependencies: [WEBPACK_PACKAGE, WEBPACK_DEV_SERVER_PACKAGE],
5229
// eslint-disable-next-line @typescript-eslint/no-explicit-any
53-
async (entries: string[], options: any) => {
30+
async action(entries: string[], options: any) {
5431
const builtInOptions = cli.getBuiltInOptions();
55-
let devServerFlags = [];
56-
57-
try {
58-
devServerFlags = loadDevServerOptions();
59-
} catch {
60-
// Nothing, to prevent future updates
61-
}
6232

6333
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6434
const webpackCLIOptions: Record<string, any> = {};
@@ -243,7 +213,7 @@ class ServeCommand {
243213
process.exit(2);
244214
}
245215
},
246-
);
216+
});
247217
}
248218
}
249219

packages/webpack-cli/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"commander": "^12.1.0",
4040
"cross-spawn": "^7.0.6",
4141
"envinfo": "^7.14.0",
42-
"fastest-levenshtein": "^1.0.12",
4342
"import-local": "^3.0.2",
4443
"interpret": "^3.1.1",
4544
"rechoir": "^0.8.0",

packages/webpack-cli/src/bootstrap.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ export default runCLI;
1717

1818
// TODO remove me in the next major release and use `default` export
1919
module.exports = runCLI;
20+
21+
// @ts-expect-error ...
22+
if (process.env.npm_lifecycle_script === "tsx") runCLI();

packages/webpack-cli/src/types.ts

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,8 @@ interface IWebpackCLI {
5454
tryRequireThenImport<T = unknown>(module: ModuleName, handleError: boolean): Promise<T>;
5555
getInfoOptions(): WebpackCLIBuiltInOption[];
5656
getInfoOutput(options: { output: string; additionalPackage: string[] }): Promise<string>;
57-
makeCommand(
58-
commandOptions: WebpackCLIOptions,
59-
options: WebpackCLICommandOptions,
60-
action: CommandAction,
61-
): Promise<WebpackCLICommand | undefined>;
62-
makeOption(command: WebpackCLICommand, option: WebpackCLIBuiltInOption): void;
57+
makeCommand(commandOptions: WebpackCLIOptions): Promise<WebpackCLICommand | undefined>;
58+
makeOption(option: WebpackCLIBuiltInFlag): WebpackCLICommandOption[];
6359
run(
6460
args: Parameters<WebpackCLICommand["parseOptions"]>[0],
6561
parseOptions?: ParseOptions,
@@ -80,6 +76,11 @@ interface IWebpackCLI {
8076
runWebpack(options: WebpackRunOptions, isWatchCommand: boolean): Promise<void>;
8177
}
8278

79+
declare interface WebpackCallback {
80+
(err: null | Error, result?: Stats): void;
81+
(err: null | Error, result?: MultiStats): void;
82+
}
83+
8384
interface WebpackCLIColors extends Colors {
8485
isColorSupported: boolean;
8586
}
@@ -118,18 +119,13 @@ type WebpackCLIMainOption = Pick<
118119

119120
interface WebpackCLIOptions extends CommandOptions {
120121
name: string;
121-
alias: string | string[];
122-
description?: string;
123-
usage?: string;
122+
alias: string[];
123+
description: string;
124124
dependencies?: string[];
125-
pkg?: string;
126-
argsDescription?: Record<string, string>;
125+
options?: Option[];
126+
action: CommandAction;
127127
}
128128

129-
type WebpackCLICommandOptions =
130-
| WebpackCLIBuiltInOption[]
131-
| (() => Promise<WebpackCLIBuiltInOption[]>);
132-
133129
interface WebpackCLIBuiltInFlag {
134130
name: string;
135131
alias?: string;
@@ -145,18 +141,13 @@ interface WebpackCLIBuiltInFlag {
145141
describe?: string;
146142
negatedDescription?: string;
147143
defaultValue?: string;
148-
helpLevel: "minimum" | "verbose";
144+
hidden: boolean;
149145
}
150146

151147
interface WebpackCLIBuiltInOption extends WebpackCLIBuiltInFlag {
152-
hidden?: boolean;
153148
group?: "core";
154149
}
155150

156-
type WebpackCLIExternalCommandInfo = Pick<WebpackCLIOptions, "name" | "alias" | "description"> & {
157-
pkg: string;
158-
};
159-
160151
/**
161152
* Webpack dev server
162153
*/
@@ -254,7 +245,7 @@ type PotentialPromise<T> = T | Promise<T>;
254245
type ModuleName = string;
255246
type Path = string;
256247
// eslint-disable-next-line @typescript-eslint/no-explicit-any
257-
type LogHandler = (value: any) => void;
248+
type LogHandler = (value: any, raw?: boolean) => void;
258249
type StringFormatter = (value: string) => string;
259250

260251
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -313,7 +304,6 @@ export {
313304
type CommandAction,
314305
type CommanderOption,
315306
type DynamicImport,
316-
type EnumValue,
317307
type FileSystemCacheOptions,
318308
type IWebpackCLI,
319309
type ImportLoaderError,
@@ -335,9 +325,7 @@ export {
335325
type WebpackCLIColors,
336326
type WebpackCLICommand,
337327
type WebpackCLICommandOption,
338-
type WebpackCLICommandOptions,
339328
type WebpackCLIConfig,
340-
type WebpackCLIExternalCommandInfo,
341329
type WebpackCLILogger,
342330
type WebpackCLIMainOption,
343331
type WebpackCLIOptions,

0 commit comments

Comments
 (0)