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

feat(webpack-cli): allow negative property for cli-flags #1668

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 53 additions & 48 deletions packages/webpack-cli/__tests__/arg-parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,20 @@ const basicOptions = [
type: Boolean,
description: 'boolean flag',
},
{
name: 'specific-bool',
alias: 's',
usage: '--specific-bool',
type: Boolean,
description: 'boolean flag',
},
{
name: 'no-specific-bool',
usage: '--no-specific-bool',
type: Boolean,
description: 'boolean flag',
},
{
name: 'num-flag',
usage: '--num-flag <value>',
type: Number,
description: 'number flag',
},
{
name: 'neg-flag',
alias: 'n',
usage: '--neg-flag',
type: Boolean,
negative: true,
description: 'boolean flag',
},
{
name: 'string-flag',
usage: '--string-flag <value>',
Expand Down Expand Up @@ -150,16 +145,6 @@ describe('arg-parser', () => {
expect(warnMock.mock.calls.length).toEqual(0);
});

it('parses specified negated boolean flag', () => {
const res = argParser(basicOptions, ['--no-specific-bool'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
specificBool: false,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(0);
});

it('parses multi type flag as Boolean', () => {
const res = argParser(basicOptions, ['--multi-type'], true);
expect(res.unknownArgs.length).toEqual(0);
Expand All @@ -180,37 +165,15 @@ describe('arg-parser', () => {
expect(warnMock.mock.calls.length).toEqual(0);
});

it('warns on usage of both flag and same negated flag, setting it to false', () => {
const res = argParser(basicOptions, ['--specific-bool', '--no-specific-bool'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
specificBool: false,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(1);
expect(warnMock.mock.calls[0][0]).toContain('You provided both --specific-bool and --no-specific-bool');
});

it('warns on usage of both flag and same negated flag, setting it to true', () => {
const res = argParser(basicOptions, ['--no-specific-bool', '--specific-bool'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
specificBool: true,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(1);
expect(warnMock.mock.calls[0][0]).toContain('You provided both --specific-bool and --no-specific-bool');
});

it('warns on usage of both flag alias and same negated flag, setting it to true', () => {
const res = argParser(basicOptions, ['--no-specific-bool', '-s'], true);
const res = argParser(basicOptions, ['--no-neg-flag', '-n'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
specificBool: true,
negFlag: true,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(1);
expect(warnMock.mock.calls[0][0]).toContain('You provided both -s and --no-specific-bool');
expect(warnMock.mock.calls[0][0]).toContain('You provided both -n and --no-neg-flag');
});

it('parses string flag using equals sign', () => {
Expand Down Expand Up @@ -286,4 +249,46 @@ describe('arg-parser', () => {
expect(res.opts.stats).toEqual(true);
expect(warnMock.mock.calls.length).toEqual(0);
});

it('parses --neg-flag', () => {
const res = argParser(basicOptions, ['--neg-flag'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
negFlag: true,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(0);
});

it('parses --no-neg-flag', () => {
const res = argParser(basicOptions, ['--no-neg-flag'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
negFlag: false,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(0);
});

it('warns on usage of both --neg and --no-neg flag, setting it to false', () => {
const res = argParser(basicOptions, ['--neg-flag', '--no-neg-flag'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
negFlag: false,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(1);
expect(warnMock.mock.calls[0][0]).toContain('You provided both --neg-flag and --no-neg-flag');
});

it('warns on usage of both flag and same negated flag, setting it to true', () => {
const res = argParser(basicOptions, ['--no-neg-flag', '--neg-flag'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
negFlag: true,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(1);
expect(warnMock.mock.calls[0][0]).toContain('You provided both --neg-flag and --no-neg-flag');
});
});
15 changes: 11 additions & 4 deletions packages/webpack-cli/lib/groups/HelpGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class HelpGroup {
const { underline, bold } = chalk.white;
const o = (s) => chalk.keyword('orange')(s);
const options = require('../utils/cli-flags');
const negatedFlags = options.core
.filter((flag) => flag.negative)
.reduce((allFlags, flag) => {
return [...allFlags, { name: `no-${flag.name}`, description: `Negates ${flag.name}`, type: Boolean }];
}, []);
const title = bold('⬡ ') + underline('webpack') + bold(' ⬡');
const desc = 'The build tool for modern web applications';
const websitelink = ' ' + underline('https://webpack.js.org');
Expand All @@ -110,10 +115,12 @@ class HelpGroup {
},
{
header: 'Options',
optionList: options.core.map((e) => {
if (e.type.length > 1) e.type = e.type[0];
return e;
}),
optionList: options.core
.map((e) => {
if (e.type.length > 1) e.type = e.type[0];
return e;
})
.concat(negatedFlags),
},
]);
return {
Expand Down
5 changes: 5 additions & 0 deletions packages/webpack-cli/lib/utils/arg-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ function argParser(options, args, argsOnly = false, name = '', helpFunction = un
parserInstance.option(flagsWithType, option.description, option.type, option.defaultValue).action(() => {});
}
}
if (option.negative) {
// commander requires explicitly adding the negated version of boolean flags
const negatedFlag = `--no-${option.name}`;
parserInstance.option(negatedFlag, `negates ${option.name}`).action(() => {});
}

return parserInstance;
}, parser);
Expand Down
18 changes: 2 additions & 16 deletions packages/webpack-cli/lib/utils/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,11 @@ module.exports = {
usage: '--hot',
alias: 'h',
type: Boolean,
negative: true,
group: ADVANCED_GROUP,
description: 'Enables Hot Module Replacement',
link: 'https://webpack.js.org/concepts/hot-module-replacement/',
},
{
name: 'no-hot',
usage: '--no-hot',
type: Boolean,
group: ADVANCED_GROUP,
description: 'Disables Hot Module Replacement',
link: 'https://webpack.js.org/concepts/hot-module-replacement/',
},
{
name: 'sourcemap',
usage: '--sourcemap <sourcemap | eval>',
Expand Down Expand Up @@ -242,17 +235,10 @@ module.exports = {
usage: '--stats <value>',
type: [String, Boolean],
group: DISPLAY_GROUP,
negative: true,
description: 'It instructs webpack on how to treat the stats e.g. verbose',
link: 'https://webpack.js.org/configuration/stats/#stats',
},
{
name: 'no-stats',
usage: '--no-stats',
type: Boolean,
group: DISPLAY_GROUP,
description: 'Disables stats output',
link: 'https://webpack.js.org/configuration/stats/#stats',
},
{
name: 'verbose',
usage: '--verbose',
Expand Down