-
-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: use commander * chore: add support for help and version command * chore: remove duplicate code for no-mode * chore: create helpers * chore: remove global flag * chore: improve arg parsing and tests * chore: remove command-line-args * chore: remove instance of command-line-args * chore: add explaination to the change * chore: fix * chore: migrate serve package * chore: handle error * chore: reactor argparser * chore: get serve args from process.argv * chore: sync message with test * chore: fix styling * chore: fix problem with no mode flag * chore: started adding info parsing * chore: parsing without first few parts of process argv * chore: fixed args calling in serve * chore: fixing serve * chore: fix no-mode tests * chore: fix output defaults tests * chore: handle default webpack entry arg * chore: fix basic config test * chore: fix version and help commands * chore: change help, version, and fix entry checking * chore: add some negated flag handling * chore: fix serve test * chore: rename misleading parse-args to parse-node-args * chore: added arg-parser tests * chore: use args passed to serve * chore: fix stats flag handling * chore: fix function as a custom type * chore: change boolean logic in arg parser * chore: fixed stats test and removed stats custom type * chore: removed output todo and added tests for warning * chore: changed invalid mode message and updated test * chore: add comment about commander negation * chore: made output tests sync * chore: fix spelling in comment * chore: remove unnecessary log * chore: add runPromptWithAnswers changes for stability Co-authored-by: Rishabh Chawla <rishabh31121999@gmail.com> Co-authored-by: James George <jamesgeorge998001@gmail.com>
- Loading branch information
1 parent
4a64f0f
commit 8892926
Showing
32 changed files
with
542 additions
and
285 deletions.
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.