Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve error message #2482

Merged
merged 4 commits into from Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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';