From e8010b3aac695971e542ad4d3584ce534da39b8f Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Wed, 21 Oct 2020 16:43:40 +0530 Subject: [PATCH] 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 --- packages/webpack-cli/lib/bootstrap.js | 13 ------------- packages/webpack-cli/lib/utils/arg-parser.js | 13 ++++++++++--- test/help/help-commands.test.js | 7 ++++--- test/help/help-flags.test.js | 8 ++++++++ 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/packages/webpack-cli/lib/bootstrap.js b/packages/webpack-cli/lib/bootstrap.js index 31ae522773e..56996ec64c7 100644 --- a/packages/webpack-cli/lib/bootstrap.js +++ b/packages/webpack-cli/lib/bootstrap.js @@ -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'); @@ -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; } diff --git a/packages/webpack-cli/lib/utils/arg-parser.js b/packages/webpack-cli/lib/utils/arg-parser.js index 5d3aec0a9eb..896362c2503 100644 --- a/packages/webpack-cli/lib/utils/arg-parser.js +++ b/packages/webpack-cli/lib/utils/arg-parser.js @@ -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'); /** @@ -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); diff --git a/test/help/help-commands.test.js b/test/help/help-commands.test.js index 63f2e94fdf4..aa2f8dad0e1 100644 --- a/test/help/help-commands.test.js +++ b/test/help/help-commands.test.js @@ -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', () => { diff --git a/test/help/help-flags.test.js b/test/help/help-flags.test.js index 544824ce3c4..0829d58a9be 100644 --- a/test/help/help-flags.test.js +++ b/test/help/help-flags.test.js @@ -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 '); + 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);