npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-ask

v1.0.1

Published

Command line keyboard entry using promises

Downloads

285,831

Readme

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:

| Function | Description | |---|---| | 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:

| Property | Description | |---|---| | key | The name of the property, in the answers object, to use for storing the user entered response. | | msg | The message to output before waiting for the user to respond. | | fn | The type of function you want called.Valid values are:'prompt', 'confirm', or 'mulitline'

License:

MIT