Skip to content

Commit

Permalink
fix: a
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Apr 15, 2020
1 parent 6da6aa9 commit 5c9cf2a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
24 changes: 17 additions & 7 deletions packages/webpack-cli/lib/groups/HelpGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`);
Expand Down
7 changes: 7 additions & 0 deletions test/help/help-multi-args.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
16 changes: 14 additions & 2 deletions test/version/version-external-packages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down

0 comments on commit 5c9cf2a

Please sign in to comment.