JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 171001
  • Score
    100M100P100Q169984F
  • License MIT

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

Package Exports

  • question-cache

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (question-cache) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

question-cache NPM version

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

Example

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

questions
  .set('first', 'What is your first name?')
  .set('last', 'What is your last name?')
  .ask(function (err, answers) {
    console.log(answers);
    //=> { first: 'Brian', last: 'Woodward' }
  });

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');

// you must pass your own instance of inquirer!
var questions = new Questions({
  inquirer: require('inquirer')
});

// question `type` "input" is used by default
questions.set('name', 'What is your name?');

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

Getting started

First things first, question-cache is just a wrapper around inquirer. If you have an issue related to the actual console interface (like scrolling, colors, styling, etc), then please create issues 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);
});
  • ask-once: Only ask a question one time and store the answer.
  • generate: Project generator, for node.js.
  • question-helper: Template helper that asks a question in the command line and resolves the template with… more

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 August 09, 2015.