Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-ask

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-ask

Command line keyboard entry using promises

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
59K
increased by3.87%
Maintainers
1
Weekly downloads
 
Created
Source

node-ask

A simple, Promise based, keyboard input system for node.js

node-ask provides a simple set of functions to allow a node.js application to ask a question and get keyboard input for a response.

node-ask requires a version of node that supports native Promises. That is version 4.0 or greater or you can use version 0.12 with the --harmony flag turned on.



Basic Functions

The three basic functions of node-ask are:

FunctionDescription
prompt(<message>)Output the <message> to stdout and wait for keyboard input from stdin. When the user pressed the RETURN key prompt will resolve with whatever the user entered minus the RETURN key
multiline(<message>)Output the <message> to stdout and wait for keyboard input from stdin. When the user pressed the RETURN key on a blank line multiline will resolve with whatever the user entered minus the last RETURN key
confirm(<message>)Output the <message> to stdout and wait for keyboard input from stdin. When the user pressed the RETURN key, if the keyboard input was y, yes, okay or true then confirm will resolve with true otherwise it will resolve with false.

Examples:

const prompt = require('node-ask').prompt;
const confirm = require('node-ask').confirm;
const multiline = require('node-ask').multiline;

prompt('What is your name? ').then(
  function(answer) {
    console.log('Your name is', answer);
    return confirm('Are you living? ');
  }
).then(
  function(answer) {
    console.log('You '+(answer?'are':'are not')+' living');
    return multiline('Describe yourself:');
  }
).then(
  function(answer) {
    console.log('You described yourself as:', answer);
  }
);

Asking multiple questions in one call

node-ask allows for a single Promise based function to ask the user several questions through one call instead of having to make a separate call per question.

ask(<questions>)

The ask function takes <questions>, an array of objects, to indicate the series of questions to ask and what type of function should be used per question. Each object in the array also defines the property name to be used in the resolved answer object that will contain the user's responses.

While prompt, confirm and multiline always resolve their promises, ask will resolve its promise unless there is an error in the format of your <questions> array. So it is important that you use .catch anytime you call ask.

var questions = [
  { key: 'name',   msg: 'What is your name? ',       fn: 'prompt' },
  { key: 'living', msg: 'Are you living? ',          fn: 'confirm' },
  { key: 'age',    msg: 'How old are you? ',         fn: 'prompt' },
  { key: 'hair',   msg: 'What color is your hair? ', fn: 'prompt' },
  { key: 'big',    msg: 'Give us a description:',    fn: 'multiline' }
];

nodeAsk.ask(questions).then(
  function(answers) {
    console.log(JSON.stringify(answers,0,2));
  }
).catch(
  function(ex) {
    // Do your error management here
    console.log(ex.stack);
  }
);

Based on the <questions> array in the above example the answers object that is resolved by ask will look like this:

answers = {
  name: <string>,
  living: <boolean>,
  age: <string>,
  hair: <string>,
  big: <multi-lined string>
};

<questions> is an array of objects that must contain all of the following parameters:

PropertyDescription
keyThe name of the property, in the answers object, to use for storing the user entered response.
msgThe message to output before waiting for the user to respond.
fnThe type of function you want called.

Valid values are:
'prompt', 'confirm', or 'mulitline'

License:

MIT

Keywords

FAQs

Package last updated on 07 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc