-
Notifications
You must be signed in to change notification settings - Fork 147
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
Coalesce boolean arguments to actual Boolean type instead of string #763
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6b69ac1
feat(parameters-tools): add yargsParse type
joshuayoes 4cbbed9
feat(package.json): add watch:debug script
joshuayoes 0ac6162
feat(parameters-tools.test): add coalesce boolean flag tests
joshuayoes 0a365c4
feat(parameter-tools): normalize boolean strings to boolean type
joshuayoes 7690bd1
fix(runtime-parameters.test): update 'true' assert to true
joshuayoes 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { parseParams } from './parameter-tools' | ||
|
||
describe('parameter-tools', () => { | ||
describe('parseParams', () => { | ||
const cases = [ | ||
{ | ||
name: 'should coalesce --key=false boolean arguments', | ||
input: 'node ignite/bin/ignite new PizzaApp --overwrite=false', | ||
output: { array: ['node', 'ignite/bin/ignite', 'new', 'PizzaApp'], options: { overwrite: false } }, | ||
}, | ||
{ | ||
name: 'should coalesce --key=true boolean arguments', | ||
input: 'node ignite/bin/ignite new PizzaApp --overwrite=true', | ||
output: { array: ['node', 'ignite/bin/ignite', 'new', 'PizzaApp'], options: { overwrite: true } }, | ||
}, | ||
{ | ||
name: 'should coalesce --key true boolean arguments', | ||
input: 'node ignite/bin/ignite new PizzaApp --overwrite true', | ||
output: { array: ['node', 'ignite/bin/ignite', 'new', 'PizzaApp'], options: { overwrite: true } }, | ||
}, | ||
{ | ||
name: 'should coalesce --key false boolean arguments', | ||
input: 'node ignite/bin/ignite new PizzaApp --overwrite false', | ||
output: { array: ['node', 'ignite/bin/ignite', 'new', 'PizzaApp'], options: { overwrite: false } }, | ||
}, | ||
{ | ||
name: 'should coalesce --key flag with no value to true', | ||
input: 'node ignite/bin/ignite new PizzaApp --overwrite', | ||
output: { array: ['node', 'ignite/bin/ignite', 'new', 'PizzaApp'], options: { overwrite: true } }, | ||
}, | ||
{ | ||
name: 'should coalesce --key=0 number arguments to number type', | ||
input: 'node ignite/bin/ignite new PizzaApp --port=8080', | ||
output: { array: ['node', 'ignite/bin/ignite', 'new', 'PizzaApp'], options: { port: 8080 } }, | ||
}, | ||
] | ||
|
||
cases.forEach(({ name, input, output }) => { | ||
it(name, () => { | ||
expect(parseParams(input)).toEqual(output) | ||
}) | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { GluegunParameters } from '../domain/toolbox' | ||
import { Options } from '../domain/options' | ||
import { equals, is } from './utils' | ||
import type yargsParser from 'yargs-parser' | ||
|
||
const COMMAND_DELIMITER = ' ' | ||
|
||
|
@@ -12,7 +13,7 @@ const COMMAND_DELIMITER = ' ' | |
* @returns Normalized parameters. | ||
*/ | ||
export function parseParams(commandArray: string | string[], extraOpts: Options = {}): GluegunParameters { | ||
const yargsParse = require('yargs-parser') | ||
const yargsParse: yargsParser.Parser = require('yargs-parser') | ||
|
||
// use the command line args if not passed in | ||
if (is(String, commandArray)) { | ||
|
@@ -31,7 +32,19 @@ export function parseParams(commandArray: string | string[], extraOpts: Options | |
const parsed = yargsParse(commandArray) | ||
const array = parsed._.slice() | ||
delete parsed._ | ||
const options = { ...parsed, ...extraOpts } | ||
const normalizedParsed: Options = Object.fromEntries( | ||
Object.entries(parsed).map(([key, value]) => { | ||
// if value is 'true' or 'false', convert to boolean | ||
if (value === 'true') { | ||
return [key, true] | ||
} | ||
if (value === 'false') { | ||
return [key, false] | ||
} | ||
return [key, value] | ||
}), | ||
Comment on lines
+36
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd use the simplified version here that I mentioned in the related Ignite PR. |
||
) | ||
const options = { ...normalizedParsed, ...extraOpts } | ||
return { array, options } | ||
} | ||
|
||
|
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.
Smart to use an
import type
here.