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

question-cache

Package Overview
Dependencies
Maintainers
2
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

question-cache

A wrapper around inquirer that makes it easy to create and selectively reuse questions.

  • 0.3.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

question-cache NPM version

A wrapper around inquirer that makes it easy to create and selectively reuse questions.

Example

var questions = require('question-cache');

questions()
  .set('first', 'What is your first name?')
  .set('last', 'What is your last name?')
  .ask(function (err, answers) {
    console.log(answers);
  });

Screen capture

screen shot 2015-07-13 at 8 05 20 am

See the working examples.

Install

Install with npm

$ npm i question-cache --save

Basic Usage

See the working examples.

var questions = require('question-cache')();

// question type "input" is used by default
questions
  .set('name', 'What is your name?')
  .ask('name', function (err, answers) {
    console.log(answers);
  });

inquirer

You may optionally pass your own instance of inquirer to the constructor:

// on the options
var questions = require('question-cache');
var questions = new Questions({
  inquirer: require('inquirer')
});

// or if inquirer is the only thing passed
var questions = new Questions(require('inquirer'));

Getting started

question-cache is a wrapper around inquirer. If you have any issues related to the interface (like scrolling, colors, styling, etc), then please create an issue on the inquirer project.

Asking questions

The simplest way to ask a question is by passing a string and a callback:

questions.ask('name', function (err, answers) {
  console.log(answers);
});

Ask all stored questions

questions.ask(function (err, answers) {
  console.log(answers);
});

Table of contents

See the working examples.

(Table of contents generated by verb)

API

Questions

Create an instance of Questions with the given options.

Params

  • options {Object}: Pass your instance of inquireron the inquirer option.

Example

var inquirer = require('inquirer')
var questions = new Questions({inquirer: inquirer});

.set

Store a question object by key.

Params

  • key {String}: Unique question id.
  • value {Object}: Question object that follows inquirerconventions.

Example

questions.set('name', {
  type: 'input',
  message: 'Project name?',
  default: 'undefined'
});

.get

Get a question by key.

Params

  • key {String}: Unique question id.
  • value {Object}: Question object that follows inquirerconventions.

Example

questions.get('name');
//=> {type: 'input', message: 'What is your name?', default: ''}

.has

Return true if the given question name is stored on question-cache.

Params

  • key {String}: Unique question id.
  • value {Object}: Question object that follows inquirerconventions.

Example

var exists = questions.has('name');
//=> true or false

.resolve

Returns an array of question objects from an array of keys. Keys may use dot notation.

Params

  • keys {Array}: Question names or object paths.
  • returns {Array}: Array of question objects.

.toQuestion

Create a question object from a string. Uses the input question type, and does the following basic normalization:

  • when foo is passed, a ? is added to the question. e.g. foo?
  • when foo? is passed, ? is removed on the question key, so the answer to foo? is {foo: 'bar'}

Params

  • key {String}
  • returns {Object}: Returns a question object.

.ask

Ask a question or array of questions.

Params

  • key {String}: Unique question id.
  • value {Object}: Question object that follows inquirerconventions.

Example

questions.ask(['name', 'homepage']);
//=> { name: 'foo', homepage: 'https://github/foo' }

.prompt

Exposes the prompt method on inquireras a convenience.

Params

  • question {Object|Array}: Question object or array of question objects.
  • callback {Object}: Callback function.

Example

questions.prompt({
  type: 'list',
  name: 'chocolate',
  message: 'What\'s your favorite chocolate?',
  choices: ['Mars', 'Oh Henry', 'Hershey']
}, function(answers) {
  //=> {chocolate: 'Hershey'}
});

Dot notation

See the working examples.

Qestions may be stored using object-path notatation (e.g. a.b.c).

Example

All of the following will be stored on the name object:

questions
  .set('name.first', 'What is your first name?')
  .set('name.middle', 'What is your middle name?')
  .set('name.last', 'What is your last name?')

Dot notation usage

When stored using dot-notation, there are a few different ways questions that may be asked.

Dot notation usage

ask one

Ask a single name question:

questions.ask('name.first', function (err, answers) {
  console.log(answers);
});
ask all "name" questions

Ask all name questions, first, middle and last:

questions.ask('name', function (err, answers) {
  console.log(answers);
});
array of questions

Ask specific questions on name:

questions.ask(['name.first', 'name.last'], function (err, answers) {
  console.log(answers);
});
ask all questions

Ask specific questions on name:

questions
  .set('name.first', {
    message: 'What is your first name?',
  })
  .set('name.last', {
    message: 'What is your last name?',
  })
  .set('foo', {
    message: 'Any thoughts about foo?',
  })

questions.ask(['name', 'foo'], function (err, answers) {
  console.log(answers);
});
nested questions

Ask one question at a time, based on feedback:

questions.ask('name.first', function (err, answers) {
  console.log(answers);
  //=> {name: { first: 'Brian' }}

  questions.ask('name.last', function (err, answers) {
    console.log(answers);
    //=> {name: { last: 'Woodward' }}
  });
});

More examples

Mixture of dot notation and non-dot-notation

Given you have the following questions:

questions
  .set('name.first', 'What is your first name?')
  .set('name.last', 'What is your last name?')
  .set('foo', 'Any thoughts about foo?')
  .set('bar', 'Any thoughts about bar?')

The following will ask questions: name.first, name.last and foo

questions.ask(['name', 'foo'], function (err, answers) {
  console.log(answers);
});

Thank you

@sboudrias, I appreciate having inquirer and I appreciate you for creating it! All that time and effort you put into it makes my life easier. Thank you!

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Author

Jon Schlinkert

License

Copyright © 2015 Jon Schlinkert Released under the MIT license.


This file was generated by verb-cli on October 17, 2015.

Keywords

FAQs

Package last updated on 21 Oct 2015

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