Skip to content

Commit f483370

Browse files
refactor: code
1 parent 06cbe46 commit f483370

File tree

1 file changed

+21
-32
lines changed

1 file changed

+21
-32
lines changed

packages/webpack-cli/src/webpack-cli.ts

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import util from "node:util";
66
import { type stringifyChunked as stringifyChunkedType } from "@discoveryjs/json-ext";
77
import {
88
type Argument,
9-
type Command,
9+
type Command as CommanderCommand,
1010
type CommandOptions as CommanderCommandOptions,
1111
type Help,
1212
Option,
1313
type ParseOptions,
1414
program,
1515
} from "commander";
1616
import { type Config as EnvinfoConfig, type Options as EnvinfoOptions } from "envinfo";
17+
import { distance } from "fastest-levenshtein";
1718
import { type prepare } from "rechoir";
1819
import {
1920
type Argument as WebpackArgument,
@@ -78,6 +79,11 @@ interface Colors extends WebpackColors {
7879
isColorSupported: boolean;
7980
}
8081

82+
interface Command extends CommanderCommand {
83+
pkg?: string;
84+
forHelp?: boolean;
85+
}
86+
8187
interface CommandOptions extends CommanderCommandOptions {
8288
rawName: string;
8389
name: string;
@@ -86,7 +92,6 @@ interface CommandOptions extends CommanderCommandOptions {
8692
usage?: string;
8793
dependencies?: string[];
8894
pkg?: string;
89-
argsDescription?: Record<string, string>;
9095
external?: boolean;
9196
}
9297

@@ -501,9 +506,7 @@ class WebpackCLI {
501506
action: Parameters<Command["action"]>[0],
502507
): Promise<Command | undefined> {
503508
const alreadyLoaded = this.program.commands.find(
504-
(command) =>
505-
command.name() === commandOptions.rawName ||
506-
command.aliases().includes(commandOptions.alias as string),
509+
(command) => command.name() === commandOptions.rawName,
507510
);
508511

509512
if (alreadyLoaded) {
@@ -513,10 +516,10 @@ class WebpackCLI {
513516
const command = this.program.command(commandOptions.name, {
514517
hidden: commandOptions.hidden,
515518
isDefault: commandOptions.isDefault,
516-
});
519+
}) as Command;
517520

518521
if (commandOptions.description) {
519-
command.description(commandOptions.description, commandOptions.argsDescription!);
522+
command.description(commandOptions.description);
520523
}
521524

522525
if (commandOptions.usage) {
@@ -529,10 +532,9 @@ class WebpackCLI {
529532
command.alias(commandOptions.alias);
530533
}
531534

532-
// TODO search API for this
533-
(command as Command & { pkg: string }).pkg = commandOptions.pkg || "webpack-cli";
535+
command.pkg = commandOptions.pkg || "webpack-cli";
534536

535-
const { forHelp } = this.program as Command & { forHelp?: boolean };
537+
const { forHelp } = this.program;
536538

537539
let allDependenciesInstalled = true;
538540

@@ -1609,7 +1611,7 @@ class WebpackCLI {
16091611

16101612
webpackCLIOptions.isWatchingLikeCommand = true;
16111613

1612-
const compiler = await this.createCompiler(webpackCLIOptions as Options);
1614+
const compiler = await this.createCompiler(webpackCLIOptions);
16131615

16141616
if (!compiler) {
16151617
return;
@@ -1881,8 +1883,8 @@ class WebpackCLI {
18811883

18821884
let pkg: string;
18831885

1884-
if (builtInExternalCommandInfo) {
1885-
({ pkg } = builtInExternalCommandInfo as CommandOptions & { pkg: string });
1886+
if (builtInExternalCommandInfo && builtInExternalCommandInfo.pkg) {
1887+
({ pkg } = builtInExternalCommandInfo);
18861888
} else {
18871889
pkg = commandName;
18881890
}
@@ -1906,10 +1908,10 @@ class WebpackCLI {
19061908
ConstructorParameters extends unknown[] = unknown[],
19071909
> = new (...args: ConstructorParameters) => InstanceType;
19081910

1909-
let loadedCommand: Instantiable<() => void>;
1911+
let LoadedCommand: Instantiable<() => void>;
19101912

19111913
try {
1912-
loadedCommand = (await import(pkg)).default;
1914+
LoadedCommand = (await import(pkg)).default;
19131915
} catch {
19141916
// Ignore, command is not installed
19151917
return;
@@ -1918,8 +1920,7 @@ class WebpackCLI {
19181920
let command;
19191921

19201922
try {
1921-
// eslint-disable-next-line new-cap
1922-
command = new loadedCommand();
1923+
command = new LoadedCommand();
19231924

19241925
await command.apply(this);
19251926
} catch (error) {
@@ -1942,13 +1943,6 @@ class WebpackCLI {
19421943
return;
19431944
}
19441945

1945-
const isInfo = ["commander.helpDisplayed", "commander.version"].includes(error.code);
1946-
1947-
if (isInfo) {
1948-
process.exit(0);
1949-
return;
1950-
}
1951-
19521946
if (error.code === "commander.unknownOption") {
19531947
let name = error.message.match(/'(.+)'/) as string | null;
19541948

@@ -1972,9 +1966,7 @@ class WebpackCLI {
19721966
process.exit(2);
19731967
}
19741968

1975-
const { distance } = require("fastest-levenshtein");
1976-
1977-
for (const option of (command as Command).options) {
1969+
for (const option of command.options) {
19781970
if (
19791971
!(option as Option & { internal?: boolean }).internal &&
19801972
distance(name, option.long?.slice(2) as string) < 3
@@ -1988,8 +1980,6 @@ class WebpackCLI {
19881980

19891981
this.logger.error("Run 'webpack --help' to see available commands and options");
19901982
process.exit(2);
1991-
1992-
throw error;
19931983
});
19941984

19951985
this.program.option("--color", "Enable colors on console.");
@@ -2047,7 +2037,7 @@ class WebpackCLI {
20472037
isVerbose = true;
20482038
}
20492039

2050-
(this.program as Command & { forHelp?: boolean }).forHelp = true;
2040+
this.program.forHelp = true;
20512041

20522042
const optionsForHelp = [
20532043
...(isHelpOption && hasOperand ? [operand] : []),
@@ -2116,8 +2106,6 @@ class WebpackCLI {
21162106
} else {
21172107
this.logger.error(`Unknown command or entry '${operand}'`);
21182108

2119-
const { distance } = await import("fastest-levenshtein");
2120-
21212109
const found = Object.values(WebpackCLI.#commands).find(
21222110
(commandOptions) => distance(operand, commandOptions.rawName) < 3,
21232111
);
@@ -2839,6 +2827,7 @@ class WebpackCLI {
28392827
});
28402828
}
28412829
} else {
2830+
// TODO bug on webpack side
28422831
const printedStats = stats.toString(statsOptions as StatsOptions);
28432832

28442833
// Avoid extra empty line when `stats: 'none'`

0 commit comments

Comments
 (0)