cac
javascript製のCLI作成ライブラリ。cacはCommand And Conquerの頭文字を取ったものだそう。なぜConquerなのかは。。。
import cac from 'cac'
const cli = cac()
cli.command('Scream <word>', 'Scream someting')
.option('--color-red', 'font color red')
.option('--color-blue', 'font color blue')
.action((word, option) => {
let fontColorEscape = ''
if (option.colorRed) {
fontColorEscape = '\x1b[31m'
} else if (option.colorBlue) {
fontColorEscape = '\x1b[34m'
}
console.log(`${fontColorEscape}${word}`)
})
cli.help()
cli.parse()
inquirer
対話型CLIを簡単に作成できるjavascript製ライブラリ。
import { prompt, QuestionCollection } from 'inquirer'
export default async (): Promise<void> => {
// input
const givenNameField: QuestionCollection = [
{
type: 'input',
message: "What's your given name?",
name: 'givenName'
}
]
const givenName = await prompt(givenNameField).then((givenName: any) => {
return (givenName as { 'givenName': string })['givenName']
})
// required
const familyNameField: QuestionCollection = [
{
type: 'input',
message: "What's your family name?",
name: 'familyName',
validate: (answer: any): boolean | string => {
if (answer.length < 1) {
return 'Required fields'
}
return true
}
}
]
const familyName = await prompt(familyNameField).then((familyName: any) => {
return (familyName as { 'familyName': string })['familyName']
})
// select
const listQuestions: QuestionCollection = [
{
type: 'list',
name: 'address',
message: 'Where is your address?',
choices: [
'Shinjuku',
'Shibuya',
{
name: 'Yotsuya',
disabled: 'disabled'
},
'Nakano'
]
}
]
const address = await prompt(listQuestions).then((address: any) => {
return (address as { 'address': string })['address']
})
// multiSelect
const checkboxQuestions: QuestionCollection = [
{
type: 'checkbox',
message: 'What is your favorite color?',
name: 'color',
choices: [
{ name: 'red' },
{ name: 'green' },
{ name: 'blue' }
]
}
]
const colors = await prompt(checkboxQuestions).then((answers: any) => {
return (answers as { 'color': string[] })['color']
})
console.log(
`Name: "${ givenName } ${ familyName }"
Address: "${ address }"
Color: "${ colors.join(', ') }"`
)
}