question-cache
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
See the working examples.
Install
Install with npm
$ npm i question-cache --save
Basic Usage
See the working examples.
var questions = require('question-cache')();
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:
var questions = require('question-cache');
var questions = new Questions({
inquirer: require('inquirer')
});
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.
API
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});
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 a question by key
.
Params
key
{String}: Unique question id.value
{Object}: Question object that follows inquirerconventions.
Example
questions.get('name');
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');
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.
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 a question or array of questions.
Params
key
{String}: Unique question id.value
{Object}: Question object that follows inquirerconventions.
Example
questions.ask(['name', 'homepage']);
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) {
});
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);
questions.ask('name.last', function (err, answers) {
console.log(answers);
});
});
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!
Related
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 22, 2015.