inquirer模块

1,219 阅读3分钟

说明

inquirer主要是用通过命令行交互,来选择一些基座信息。具有良好的api接口,可以很方便的使用它来进行脚手架中命令行的创建。

yarn add inquirer
# 版本为8.2.0

使用

inquirer通过配置question对象和answer回答对象,指导用户在命令行中进行选择,最终会生成一份选择答案。可以根据选择答案进行后续的流程处理。

inquirer.prompt(question:array, answer:object) -> promise

主要接口,可以配置最终的选择列表question,answer可选,如果配置了answer,则在question选择时,不会再进行选择此question的问题。返回promise格式。

const question = [
    {
        type: 'list', // 问题类型,默认是input,可以选input, number, confirm, list, rawlist, expand, checkbox, password, editor
        name: 'size', // 问题名称,可以在answer中的key一致
        message: '选择一个大小', // 显示在命令行上消息。默认是配置name的值
        // 配置选择项,可以是数组,也可以是function,并返回一个数组
        // 数组可以是number、string、object
        // object.key 显示在选择后;object.name在进行选择后的显示;object.value保存在answer中
        choices: ['Larger', 'Medium', 'Smaller'], 
        filter (val) {
            return val.toLowerCase()
        }
    }
]
inquirer.prompt(question).then(answer => {
    console.log(answer)
})

1636527580663.gif

question.type

  1. list: 配置为list时,最少需要配置name、message、choices信息,默认会有filter(默认返回choices的值),loop(默认为true,可以循环定位choices选择项)

68747470733a2f2f63646e2e7261776769742e636f6d2f53426f7564726961732f496e7175697265722e6a732f323861653833333762613531643933653335396566346637656532346537396236393839383936322f6173736574732f73637265656e73686f74732f6c6973742e73766.png

  1. rawlist: 配置同list。但是此配置选择项为有序选择,及会显示数字选择。最终输入数字进行选择

68747470733a2f2f63646e2e7261776769742e636f6d2f53426f7564726961732f496e7175697265722e6a732f323861653833333762613531643933653335396566346637656532346537396236393839383936322f6173736574732f73637265656e73686f74732f7261776c6973742.png

  1. expand:配置信息choices必须是object类型数组。其中object.k必须是单个字符。
choices: [
            {
              key: 'p',
              name: 'Pepperoni and cheese',
              value: 'PepperoniCheese',
            },
            {
              key: 'a',
              name: 'All dressed',
              value: 'alldressed',
            },
            {
              key: 'w',
              name: 'Hawaiian',
              value: 'hawaiian',
            },
        ]

1.png2.png

  1. checkbox: 配置用户选择某些问题(包括多选和单选)。其中choice中,主要配置name信息,如果配置checked为true,表示默认选择。

    disabled设置为字符串时,会显示在选择列表后。disabled为true时,表示此项不可选择
    

    同时可以设置validate属性,对选择的选项进行校验。

choices: [
            new inquirer.Separator(' = The Meats = '),
            {
                name: 'Pepperoni'
            },
            {
                name: 'Ham',
                checked: true
            },
            {
                name: 'Mushroom',
                disabled: 'out of stock',
            }
        ]

1.jpg

  1. confirm: 配置yes/no选择信息。可以配置默认信息default
{
        type: 'confirm',
        name: 'toBeDelivered',
        message: 'Is this for delivery?',
        default: false,
    }

1.jpg

  1. input: 配置用户输入信息,此信息可以配置validate、filter、transformer.
{
  validate(value) {
    const pass = value.match(
      /^([01]{1})?[-.\s]?\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})\s?((?:#|ext\.?\s?|x\.?\s?){1}(?:\d+)?)?$/i
    );
    if (pass) {
      return true;
    }

    return 'Please enter a valid phone number';
  },
},
{
  transformer(color, answers, flags) {
    const text = chalkPipe(color)(color);
    if (flags.isFinal) {
      return text + '!';
    }

    return text;
  },
},
  1. number:配置数字类型的选择
  2. password:配置密码类型的选择,脱敏显示。mask可以设置显示*,如果不设置,则不会输出输入信息
{
  type: 'password',
  message: 'Enter a password',
  name: 'password1',
  validate: requireLetterAndNumber,
},
{
  type: 'password',
  message: 'Enter a masked password',
  name: 'password2',
  mask: '*',
  validate: requireLetterAndNumber,
},

1.jpg

  1. editor: 此配置会启用一个本地的编辑器,进行编辑信息,关闭后会读取内容。
{
  type: 'editor',
  name: 'bio',
  message: 'Please write a short bio of at least 3 lines.',
  validate(text) {
    if (text.split('\n').length < 3) {
      return 'Must be at least 3 lines.';
    }

    return true;
  },
},

1.jpg

question.name

和answer中的key对应。

question.message

在选择列表中显示,默认为name的值。一般为字符串

question.default

设置此question的默认值,根据type设置不同,有不同类型的默认值。

question.choices

设置选择项,可以是Array,也可以是function,

question.validate

对输入进行校验,校验成功返回true,否则,可以返回错误信息,或false

question.filter

对输入或答案进行筛选,并最终添加到答案中

question.transformer

根据输入、答案、flags进行转换,此信息只是呈现在命令行中,不会对最终存储的答案有影响

question.when

根据上次answer的选择,判断此次question是否需要选择,如果返回true则会进行本次question的问答,如果false则会跳过本次问答

{
  type: 'confirm',
  name: 'bacon',
  message: 'Do you like bacon?',
},
{
  type: 'input',
  name: 'favorite',
  message: 'Bacon lover, what is your favorite type of bacon?',
  when(answers) {
    return answers.bacon; // 在第一个question中设置yes时,answers.bacon才会显示true。
  },
},

question.pageSize

在使用list、rawList、expand、checkbox时,配置选择列表项的行数显示。

question.prefix

设置question的前缀显示。默认是?可以设置其他的字符

question.suffix

设置question的后缀显示。默认不显示。

question.askAnswered

如果已经存在答案,是否还提出question

question.loop

是否循环显示选项,经常在list、rawList等列表选项中使用,默认为true ​

分割separator

在添加choices时可以添加分隔符信息,比如

choices: [ "Choice A", new inquirer.Separator(), "choice B" ]

inquirer.registerPrompt(name, prompt)

注册inquirer插件包。

inquirer.createPromptModule() -> prompt function

创建prompt对象,一般在需要给prompt添加自定义类型,或重写方法时使用。