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

Coalesce boolean arguments to actual Boolean type instead of string #763

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"integration": "yarn build && jest --config=./.circleci/jest-integration.config.json",
"test": "jest",
"watch": "jest --watch",
"watch:debug": "yarn watch --runInBand --verbose",
"snapupdate": "jest --updateSnapshot",
"windows:test": "yarn && yarn format && yarn test && yarn clean-build && yarn compile && yarn copy-templates && jest --config=./.circleci/jest-integration.config.json",
"ci:test": "yarn lint && yarn test --maxWorkers=2 && yarn integration",
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/runtime-parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ test('can pass arguments with mixed options', async () => {
expect(parameters.command).toBe('hello')
expect(parameters.options.foo).toBe(true)
expect(parameters.options.n).toBe(1)
expect(parameters.options.chocolate).toBe('true')
expect(parameters.options.chocolate).toBe(true)
})

test('properly infers the heirarchy from folder structure', async () => {
Expand Down
44 changes: 44 additions & 0 deletions src/toolbox/parameter-tools.test.ts
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)
})
})
})
})
17 changes: 15 additions & 2 deletions src/toolbox/parameter-tools.ts
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'
Copy link
Member

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.


const COMMAND_DELIMITER = ' '

Expand All @@ -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)) {
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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 }
}

Expand Down