Skip to content

Commit

Permalink
fix: improve error message for help (#2482)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Mar 9, 2021
1 parent 686a828 commit 99ae2a3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
11 changes: 9 additions & 2 deletions packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -940,8 +940,15 @@ class WebpackCLI {
const command = findCommandByName(name);

if (!command) {
this.logger.error(`Can't find and load command '${name}'`);
this.logger.error("Run 'webpack --help' to see available commands and options");
const builtInCommandUsed = externalBuiltInCommandsInfo.find(
(command) => command.name.includes(name) || name === command.alias,
);
if (typeof builtInCommandUsed !== 'undefined') {
this.logger.error(`For using '${name}' command you need to install '${builtInCommandUsed.pkg}' package`);
} else {
this.logger.error(`Can't find and load command '${name}'`);
this.logger.error("Run 'webpack --help' to see available commands and options");
}
process.exit(2);
}

Expand Down
6 changes: 5 additions & 1 deletion smoketests/index.js
@@ -1,5 +1,9 @@
/* eslint-disable node/no-unpublished-require */
const tests = [require('./missing-packages/webpack-dev-server.test.js'), require('./missing-packages/webpack.test.js')];
const tests = [
require('./missing-packages/webpack-dev-server.test.js'),
require('./missing-packages/webpack.test.js'),
require('./missing-command-help/generator.test.js'),
];

(async () => {
let isAllPassed = true;
Expand Down
77 changes: 77 additions & 0 deletions smoketests/missing-command-help/generator.test.js
@@ -0,0 +1,77 @@
'use strict';

const path = require('path');
const execa = require('execa');
const { renameSync } = require('fs');
const stripAnsi = require('strip-ansi');

const ROOT = process.env.GITHUB_WORKSPACE ? process.env.GITHUB_WORKSPACE : path.resolve(__dirname, '../../');
const CLI_ENTRY_PATH = path.resolve(ROOT, './packages/webpack-cli/bin/cli.js');

const getPkgPath = (pkg) => {
return path.resolve(ROOT, `./node_modules/@webpack-cli/${pkg}`);
};

const swapPkgName = (current, next) => {
console.log(` swapping ${current} with ${next}`);
renameSync(getPkgPath(current), getPkgPath(next));
};

const runTest = () => {
// Simulate package missing
swapPkgName('generators', '.generators');

const proc = execa(CLI_ENTRY_PATH, ['help', 'init'], {
cwd: __dirname,
});

proc.stdin.setDefaultEncoding('utf-8');

proc.stdout.on('data', (chunk) => {
console.log(` stdout: ${chunk.toString()}`);
});

return new Promise((resolve) => {
setTimeout(() => {
proc.kill();
}, 30000);

const logMessage = "For using 'init' command you need to install '@webpack-cli/generators' package";
const undefinedLogMessage = "Can't find and load command";

let hasLogMessage = false,
hasUndefinedLogMessage = false,
hasPassed = false;

proc.stderr.on('data', (chunk) => {
let data = stripAnsi(chunk.toString());
console.log(` stderr: ${data}`);

if (data.includes(logMessage)) {
hasLogMessage = true;
}

if (data.includes(undefinedLogMessage)) {
hasUndefinedLogMessage = true;
}

if (hasLogMessage || hasUndefinedLogMessage) {
hasPassed = true;
proc.kill();
}
});

proc.on('exit', () => {
swapPkgName('.generators', 'generators');
resolve(hasPassed);
});

proc.on('error', () => {
swapPkgName('.generators', 'generators');
resolve(false);
});
});
};

module.exports.run = runTest;
module.exports.name = 'Missing @webpack-cli/generators';

0 comments on commit 99ae2a3

Please sign in to comment.