Introduction to the

Inquirer Interactive command line information collector. Version v8.8.1

The installation

npm i Inquirer
Copy the code

Using the example

async function run(){


  try {
    const answers = await inquirer.prompt([
      {
        type: 'input'.name: 'project'.message: 'Project Name'.default: 'copyLeft'}, {type: 'list'.name: 'type'.message: 'Project Type'.default: 'vue'.choices: [{name: 'vue'.value: 'vue' },
          { name: 'react'.value: 'react' },
          { name: 'jq'.value: 'jq'}},]])console.log('Answer:', answers)
    
  } catch (error) {
    console.log(` error:${error}`)}}Copy the code

Notifiers promp

Inquirer. Prompt (questions, answers) → Promise The prompt receives preset interactive questions configuration and preset answers. Return a Promise object containing the interaction collection information

  • A list of questions
  • Answers default or collected question answers, and for answered questions, the corresponding question collection is skipped.
const questions = [
  {
    // Problem object
    type: 'input'.name: 'project'.message: 'Project Name'.default: 'copyLeft'}, {type: 'list'.name: 'type'.message: 'Project Type'.default: 'vue'.choices: [{name: 'vue'.value: 'vue' },
      { name: 'react'.value: 'react' },
      { name: 'jq'.value: 'jq'}},]]const answers = {
  project: 'demo'
}
// The question of 'project name' will be skipped after setting answers
inquirer.prompt(questions, answers)  
Copy the code

Question to ask

{
  type: 'input' // Interaction component type
  name: 'name' // Data attribute name
  message: 'Username' // Interactive prompts
  default: ' ' / / the default value
  choices: ' ' // When the interaction type is' select type ', this property configures optional items


  // Validates the function, which takes the current answer as an argument. Returns: true yes False No, Error No, Error information is displayed
  validate(value){
    return! value.length ?new Error('Project name cannot be empty') : true
  }
  
  // filter to return the modified answer. Priority is higher than 'validte'
  filter(value){
     return /vue/.test(value) ? `${value}-demo` : value
  }
  // Converter, returns the converted value, only as a display, does not affect the collection results
  transformer(value){
     return /vue/.test(value) ? `${value}-demo` : value
  },
  // Whether to display a problem
  when(answers){
     return!!!!! answers.company },/ / the message prefix
  prefix: ' './ / the message suffix
  suffix: ' '.// If the answer already exists, ask the question again
  askAnswered: false.// 
}
Copy the code

Answers to answer

The answer is a hash object for key: value D. Key: corresponds to the name attribute value in the question. Value: The value varies with the interaction type

{
  // value Specifies the value type
  confirm: boolean
  input: string
  number: number
  rawlist/list: value 
}
Copy the code

Interaction component types

  • The input fields
const input = {
  type: 'input'.name: 'Input field'.message: 'input',}Copy the code
  • Number Indicates the number input box
const number = {
  type: 'number'.name: 'Number entry field'.message: 'number',}Copy the code
  • Password the password
const password = {
  type: 'password'.name: 'Password box'.message: 'password',}Copy the code
  • The list of radio
const list = {
  type: 'list'.name: 'radio'.message: 'list'.choices: [{name: '1'.value: 1 },
    { name: '2'.value: 2 },
    { name: '3'.value: 3}}]Copy the code
  • Rawlist list
const rawlist = {
  type: 'rawlist'.name: 'list'.message: 'rawlist'.choices: [{name: '1'.value: 1 },
    { name: '2'.value: 2 },
    { name: '3'.value: 3}}]Copy the code
  • The checkbox multi-select
const checkbox = {
  type: 'checkbox'.name: 'alternative'.message: 'checkbox'.choices: [{name: '1'.value: 1 },
    { name: '2'.value: 2 },
    { name: '3'.value: 3}}]Copy the code
  • Confirm to judge
const confirm = {
  type: 'confirm'.name: 'judgment'.message: 'confirm',}Copy the code
  • Editor Text editor
const editor = {
  type: 'editor'.name: 'Editor'.message: 'editor',}Copy the code

The plug-in

Loading external plug-ins

Inquirer. RegisterPrompt (Plugin name, plugin package)'selectLine'.require('inquirer-select-line'));
Copy the code

other

The Separator Separator

inquirer.prompt([
  {
    type: 'checkbox'.name: 'alternative'.message: 'checkbox'.choices: [ "Choice A".new inquirer.Separator(), "choice B"]}])Copy the code