Skip to content

Commit 99ae2a3

Browse files
authored
fix: improve error message for help (#2482)
1 parent 686a828 commit 99ae2a3

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed

packages/webpack-cli/lib/webpack-cli.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -940,8 +940,15 @@ class WebpackCLI {
940940
const command = findCommandByName(name);
941941

942942
if (!command) {
943-
this.logger.error(`Can't find and load command '${name}'`);
944-
this.logger.error("Run 'webpack --help' to see available commands and options");
943+
const builtInCommandUsed = externalBuiltInCommandsInfo.find(
944+
(command) => command.name.includes(name) || name === command.alias,
945+
);
946+
if (typeof builtInCommandUsed !== 'undefined') {
947+
this.logger.error(`For using '${name}' command you need to install '${builtInCommandUsed.pkg}' package`);
948+
} else {
949+
this.logger.error(`Can't find and load command '${name}'`);
950+
this.logger.error("Run 'webpack --help' to see available commands and options");
951+
}
945952
process.exit(2);
946953
}
947954

smoketests/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/* eslint-disable node/no-unpublished-require */
2-
const tests = [require('./missing-packages/webpack-dev-server.test.js'), require('./missing-packages/webpack.test.js')];
2+
const tests = [
3+
require('./missing-packages/webpack-dev-server.test.js'),
4+
require('./missing-packages/webpack.test.js'),
5+
require('./missing-command-help/generator.test.js'),
6+
];
37

48
(async () => {
59
let isAllPassed = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const execa = require('execa');
5+
const { renameSync } = require('fs');
6+
const stripAnsi = require('strip-ansi');
7+
8+
const ROOT = process.env.GITHUB_WORKSPACE ? process.env.GITHUB_WORKSPACE : path.resolve(__dirname, '../../');
9+
const CLI_ENTRY_PATH = path.resolve(ROOT, './packages/webpack-cli/bin/cli.js');
10+
11+
const getPkgPath = (pkg) => {
12+
return path.resolve(ROOT, `./node_modules/@webpack-cli/${pkg}`);
13+
};
14+
15+
const swapPkgName = (current, next) => {
16+
console.log(` swapping ${current} with ${next}`);
17+
renameSync(getPkgPath(current), getPkgPath(next));
18+
};
19+
20+
const runTest = () => {
21+
// Simulate package missing
22+
swapPkgName('generators', '.generators');
23+
24+
const proc = execa(CLI_ENTRY_PATH, ['help', 'init'], {
25+
cwd: __dirname,
26+
});
27+
28+
proc.stdin.setDefaultEncoding('utf-8');
29+
30+
proc.stdout.on('data', (chunk) => {
31+
console.log(` stdout: ${chunk.toString()}`);
32+
});
33+
34+
return new Promise((resolve) => {
35+
setTimeout(() => {
36+
proc.kill();
37+
}, 30000);
38+
39+
const logMessage = "For using 'init' command you need to install '@webpack-cli/generators' package";
40+
const undefinedLogMessage = "Can't find and load command";
41+
42+
let hasLogMessage = false,
43+
hasUndefinedLogMessage = false,
44+
hasPassed = false;
45+
46+
proc.stderr.on('data', (chunk) => {
47+
let data = stripAnsi(chunk.toString());
48+
console.log(` stderr: ${data}`);
49+
50+
if (data.includes(logMessage)) {
51+
hasLogMessage = true;
52+
}
53+
54+
if (data.includes(undefinedLogMessage)) {
55+
hasUndefinedLogMessage = true;
56+
}
57+
58+
if (hasLogMessage || hasUndefinedLogMessage) {
59+
hasPassed = true;
60+
proc.kill();
61+
}
62+
});
63+
64+
proc.on('exit', () => {
65+
swapPkgName('.generators', 'generators');
66+
resolve(hasPassed);
67+
});
68+
69+
proc.on('error', () => {
70+
swapPkgName('.generators', 'generators');
71+
resolve(false);
72+
});
73+
});
74+
};
75+
76+
module.exports.run = runTest;
77+
module.exports.name = 'Missing @webpack-cli/generators';

0 commit comments

Comments
 (0)