This is the second day of my participation in the August More text Challenge. For details, see: August More Text Challenge.

If Child_process is the cornerstone of automated builds, inquirer makes automated builds more agile and scalable.

First of all, what is an inquirer? The Inquirer is a tool that allows users to interact with the command line. It can be used everywhere, such as yes/no queries on the command line, remark input, list selection, and so on.

1. Introduction

The pain points inquirer addresses for its users are:

  • Provide error callbacks
  • Ask operator questions
  • Gets and parses user input
  • Check whether the user’s answer is valid
  • Manage multiple levels of prompts
  • The Inquirer provides a number of parameters for each question because of the different kinds of questions it interacts with:
Type: Indicates the type of the question, including: input, confirm, list, RawList, expand, Checkbox, Password, editor; Name: the variable that stores the current question answer; Message: description of the problem; Default: Default value. Choices: List options, available under certain types, and containing a separator; Validate: Validates the user's answer; Filter: filters the user's answers and returns the processed value. Transformer: Processing the display of the user's response (e.g. changing the font or background color of the response) without affecting the content of the final answer; When: Judging whether the current question needs to be answered based on the answers to the previous question; PageSize: Changes the number of render rows for certain types; Prefix: Changes the default prefix of message. Suffix: Changes the default suffix of message.Copy the code

The Inquirer returns a promise object that can be called back through the.then() method, better handling the asynchronous time we need.

2. Grammatical structure

const inquirer = require('inquirer');

const promptList = [
    
];

inquirer.prompt(promptList).then(result => {
    console.log(result);
})
Copy the code

3. The type classification

– input

Input boxes, which are generally used to allow users to fill in information and so on, can also be checked by regular

Const promptList = [{type: 'input', Message :' Please input mobile phone number :', Name :' phone', validate: Function (val) {if(val.match(/\d{11}/g)) {return val;} return "Please enter 11 digits ";}}]Copy the code

– confirm

This is also common, for example when we stop the command, let’s select yes/no

Const promptList = [{type: "confirm", Message: "Whether to terminate? ", name:" terminate ", prefix: "prefix ", suffix:" suffix ",}];Copy the code

– list

List is used for multiple choices. When you just want the user to choose between the answers you’ve given, you can use list to limit efficiency. Enter is faster than typing.

Const promptList = [{type: 'list', Message :' Please select :', name: 'options', choices: Filter: function (val) {return val.tolowerCase ();}}];Copy the code

– password

Password input, can hide your input value

Const promptList = [{type: "password", message: "Please enter the password: ", name:" PWD "}];Copy the code

4. The last

The Inquirer is the icing on the cake, taking your project to the next level and making it more comfortable to use.