Skip to content

Commit

Permalink
fix: help and version functionality (#1972)
Browse files Browse the repository at this point in the history
* fix: invoke runHelp function if --help is passed

* tests: for --help

* fix: help & version

* tests: updates

* fix: conflict

* tests: update
  • Loading branch information
snitin315 authored Oct 21, 2020
1 parent 1cc1fa0 commit e8010b3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
13 changes: 0 additions & 13 deletions packages/webpack-cli/lib/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const WebpackCLI = require('./webpack-cli');
const { core } = require('./utils/cli-flags');
const versionRunner = require('./groups/runVersion');
const helpRunner = require('./groups/runHelp');
const logger = require('./utils/logger');
const { isCommandUsed } = require('./utils/arg-utils');
const argParser = require('./utils/arg-parser');
Expand All @@ -11,18 +9,7 @@ process.title = 'webpack-cli';
const runCLI = async (cliArgs) => {
const parsedArgs = argParser(core, cliArgs, true, process.title);

if (parsedArgs.unknownArgs.includes('help') || parsedArgs.opts.help) {
helpRunner(cliArgs);
process.exit(0);
}

const commandIsUsed = isCommandUsed(cliArgs);

if (parsedArgs.unknownArgs.includes('version') || parsedArgs.opts.version) {
versionRunner(cliArgs, commandIsUsed);
process.exit(0);
}

if (commandIsUsed) {
return;
}
Expand Down
13 changes: 10 additions & 3 deletions packages/webpack-cli/lib/utils/arg-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const commander = require('commander');
const logger = require('./logger');
const { commands } = require('./cli-flags');
const runHelp = require('../groups/runHelp');
const runVersion = require('../groups/runVersion');
const { defaultCommands } = require('./commands');

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

// Use customized help output if available
parser.on('option:help', () => {
// Use customized help output
if (args.includes('--help') || args.includes('help')) {
runHelp(args);
process.exit(0);
});
}

// Use Customized version
if (args.includes('--version') || args.includes('version') || args.includes('-v')) {
runVersion(args);
process.exit(0);
}

// Allow execution if unknown arguments are present
parser.allowUnknownOption(true);
Expand Down
7 changes: 4 additions & 3 deletions test/help/help-commands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const { run } = require('../utils/test-utils');
const helpHeader = 'The build tool for modern web applications';

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

it('shows help information with subcommands as an arg', () => {
Expand Down
8 changes: 8 additions & 0 deletions test/help/help-flags.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ describe('commands help', () => {
expect(stderr).toHaveLength(0);
});

it('should show help for --mode', () => {
const { stdout, stderr } = run(__dirname, ['--mode', '--help'], false);
expect(stdout).not.toContain(helpHeader);
expect(stdout).toContain('webpack --mode <development | production | none>');
expect(stdout).toContain('Defines the mode to pass to webpack');
expect(stderr).toHaveLength(0);
});

it('gives precedence to earlier flag in case of multiple flags', () => {
const { stdout, stderr } = run(__dirname, ['--help', '--entry', '--merge'], false);
expect(stdout).not.toContain(helpHeader);
Expand Down

0 comments on commit e8010b3

Please sign in to comment.