Skip to content

Commit 540290f

Browse files
feat(ui): add frame helper and place it to ui-renderer
It avoid repreated code in webpack-cli
1 parent c8bf2fd commit 540290f

2 files changed

Lines changed: 75 additions & 25 deletions

File tree

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ export interface CommandHelpData {
5959
globalOptions: HelpOption[];
6060
}
6161

62+
/** Passed to `renderCommandHeader` to describe the running command. */
63+
export interface CommandMeta {
64+
name: string;
65+
description: string;
66+
}
67+
6268
/** One section emitted by `renderInfoOutput`, e.g. "System" or "Binaries". */
6369
export interface InfoSection {
6470
title: string;
@@ -133,6 +139,23 @@ export function renderRows(
133139
}
134140
}
135141

142+
export function renderCommandHeader(meta: CommandMeta, opts: RenderOptions): void {
143+
const { colors, log } = opts;
144+
const termWidth = Math.min(opts.columns || MAX_WIDTH, MAX_WIDTH);
145+
146+
log("");
147+
log(`${indent(INDENT)}${colors.bold(colors.cyan("⬡"))} ${colors.bold(`webpack ${meta.name}`)}`);
148+
log(divider(termWidth, colors));
149+
150+
if (meta.description) {
151+
const descWidth = termWidth - INDENT * 2;
152+
for (const line of wrapValue(meta.description, descWidth)) {
153+
log(`${indent(INDENT)}${line}`);
154+
}
155+
log("");
156+
}
157+
}
158+
136159
function _renderHelpOptions(
137160
options: HelpOption[],
138161
colors: Colors,
@@ -361,3 +384,13 @@ export function renderFooter(opts: RenderOptions, footer: FooterOptions = {}): v
361384
log(` ${colors.bold("Made with")} ${colors.red("♥")} ${colors.bold("by the webpack team")}`);
362385
log("");
363386
}
387+
388+
export async function frame(
389+
meta: CommandMeta,
390+
func: (opts: RenderOptions) => void | Promise<void>,
391+
opts: RenderOptions,
392+
): Promise<void> {
393+
renderCommandHeader(meta, opts);
394+
await func(opts);
395+
renderFooter(opts);
396+
}

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

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
HelpOption,
3737
MAX_WIDTH,
3838
RenderOptions,
39+
frame,
3940
renderAliasHelp,
4041
renderCommandHelp,
4142
renderError,
@@ -1009,7 +1010,8 @@ class WebpackCLI {
10091010
return {
10101011
colors: this.colors,
10111012
log: (line) => this.logger.raw(line),
1012-
columns: process.stdout.columns ?? MAX_WIDTH, // import MAX_WIDTH from ui-renderer
1013+
// we mirror the way commander handle the width here as calling it directly will cause an infinite loop
1014+
columns: process.stdout.columns ?? MAX_WIDTH, // import MAX_WIDTH from ui-renderer instead of hardcode 80 even if it's consider as the standard
10131015
};
10141016
}
10151017

@@ -1823,18 +1825,24 @@ class WebpackCLI {
18231825
},
18241826
],
18251827
action: async (options: { output?: string }) => {
1826-
const rawInfo = await this.#getInfoOutput({
1827-
information: {
1828-
npmPackages: `{${DEFAULT_WEBPACK_PACKAGES.map((item) => `*${item}*`).join(",")}}`,
1829-
},
1830-
});
1831-
18321828
if (options.output) {
18331829
this.logger.raw(await this.#renderVersion(options));
18341830
return;
18351831
}
18361832

1837-
renderVersionOutput(rawInfo, this.#renderOptions());
1833+
await frame(
1834+
{ name: "version", description: "Installed package versions." },
1835+
async (option) =>
1836+
renderVersionOutput(
1837+
await this.#getInfoOutput({
1838+
information: {
1839+
npmPackages: `{${DEFAULT_WEBPACK_PACKAGES.map((item) => `*${item}*`).join(",")}}`,
1840+
},
1841+
}),
1842+
option,
1843+
),
1844+
this.#renderOptions(),
1845+
);
18381846
},
18391847
},
18401848
info: {
@@ -1872,7 +1880,11 @@ class WebpackCLI {
18721880
return;
18731881
}
18741882

1875-
renderInfoOutput(info, this.#renderOptions());
1883+
await frame(
1884+
{ name: "info", description: "System and environment information." },
1885+
(option) => renderInfoOutput(info, option),
1886+
this.#renderOptions(),
1887+
);
18761888
},
18771889
},
18781890
configtest: {
@@ -1894,7 +1906,6 @@ class WebpackCLI {
18941906
configPath ? { env, argv, webpack, config: [configPath] } : { env, argv, webpack },
18951907
);
18961908
const configPaths = new Set<string>();
1897-
const renderOpts = this.#renderOptions();
18981909

18991910
if (Array.isArray(config.options)) {
19001911
for (const options of config.options) {
@@ -1912,21 +1923,27 @@ class WebpackCLI {
19121923
}
19131924
}
19141925

1915-
if (configPaths.size === 0) {
1916-
renderError("No configuration found.", renderOpts);
1917-
process.exit(2);
1918-
}
1919-
renderWarning(`Validating: ${[...configPaths].join(", ")}`, renderOpts);
1920-
try {
1921-
cmd.context.webpack.validate(config.options);
1922-
} catch (error) {
1923-
renderError(
1924-
this.isValidationError(error as Error) ? (error as Error).message : String(error),
1925-
renderOpts,
1926-
);
1927-
process.exit(2);
1928-
}
1929-
renderSuccess("No validation errors found.", renderOpts);
1926+
await frame(
1927+
{ name: "configtest", description: "Validating your webpack configuration." },
1928+
async (option) => {
1929+
if (configPaths.size === 0) {
1930+
renderError("No configuration found.", option);
1931+
process.exit(2);
1932+
}
1933+
renderWarning(`Validating: ${[...configPaths].join(", ")}`, option);
1934+
try {
1935+
cmd.context.webpack.validate(config.options);
1936+
} catch (error) {
1937+
renderError(
1938+
this.isValidationError(error as Error) ? (error as Error).message : String(error),
1939+
option,
1940+
);
1941+
process.exit(2);
1942+
}
1943+
renderSuccess("No validation errors found.", option);
1944+
},
1945+
this.#renderOptions(),
1946+
);
19301947
},
19311948
},
19321949
};

0 commit comments

Comments
 (0)