-
-
Notifications
You must be signed in to change notification settings - Fork 602
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
migrate to commander #1481
Merged
jamesgeorge007
merged 44 commits into
webpack:next
from
knagaitsev:chore/migrate-commander
Apr 25, 2020
Merged
migrate to commander #1481
Changes from 41 commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
e663d10
chore: use commander
rishabh3112 87403bf
chore: add support for help and version command
rishabh3112 ec8c474
chore: remove duplicate code for no-mode
rishabh3112 f7009ae
chore: create helpers
rishabh3112 af62829
chore: remove global flag
rishabh3112 eab3500
chore: improve arg parsing and tests
rishabh3112 ab17bf0
chore: remove command-line-args
rishabh3112 327fc7d
chore: remove instance of command-line-args
rishabh3112 05173d8
chore: add explaination to the change
rishabh3112 5244654
chore: fix
rishabh3112 777f8e5
chore: migrate serve package
rishabh3112 b9a4932
chore: handle error
rishabh3112 ad0cc8f
chore: reactor argparser
rishabh3112 1b3212d
chore: get serve args from process.argv
rishabh3112 503a1f7
chore: sync message with test
rishabh3112 7cf46bf
chore: fix styling
knagaitsev 231f181
chore: fix problem with no mode flag
knagaitsev 0e1db3b
chore: started adding info parsing
knagaitsev 24cbc85
chore: parsing without first few parts of process argv
knagaitsev cbbed32
chore: fixed args calling in serve
knagaitsev edf38f2
chore: fixing serve
knagaitsev a7fa090
chore: fix no-mode tests
knagaitsev ab8e885
chore: fix output defaults tests
knagaitsev c4a4d69
chore: handle default webpack entry arg
knagaitsev 4eccb84
chore: fix basic config test
knagaitsev 26c65cc
chore: fix version and help commands
knagaitsev 783f456
chore: change help, version, and fix entry checking
knagaitsev 29aa5c0
chore: add some negated flag handling
knagaitsev f1888cf
chore: fix serve test
knagaitsev 793e51e
chore: rename misleading parse-args to parse-node-args
knagaitsev 03b7618
chore: added arg-parser tests
knagaitsev 575fbe8
chore: use args passed to serve
knagaitsev fa0f145
chore: fix stats flag handling
knagaitsev 91ee8f5
chore: fix function as a custom type
knagaitsev 9240879
chore: change boolean logic in arg parser
knagaitsev f4c94da
chore: fixed stats test and removed stats custom type
knagaitsev 11282ee
chore: removed output todo and added tests for warning
knagaitsev 8339c7b
chore: changed invalid mode message and updated test
knagaitsev 426c666
chore: add comment about commander negation
knagaitsev 2deb7e3
chore: made output tests sync
knagaitsev dfb6f6c
chore: fix spelling in comment
knagaitsev c068011
chore: remove unnecessary log
knagaitsev 9351796
chore: add runPromptWithAnswers changes for stability
knagaitsev d160106
Merge branch 'next' into chore/migrate-commander
jamesgeorge007 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
const warnMock = jest.fn(); | ||
jest.mock('../lib/utils/logger', () => { | ||
return { | ||
warn: warnMock, | ||
}; | ||
}); | ||
jest.spyOn(process, 'exit').mockImplementation(() => {}); | ||
|
||
const argParser = require('../lib/utils/arg-parser'); | ||
const { core } = require('../lib/utils/cli-flags'); | ||
|
||
const basicOptions = [ | ||
{ | ||
name: 'bool-flag', | ||
alias: 'b', | ||
usage: '--bool-flag', | ||
type: Boolean, | ||
description: 'boolean flag', | ||
}, | ||
{ | ||
name: 'string-flag', | ||
usage: '--string-flag <value>', | ||
type: String, | ||
description: 'string flag', | ||
}, | ||
{ | ||
name: 'string-flag-with-default', | ||
usage: '--string-flag-with-default <value>', | ||
type: String, | ||
description: 'string flag', | ||
defaultValue: 'default-value', | ||
}, | ||
{ | ||
name: 'custom-type-flag', | ||
usage: '--custom-type-flag <value>', | ||
type: (val) => { | ||
return val.split(','); | ||
}, | ||
description: 'custom type flag', | ||
}, | ||
]; | ||
|
||
const helpAndVersionOptions = basicOptions.slice(0); | ||
helpAndVersionOptions.push( | ||
{ | ||
name: 'help', | ||
usage: '--help', | ||
type: Boolean, | ||
description: 'help', | ||
}, | ||
{ | ||
name: 'version', | ||
alias: 'v', | ||
usage: '--version', | ||
type: Boolean, | ||
description: 'version', | ||
}, | ||
); | ||
|
||
describe('arg-parser', () => { | ||
beforeEach(() => { | ||
warnMock.mockClear(); | ||
}); | ||
|
||
it('parses no flags', () => { | ||
const res = argParser(basicOptions, [], true); | ||
expect(res.unknownArgs.length).toEqual(0); | ||
expect(res.opts).toEqual({ | ||
stringFlagWithDefault: 'default-value', | ||
}); | ||
expect(warnMock.mock.calls.length).toEqual(0); | ||
}); | ||
|
||
it('parses basic flags', () => { | ||
const res = argParser(basicOptions, ['--bool-flag', '--string-flag', 'val'], true); | ||
expect(res.unknownArgs.length).toEqual(0); | ||
expect(res.opts).toEqual({ | ||
boolFlag: true, | ||
stringFlag: 'val', | ||
stringFlagWithDefault: 'default-value', | ||
}); | ||
expect(warnMock.mock.calls.length).toEqual(0); | ||
}); | ||
|
||
it('parses negated boolean flags', () => { | ||
const res = argParser(basicOptions, ['--no-bool-flag'], true); | ||
expect(res.unknownArgs.length).toEqual(0); | ||
expect(res.opts).toEqual({ | ||
boolFlag: false, | ||
stringFlagWithDefault: 'default-value', | ||
}); | ||
expect(warnMock.mock.calls.length).toEqual(0); | ||
}); | ||
|
||
it('parses boolean flag alias', () => { | ||
const res = argParser(basicOptions, ['-b'], true); | ||
expect(res.unknownArgs.length).toEqual(0); | ||
expect(res.opts).toEqual({ | ||
boolFlag: true, | ||
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, ['--bool-flag', '--no-bool-flag'], true); | ||
expect(res.unknownArgs.length).toEqual(0); | ||
expect(res.opts).toEqual({ | ||
boolFlag: false, | ||
stringFlagWithDefault: 'default-value', | ||
}); | ||
expect(warnMock.mock.calls.length).toEqual(1); | ||
expect(warnMock.mock.calls[0][0]).toContain('You provided both --bool-flag and --no-bool-flag'); | ||
}); | ||
|
||
it('warns on usage of both flag and same negated flag, setting it to true', () => { | ||
const res = argParser(basicOptions, ['--no-bool-flag', '--bool-flag'], true); | ||
expect(res.unknownArgs.length).toEqual(0); | ||
expect(res.opts).toEqual({ | ||
boolFlag: true, | ||
stringFlagWithDefault: 'default-value', | ||
}); | ||
expect(warnMock.mock.calls.length).toEqual(1); | ||
expect(warnMock.mock.calls[0][0]).toContain('You provided both --bool-flag and --no-bool-flag'); | ||
}); | ||
|
||
it('warns on usage of both flag alias and same negated flag, setting it to true', () => { | ||
const res = argParser(basicOptions, ['--no-bool-flag', '-b'], true); | ||
expect(res.unknownArgs.length).toEqual(0); | ||
expect(res.opts).toEqual({ | ||
boolFlag: true, | ||
stringFlagWithDefault: 'default-value', | ||
}); | ||
expect(warnMock.mock.calls.length).toEqual(1); | ||
expect(warnMock.mock.calls[0][0]).toContain('You provided both -b and --no-bool-flag'); | ||
}); | ||
|
||
it('parses string flag using equals sign', () => { | ||
const res = argParser(basicOptions, ['--string-flag=val'], true); | ||
expect(res.unknownArgs.length).toEqual(0); | ||
expect(res.opts).toEqual({ | ||
stringFlag: 'val', | ||
stringFlagWithDefault: 'default-value', | ||
}); | ||
expect(warnMock.mock.calls.length).toEqual(0); | ||
}); | ||
|
||
it('handles additional node args from argv', () => { | ||
const res = argParser(basicOptions, ['node', 'index.js', '--bool-flag', '--string-flag', 'val'], false); | ||
expect(res.unknownArgs.length).toEqual(0); | ||
expect(res.opts).toEqual({ | ||
boolFlag: true, | ||
stringFlag: 'val', | ||
stringFlagWithDefault: 'default-value', | ||
}); | ||
expect(warnMock.mock.calls.length).toEqual(0); | ||
}); | ||
|
||
it('handles unknown args', () => { | ||
const res = argParser(basicOptions, ['--unknown-arg', '-b', 'no-leading-dashes'], true); | ||
expect(res.unknownArgs).toEqual(['--unknown-arg', 'no-leading-dashes']); | ||
expect(res.opts).toEqual({ | ||
boolFlag: true, | ||
stringFlagWithDefault: 'default-value', | ||
}); | ||
expect(warnMock.mock.calls.length).toEqual(0); | ||
}); | ||
|
||
it('handles custom type args', () => { | ||
const res = argParser(basicOptions, ['--custom-type-flag', 'val1,val2,val3'], true); | ||
expect(res.unknownArgs.length).toEqual(0); | ||
expect(res.opts).toEqual({ | ||
customTypeFlag: ['val1', 'val2', 'val3'], | ||
stringFlagWithDefault: 'default-value', | ||
}); | ||
expect(warnMock.mock.calls.length).toEqual(0); | ||
}); | ||
|
||
it('calls help callback on --help', () => { | ||
const helpCb = jest.fn(); | ||
argParser(helpAndVersionOptions, ['--help'], true, '', helpCb); | ||
expect(helpCb.mock.calls.length).toEqual(1); | ||
expect(helpCb.mock.calls[0][0]).toEqual(['--help']); | ||
}); | ||
|
||
it('calls version callback on --version', () => { | ||
const versionCb = jest.fn(); | ||
argParser(helpAndVersionOptions, ['--version'], true, '', () => {}, versionCb); | ||
expect(versionCb.mock.calls.length).toEqual(1); | ||
}); | ||
|
||
it('parses webpack args', () => { | ||
const res = argParser(core, ['--entry', 'test.js', '--hot', '-o', './dist/', '--no-watch'], 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.watch).toBeFalsy(); | ||
expect(warnMock.mock.calls.length).toEqual(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can using
help
(https://github.com/tj/commander.js/#helpcb and https://github.com/tj/commander.js/#addhelpcommand) for help, same for version? Yes we can do it in futureThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, just don't want to do it here because it will probably involve some significant rewriting of https://github.com/webpack/webpack-cli/blob/next/packages/webpack-cli/lib/groups/HelpGroup.js