Skip to content

Latest commit

 

History

History
121 lines (82 loc) · 2.4 KB

.verb.md

File metadata and controls

121 lines (82 loc) · 2.4 KB

Release history

See the changelog for detailed release history.

What is this?

prompt-base is a node.js library for creating command line prompts. You can use prompt-base directly for simple input prompts, or as a "base" for creating custom prompts:

Usage

See the examples folder for additional usage examples.

var Prompt = require('{%= name %}');
var prompt = new Prompt({
  name: 'color',
  message: 'What is your favorite color?'
});

// promise
prompt.run()
  .then(function(answer) {
    console.log(answer);
    //=> 'blue'
  })

// or async
prompt.ask(function(answer) {
  console.log(answer);
  //=> 'blue'
});

You can also pass a string directly to the main export:

var prompt = require('{%= name %}')('What is your favorite color?');
  
prompt.run()
  .then(function(answer) {
    console.log(answer);
  })

Custom prompts

Inherit

var Prompt = require('{%= name %}');

function CustomPrompt(/*question, answers, rl*/) {
  Prompt.apply(this, arguments);
}

Prompt.extend(CustomPrompt);

API

{%= apidocs("index.js") %}

Events

prompt

Emitted when a prompt (plugin) is instantiated, after the readline interface is created, but before the actual "question" is asked.

Example usage

enquirer.on('prompt', function(prompt) {
  // do stuff with "prompt" instance
});

ask

Emitted when the actual "question" is asked.

Example usage

Emit keypress events to supply the answer (and potentially skip the prompt if the answer is valid):

enquirer.on('ask', function(prompt) {
  prompt.rl.input.emit('keypress', 'foo');
  prompt.rl.input.emit('keypress', '\n');
});

Change the prompt message:

enquirer.on('ask', function(prompt) {
  prompt.message = 'I..\'m Ron Burgundy...?';
});

answer

Emitted when the final (valid) answer is submitted, and custom validation function (if defined) returns true.

(An "answer" is the final input value that's captured when the readline emits a line event; e.g. when the user hits enter)

Example usage

enquirer.on('answer', function(answer) {
  // do stuff with answer
});

In the wild

The following custom prompts were created using this library:

{%= related(verb.related.prompts) %}