Skip to content

Commit cd39fe6

Browse files
refactor: code update
1 parent 06cbe46 commit cd39fe6

1 file changed

Lines changed: 19 additions & 55 deletions

File tree

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

Lines changed: 19 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ 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,
@@ -78,6 +78,11 @@ interface Colors extends WebpackColors {
7878
isColorSupported: boolean;
7979
}
8080

81+
interface Command extends CommanderCommand {
82+
pkg?: string;
83+
forHelp?: boolean;
84+
}
85+
8186
interface CommandOptions extends CommanderCommandOptions {
8287
rawName: string;
8388
name: string;
@@ -86,7 +91,6 @@ interface CommandOptions extends CommanderCommandOptions {
8691
usage?: string;
8792
dependencies?: string[];
8893
pkg?: string;
89-
argsDescription?: Record<string, string>;
9094
external?: boolean;
9195
}
9296

@@ -501,9 +505,7 @@ class WebpackCLI {
501505
action: Parameters<Command["action"]>[0],
502506
): Promise<Command | undefined> {
503507
const alreadyLoaded = this.program.commands.find(
504-
(command) =>
505-
command.name() === commandOptions.rawName ||
506-
command.aliases().includes(commandOptions.alias as string),
508+
(command) => command.name() === commandOptions.rawName,
507509
);
508510

509511
if (alreadyLoaded) {
@@ -513,10 +515,10 @@ class WebpackCLI {
513515
const command = this.program.command(commandOptions.name, {
514516
hidden: commandOptions.hidden,
515517
isDefault: commandOptions.isDefault,
516-
});
518+
}) as Command;
517519

518520
if (commandOptions.description) {
519-
command.description(commandOptions.description, commandOptions.argsDescription!);
521+
command.description(commandOptions.description);
520522
}
521523

522524
if (commandOptions.usage) {
@@ -529,10 +531,9 @@ class WebpackCLI {
529531
command.alias(commandOptions.alias);
530532
}
531533

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

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

537538
let allDependenciesInstalled = true;
538539

@@ -1609,7 +1610,7 @@ class WebpackCLI {
16091610

16101611
webpackCLIOptions.isWatchingLikeCommand = true;
16111612

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

16141615
if (!compiler) {
16151616
return;
@@ -1881,8 +1882,8 @@ class WebpackCLI {
18811882

18821883
let pkg: string;
18831884

1884-
if (builtInExternalCommandInfo) {
1885-
({ pkg } = builtInExternalCommandInfo as CommandOptions & { pkg: string });
1885+
if (builtInExternalCommandInfo && builtInExternalCommandInfo.pkg) {
1886+
({ pkg } = builtInExternalCommandInfo);
18861887
} else {
18871888
pkg = commandName;
18881889
}
@@ -1906,10 +1907,10 @@ class WebpackCLI {
19061907
ConstructorParameters extends unknown[] = unknown[],
19071908
> = new (...args: ConstructorParameters) => InstanceType;
19081909

1909-
let loadedCommand: Instantiable<() => void>;
1910+
let LoadedCommand: Instantiable<() => void>;
19101911

19111912
try {
1912-
loadedCommand = (await import(pkg)).default;
1913+
LoadedCommand = (await import(pkg)).default;
19131914
} catch {
19141915
// Ignore, command is not installed
19151916
return;
@@ -1918,8 +1919,7 @@ class WebpackCLI {
19181919
let command;
19191920

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

19241924
await command.apply(this);
19251925
} catch (error) {
@@ -1949,43 +1949,6 @@ class WebpackCLI {
19491949
return;
19501950
}
19511951

1952-
if (error.code === "commander.unknownOption") {
1953-
let name = error.message.match(/'(.+)'/) as string | null;
1954-
1955-
if (name) {
1956-
name = name[1].slice(2);
1957-
1958-
if (name.includes("=")) {
1959-
[name] = name.split("=");
1960-
}
1961-
1962-
const { operands } = this.program.parseOptions(this.program.args);
1963-
const operand =
1964-
typeof operands[0] !== "undefined" ? operands[0] : WebpackCLI.#commands.build.rawName;
1965-
1966-
if (operand) {
1967-
const command = this.#findCommandByName(operand);
1968-
1969-
if (!command) {
1970-
this.logger.error(`Can't find and load command '${operand}'`);
1971-
this.logger.error("Run 'webpack --help' to see available commands and options");
1972-
process.exit(2);
1973-
}
1974-
1975-
const { distance } = require("fastest-levenshtein");
1976-
1977-
for (const option of (command as Command).options) {
1978-
if (
1979-
!(option as Option & { internal?: boolean }).internal &&
1980-
distance(name, option.long?.slice(2) as string) < 3
1981-
) {
1982-
this.logger.error(`Did you mean '--${option.name()}'?`);
1983-
}
1984-
}
1985-
}
1986-
}
1987-
}
1988-
19891952
this.logger.error("Run 'webpack --help' to see available commands and options");
19901953
process.exit(2);
19911954

@@ -2047,7 +2010,7 @@ class WebpackCLI {
20472010
isVerbose = true;
20482011
}
20492012

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

20522015
const optionsForHelp = [
20532016
...(isHelpOption && hasOperand ? [operand] : []),
@@ -2839,6 +2802,7 @@ class WebpackCLI {
28392802
});
28402803
}
28412804
} else {
2805+
// TODO bug on webpack side
28422806
const printedStats = stats.toString(statsOptions as StatsOptions);
28432807

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

0 commit comments

Comments
 (0)