Inquirer middleware engine. Create interactive cli prompts with a middleware engine for handling individual answers.
import'@babel/register';// enable async/awaitimportinquisitivefrom'inquisitive';importdelayfrom'delay';constinq=inquisitive();// define a prompt with a middleware handlerinq.use((prompt)=>{// ask a questionprompt({name: 'name',message: 'What is your name',});// handle the answersreturnasync(answers,status,next)=>{if(answers.name!=='Nicolas Cage'){thrownewError('Legends only');}awaitdelay(1000);awaitnext();// go to next handler};});inq.run();// start askingnpm install --save inquisitiveyarn add inquisitiveInquisitive uses async/await, until this is available for production it is recommended to transpile with Babel.
Use the transform plugin, babel-plugin-transform-async-to-generator.
The concept of middleware in inquisitive is slightly different than common patterns like in Express or Koa. A middleware function is invoked immediately.
Middleware functions have two jobs, ask questions and handle answers. Both of which are optional.
Middleware functions will be given a prompt function as it's only argument. This prompt function should be called to add questions to inquirer.
inq.use((prompt)=>{// ask anythingprompt({name: 'first',message: 'What is your first name:',});// and many more...prompt({name: 'last',message: 'What is your last name:',});});Asking questions is completely optional. In some cases only an answers handler is required as prior middleware may have asked all the questions.
See inquirer Questions.
Once inquirer has complete the answers it will run through the answer handlers. To give a handler to inquisitive just return an async function from the middleware function.
Answer handler functions take 3 arguments, answers, status and next.
inq.use((prompt)=>{// ask questions?returnasync(answers,status,next)=>{status('checking something');// update spinner textdoSomething(answers.foo);// handle answersawaitnext();// don't forget to move next};});Handling answers is also optional. Some middleware may just want to ask questions and have another middleware handle the answers.
To run the interactive prompt call #run() on the inquisitive instance. run also takes a few options to control how inquisitive will report to the terminal.
inq.run({args: true,// read cli args and set question default valuesspinner: true,// enable/disable spinnertime: true,// enable/disable time message at end});Inquisitive has the ability to read args from the terminal and set them as the defaults for questions. This is a convenience options enabled by default.
cli.js
inq.use((prompt)=>{prompt({name: 'name',message: 'What is your name:',});});inq.run();node cli.js --name "Nicolas Cage"? What is your name: (Nicolas Cage) _It is also possible to give a custom built inquirer module to inquisitive. Just pass it into the factory method when creating an instance.
importinquirerfrom'inquirer';importinquisitivefrom'inquisitive';constcustom=inquirer.createPromptModule();// apply customisationsconstinq=inquisitive(custom);module: FunctionCustom inquirer module.
Returns inquisitive instance.
fn: FunctionMiddleware function to add to instance.
otps: ObjectRun options.opts.args: BooleanEnable/disable argument defaults.opts.spinner: BooleanEnable/disable spinner.opts.time: BooleanEnable/disable time feedback on success.
Returns Promise, resolving answers.
Copyright (c) 2016 9Technology