diff --git a/packages/webpack-cli/lib/groups/HelpGroup.js b/packages/webpack-cli/lib/groups/HelpGroup.js index fa33962dc05..9cb2625f05f 100644 --- a/packages/webpack-cli/lib/groups/HelpGroup.js +++ b/packages/webpack-cli/lib/groups/HelpGroup.js @@ -2,14 +2,16 @@ const chalk = require('chalk'); const { core, commands } = require('../utils/cli-flags'); const commandLineUsage = require('command-line-usage'); -const [, , ...rawArgs] = process.argv; -const commandsUsed = rawArgs.filter((val) => commands.find(({ name }) => name === val)); -const flagsUsed = rawArgs.filter((val) => core.find(({ name }) => name === val.slice(2))); -const argsUsed = [...commandsUsed, ...flagsUsed]; -const invalidArgs = rawArgs.filter((e) => !argsUsed.includes(e) && !e.includes('--color') && e !== 'version' && e !== 'help' && e !== '-v'); - class HelpGroup { outputHelp(isCommand = true, subject) { + const [, , ...rawArgs] = process.argv; + const commandsUsed = rawArgs.filter((val) => commands.find(({ name }) => name === val)); + const flagsUsed = rawArgs.filter((val) => core.find(({ name }) => name === val.slice(2))); + const argsUsed = [...commandsUsed, ...flagsUsed]; + const invalidArgs = rawArgs.filter( + (e) => !argsUsed.includes(e) && !e.includes('--color') && e !== 'version' && e !== '-v' && e !== 'help', + ); + if (subject && invalidArgs.length === 0) { const info = isCommand ? commands : core; // Contains object with details about given subject @@ -50,7 +52,15 @@ class HelpGroup { } outputVersion(externalPkg) { - if (externalPkg && commandsUsed.length === 1) { + const [, , ...rawArgs] = process.argv; + const commandsUsed = rawArgs.filter((val) => commands.find(({ name }) => name === val)); + const flagsUsed = rawArgs.filter((val) => core.find(({ name }) => name === val.slice(2))); + const argsUsed = [...commandsUsed, ...flagsUsed]; + const invalidArgs = rawArgs.filter( + (e) => !argsUsed.includes(e) && !e.includes('--color') && e !== 'version' && e !== '-v' && e !== 'help', + ); + + if (externalPkg && commandsUsed.length === 1 && invalidArgs.length === 0) { try { if (commandsUsed.includes(externalPkg.name)) { const { name, version } = require(`@webpack-cli/${externalPkg.name}/package.json`); diff --git a/test/help/help-multi-args.test.js b/test/help/help-multi-args.test.js index b81563f7fcf..9614a2c48cb 100644 --- a/test/help/help-multi-args.test.js +++ b/test/help/help-multi-args.test.js @@ -15,4 +15,11 @@ describe('help cmd with multiple arguments', () => { expect(stderr).toHaveLength(0); }); }); + + it('should output help for --version by taking precedence', () => { + const { stdout, stderr } = run(__dirname, ['--help', '--version'], false); + expect(stdout).not.toContain(helpHeader); + expect(stdout).toContain('webpack --version'); + expect(stderr).toHaveLength(0); + }); }); diff --git a/test/version/version-external-packages.test.js b/test/version/version-external-packages.test.js index 495094dd475..affba2ef835 100644 --- a/test/version/version-external-packages.test.js +++ b/test/version/version-external-packages.test.js @@ -41,8 +41,20 @@ describe('version flag with external packages', () => { expect(stderr).toContain('You provided multiple commands.'); }); - it(' should throw error if invalid argument is present', () => { - const { stderr, stdout } = run(__dirname, ['init', 'abc', '--version']); + it(' should throw error if invalid argument is present with --version flag', () => { + const { stderr, stdout } = run(__dirname, ['init', 'abc', '--version'], false); + expect(stderr).toContain(`Error: Invalid Option 'abc'`); + expect(stdout).toContain('Run webpack --help to see available commands and arguments'); + }); + + it(' should throw error if invalid argument is present with version command', () => { + const { stderr, stdout } = run(__dirname, ['init', 'abc', 'version'], false); + expect(stderr).toContain(`Error: Invalid Option 'abc'`); + expect(stdout).toContain('Run webpack --help to see available commands and arguments'); + }); + + it(' should throw error if invalid argument is present with -v alias', () => { + const { stderr, stdout } = run(__dirname, ['init', 'abc', '-v'], false); expect(stderr).toContain(`Error: Invalid Option 'abc'`); expect(stdout).toContain('Run webpack --help to see available commands and arguments'); });