Skip to content

Instantly share code, notes, and snippets.

@kenanchristian
Last active September 21, 2020 06:47
Show Gist options
  • Save kenanchristian/9ea9307658c9d998481e5fd979d5112e to your computer and use it in GitHub Desktop.
Save kenanchristian/9ea9307658c9d998481e5fd979d5112e to your computer and use it in GitHub Desktop.
[[TypeScript CLI] Make Interactive Mode Optional] #cli #oclif
...
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