Skip to content

Commit e8010b3

Browse files
authored
fix: help and version functionality (#1972)
* fix: invoke runHelp function if --help is passed * tests: for --help * fix: help & version * tests: updates * fix: conflict * tests: update
1 parent 1cc1fa0 commit e8010b3

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

packages/webpack-cli/lib/bootstrap.js

-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const WebpackCLI = require('./webpack-cli');
22
const { core } = require('./utils/cli-flags');
3-
const versionRunner = require('./groups/runVersion');
4-
const helpRunner = require('./groups/runHelp');
53
const logger = require('./utils/logger');
64
const { isCommandUsed } = require('./utils/arg-utils');
75
const argParser = require('./utils/arg-parser');
@@ -11,18 +9,7 @@ process.title = 'webpack-cli';
119
const runCLI = async (cliArgs) => {
1210
const parsedArgs = argParser(core, cliArgs, true, process.title);
1311

14-
if (parsedArgs.unknownArgs.includes('help') || parsedArgs.opts.help) {
15-
helpRunner(cliArgs);
16-
process.exit(0);
17-
}
18-
1912
const commandIsUsed = isCommandUsed(cliArgs);
20-
21-
if (parsedArgs.unknownArgs.includes('version') || parsedArgs.opts.version) {
22-
versionRunner(cliArgs, commandIsUsed);
23-
process.exit(0);
24-
}
25-
2613
if (commandIsUsed) {
2714
return;
2815
}

packages/webpack-cli/lib/utils/arg-parser.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const commander = require('commander');
22
const logger = require('./logger');
33
const { commands } = require('./cli-flags');
44
const runHelp = require('../groups/runHelp');
5+
const runVersion = require('../groups/runVersion');
56
const { defaultCommands } = require('./commands');
67

78
/**
@@ -39,11 +40,17 @@ const argParser = (options, args, argsOnly = false, name = '') => {
3940
// Prevent default behavior
4041
parser.on('command:*', () => {});
4142

42-
// Use customized help output if available
43-
parser.on('option:help', () => {
43+
// Use customized help output
44+
if (args.includes('--help') || args.includes('help')) {
4445
runHelp(args);
4546
process.exit(0);
46-
});
47+
}
48+
49+
// Use Customized version
50+
if (args.includes('--version') || args.includes('version') || args.includes('-v')) {
51+
runVersion(args);
52+
process.exit(0);
53+
}
4754

4855
// Allow execution if unknown arguments are present
4956
parser.allowUnknownOption(true);

test/help/help-commands.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ const { run } = require('../utils/test-utils');
44
const helpHeader = 'The build tool for modern web applications';
55

66
describe('commands help', () => {
7-
it('throws error if supplied as an argument for subcommands', () => {
8-
const { stderr } = run(__dirname, ['serve', 'help'], false);
9-
expect(stderr).toContain('Unknown argument: help');
7+
it('shows help for subcommands', () => {
8+
const { stderr, stdout } = run(__dirname, ['serve', 'help'], false);
9+
expect(stderr).toBeFalsy();
10+
expect(stdout).toContain('webpack s | serve');
1011
});
1112

1213
it('shows help information with subcommands as an arg', () => {

test/help/help-flags.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ describe('commands help', () => {
2121
expect(stderr).toHaveLength(0);
2222
});
2323

24+
it('should show help for --mode', () => {
25+
const { stdout, stderr } = run(__dirname, ['--mode', '--help'], false);
26+
expect(stdout).not.toContain(helpHeader);
27+
expect(stdout).toContain('webpack --mode <development | production | none>');
28+
expect(stdout).toContain('Defines the mode to pass to webpack');
29+
expect(stderr).toHaveLength(0);
30+
});
31+
2432
it('gives precedence to earlier flag in case of multiple flags', () => {
2533
const { stdout, stderr } = run(__dirname, ['--help', '--entry', '--merge'], false);
2634
expect(stdout).not.toContain(helpHeader);

0 commit comments

Comments
 (0)