Last active
September 21, 2020 06:47
-
-
Save kenanchristian/9ea9307658c9d998481e5fd979d5112e to your computer and use it in GitHub Desktop.
[[TypeScript CLI] Make Interactive Mode Optional] #cli #oclif
This file contains hidden or 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
... | |
async getInteractiveArgs() { | |
const answer = await prompt([ | |
{ | |
type: 'number', | |
name: 'count', | |
message: 'How many pizza you want to create', | |
default: 1, | |
validate(value) { | |
if (isNaN(parseInt(value, 10))) { | |
return 'Pizza count should be a number' | |
} | |
return true | |
}, | |
}, | |
{ | |
type: 'list', | |
name: 'crust', | |
message: 'How do you want you pizza crust be?', | |
default: 'Thin', | |
choices: ['Thin', 'Thick'], | |
}, | |
{ | |
type: 'checkbox', | |
name: 'toppings', | |
message: 'What do you want to add as toppings?', | |
default: '', | |
choices: [ | |
{ | |
name: 'π Pepperoni', | |
value: 'pepperoni', | |
}, | |
{ | |
name: 'π Mushroom', | |
value: 'mushroom', | |
}, | |
{ | |
name: 'π₯ Bacon', | |
value: 'bacon', | |
}, | |
{ | |
name: 'π Pineapple', | |
value: 'pineapple', | |
}, | |
], | |
validate(value) { | |
if (value.length === 0) { | |
return 'You should add at least 1 topping' | |
} | |
return true | |
}, | |
}, | |
{ | |
type: 'confirm', | |
name: 'extraSauce', | |
message: 'Do you want extra sauce?', | |
default: false, | |
}, | |
]) | |
return answer | |
} | |
async run() { | |
const {args, flags} = this.parse(Create) | |
const {count} = args | |
const {toppings, crust, extraSauce} = flags | |
let pizzaData: PizzaData | |
if (count !== null && count > 0 && toppings.length > 0 && crust) { | |
pizzaData = {count, toppings, crust, extraSauce} | |
} else { | |
pizzaData = await this.getInteractiveArgs() | |
} | |
this.log(JSON.stringify(pizzaData)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment