Skip to content

Commit

Permalink
feat: allow multiple types for --stats
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Jun 26, 2020
1 parent d087c61 commit ca2d593
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 45 deletions.
29 changes: 28 additions & 1 deletion packages/webpack-cli/__tests__/arg-parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ const basicOptions = [
description: 'string flag',
defaultValue: 'default-value',
},
{
name: 'multi-type',
usage: '--multi-type | --multi-type <value>',
type: [String, Boolean],
description: 'flag with multiple types',
},
{
name: 'custom-type-flag',
usage: '--custom-type-flag <value>',
Expand Down Expand Up @@ -127,6 +133,26 @@ describe('arg-parser', () => {
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);
expect(res.opts).toEqual({
multiType: true,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(0);
});

it('parses multi type flag as String', () => {
const res = argParser(basicOptions, ['--multi-type', 'value'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
multiType: 'value',
stringFlagWithDefault: 'default-value',
});
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);
Expand Down Expand Up @@ -225,11 +251,12 @@ describe('arg-parser', () => {
});

it('parses webpack args', () => {
const res = argParser(core, ['--entry', 'test.js', '--hot', '-o', './dist/'], true);
const res = argParser(core, ['--entry', 'test.js', '--hot', '-o', './dist/', '--stats'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts.entry).toEqual(['test.js']);
expect(res.opts.hot).toBeTruthy();
expect(res.opts.output).toEqual('./dist/');
expect(res.opts.stats).toEqual(true);
expect(warnMock.mock.calls.length).toEqual(0);
});
});
5 changes: 4 additions & 1 deletion packages/webpack-cli/lib/groups/HelpGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ class HelpGroup {
},
{
header: 'Options',
optionList: options.core,
optionList: options.core.map((e) => {
if (e.type.length > 1) e.type = e.type[0];
return e;
}),
},
]);
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/groups/StatsGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const logger = require('../utils/logger');
*/
class StatsGroup extends GroupHelper {
static validOptions() {
return ['none', 'errors-only', 'minimal', 'normal', 'detailed', 'verbose', 'errors-warnings'];
return ['none', 'errors-only', 'minimal', 'normal', 'detailed', 'verbose', 'errors-warnings', true];
}

constructor(options) {
Expand Down
9 changes: 7 additions & 2 deletions packages/webpack-cli/lib/utils/arg-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function argParser(options, args, argsOnly = false, name = '', helpFunction = un
// Register options on the parser
options.reduce((parserInstance, option) => {
const flags = option.alias ? `-${option.alias}, --${option.name}` : `--${option.name}`;
const flagsWithType = option.type !== Boolean ? flags + ' <value>' : flags;
let flagsWithType = option.type !== Boolean ? flags + ' <value>' : flags;
if (option.type === Boolean || option.type === String) {
if (!option.multiple) {
parserInstance.option(flagsWithType, option.description, option.defaultValue);
Expand All @@ -47,7 +47,12 @@ function argParser(options, args, argsOnly = false, name = '', helpFunction = un
}
} else {
// in this case the type is a parsing function
parserInstance.option(flagsWithType, option.description, option.type, option.defaultValue);
if (option.type.length > 1) {
flagsWithType = flags + ' [value]';
parserInstance.option(flagsWithType, option.description, option.type[0], option.defaultValue);
} else {
parserInstance.option(flagsWithType, option.description, option.type, option.defaultValue);
}
}

return parserInstance;
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/utils/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ module.exports = {
{
name: 'stats',
usage: '--stats <value>',
type: String,
type: [String, Boolean],
group: DISPLAY_GROUP,
description: 'It instructs webpack on how to treat the stats e.g. verbose',
link: 'https://webpack.js.org/configuration/stats/#stats',
Expand Down
62 changes: 23 additions & 39 deletions test/stats/stats.test.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,32 @@
'use strict';
// eslint-disable-next-line node/no-unpublished-require
const { run } = require('../utils/test-utils');
// eslint-disable-next-line node/no-extraneous-require
const { version } = require('webpack');

describe('stats flag', () => {
it('should accept stats "none"', () => {
const { stderr, stdout } = run(__dirname, ['--stats', 'none']);
expect(stderr).toBeFalsy();
expect(stdout).toBeFalsy();
});

it('should accept stats "errors-only"', () => {
const { stderr, stdout } = run(__dirname, ['--stats', 'errors-only']);
expect(stderr).toBeFalsy();
expect(stdout).toBeFalsy();
});
const presets = ['normal', 'detailed', 'errors-only', 'errors-warnings', 'minimal', 'verbose', 'none'];

it('should accept stats "errors-warnings"', () => {
const { stderr, stdout } = run(__dirname, ['--stats', 'errors-warnings']);
expect(stderr).toBeFalsy();
expect(stdout).toBeFalsy();
});

it('should accept stats "normal"', () => {
const { stderr, stdout } = run(__dirname, ['--stats', 'normal']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
});

it('should accept stats "verbose"', () => {
const { stderr, stdout } = run(__dirname, ['--stats', 'verbose']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
});

it('should accept stats "minimal"', () => {
const { stderr, stdout } = run(__dirname, ['--stats', 'minimal']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
});

it('should accept stats "detailed"', () => {
const { stderr, stdout } = run(__dirname, ['--stats', 'detailed']);
describe('stats flag', () => {
for (const preset of presets) {
it(`should accept --stats "${preset}"`, () => {
const { stderr, stdout } = run(__dirname, ['--stats', `${preset}`]);
expect(stderr).toBeFalsy();
if (version.startsWith('5')) {
expect(stdout).toContain(`stats: { preset: '${preset}' }`);
} else {
expect(stdout).toContain(`stats: '${preset}'`);
}
});
}

it('should accept stats as boolean', () => {
const { stderr, stdout } = run(__dirname, ['--stats']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
if (version.startsWith('5')) {
expect(stdout).toContain(`stats: {}`);
} else {
expect(stdout).toContain('stats: true');
}
});

it('should warn when --verbose & --stats are passed together', () => {
Expand Down
4 changes: 4 additions & 0 deletions test/stats/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// eslint-disable-next-line node/no-unpublished-require
const WebpackCLITestPlugin = require('../utils/webpack-cli-test-plugin');

module.exports = {
mode: 'development',
entry: './index.js',
plugins: [new WebpackCLITestPlugin()],
};

0 comments on commit ca2d593

Please sign in to comment.