Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: help and version functionality #1972

Merged
merged 7 commits into from Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 0 additions & 13 deletions packages/webpack-cli/lib/bootstrap.js
@@ -1,8 +1,6 @@
const { options } = require('colorette');
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 cliExecuter = require('./utils/cli-executer');
Expand All @@ -22,17 +20,6 @@ const runCLI = async (cliArgs) => {

const commandIsUsed = isCommandUsed(cliArgs);
const parsedArgs = parseArgs(cliArgs);
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
if (parsedArgs.unknownArgs.includes('help') || parsedArgs.opts.help) {
options.enabled = !cliArgs.includes('--no-color');
helpRunner(cliArgs);
process.exit(0);
}

if (parsedArgs.unknownArgs.includes('version') || parsedArgs.opts.version) {
options.enabled = !cliArgs.includes('--no-color');
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
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 @@ -38,11 +39,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
5 changes: 0 additions & 5 deletions test/help/help-commands.test.js
Expand Up @@ -4,11 +4,6 @@ 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Supplying help as an argument to any of the subcommands shouldn't be allowed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can allow it for consistency.

webpack --flag help - Allowed
webpack help --help - Allowed
webpack help cmd - Allowed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

webpack help --help interesting, but I think it is good

Copy link
Member Author

@snitin315 snitin315 Oct 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh!, I wanted to type webpack help --flag.
Just checked webpack help --help is allowed as well and is equivalent to webpack help

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snitin315 Can we refactor this test to allow using help here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated 👍🏻

expect(stderr).toContain('Unknown argument: help');
});

it('shows help information with subcommands as an arg', () => {
const { stdout, stderr } = run(__dirname, ['help', 'serve'], false);
expect(stdout).toContain('webpack s | serve');
Expand Down
8 changes: 8 additions & 0 deletions test/help/help-flags.test.js
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