question-store
Ask questions, persist the answers. Basic support for i18n and storing answers based on current working directory.
TOC
(TOC generated by verb using markdown-toc)
Install
Install with npm:
$ npm install question-store --save
Similar to question-cache, but persists answers to disk and supports locales and storing answers based on current working directory.
Usage
var Questions = require('question-store');
API
Create an instance of Questions
with the given options
.
Params
options
{Object}: question store options
Example
var Questions = new Questions(options);
Create stores for persisting data across sessions.
globals
: Persist non-project-specific answers when question.options.global
is truestore
: Persist project-specific answershints
: Persist project-specific hints. This is used to populate the question.default
value.
Params
options
{Object}returns
{Object}
Calls addQuestion, with the only difference being that .set
returns the questions
instance and .addQuestion
returns the question object. So use .set
if you want to chain questions, or .addQuestion
if you need the created question object.
Params
name
{Object|String}: Question name, message (string), or question/options object.value
{Object|String}: Question message (string), or question/options object.options
{Object|String}: Question/options object.
Example
questions
.set('drink', 'What is your favorite beverage?')
.set('color', 'What is your favorite color?')
.set('season', 'What is your favorite season?');
questions.set('drink', {
type: 'input',
message: 'What is your favorite beverage?'
});
questions.set({
name: 'drink'
type: 'input',
message: 'What is your favorite beverage?'
});
Add a question to be asked at a later point. Creates an instance of Question, so any Question
options or settings may be used. Also, the default type
is input
if not defined by the user.
Params
name
{Object|String}: Question name, message (string), or question/options object.value
{Object|String}: Question message (string), or question/options object.options
{Object|String}: Question/options object.
Example
questions.addQuestion('drink', 'What is your favorite beverage?');
questions.addQuestion('drink', {
type: 'input',
message: 'What is your favorite beverage?'
});
questions.addQuestion({
name: 'drink'
type: 'input',
message: 'What is your favorite beverage?'
});
Create a "choices" question from an array of values.
Params
queue
{String|Array}: Name or array of question names.options
{Object|Function}: Question options or callback functioncallback
{Function}: callback function
Example
questions.choices('foo', ['a', 'b', 'c']);
questions.choices('foo', {
message: 'Favorite letter?',
choices: ['a', 'b', 'c']
});
Create a "list" question from an array of values.
Params
queue
{String|Array}: Name or array of question names.options
{Object|Function}: Question options or callback functioncallback
{Function}: callback function
Example
questions.list('foo', ['a', 'b', 'c']);
questions.list('foo', {
message: 'Favorite letter?',
choices: ['a', 'b', 'c']
});
Create a "rawlist" question from an array of values.
Params
queue
{String|Array}: Name or array of question names.options
{Object|Function}: Question options or callback functioncallback
{Function}: callback function
Example
questions.rawlist('foo', ['a', 'b', 'c']);
questions.rawlist('foo', {
message: 'Favorite letter?',
choices: ['a', 'b', 'c']
});
Create an "expand" question from an array of values.
Params
queue
{String|Array}: Name or array of question names.options
{Object|Function}: Question options or callback functioncallback
{Function}: callback function
Example
questions.expand('foo', ['a', 'b', 'c']);
questions.expand('foo', {
message: 'Favorite letter?',
choices: ['a', 'b', 'c']
});
Create a "choices" question from an array of values.
Params
queue
{String|Array}: Name or array of question names.options
{Object|Function}: Question options or callback functioncallback
{Function}: callback function
Example
GFM_5
Get question name
, or group name
if question is not found. You can also do a direct lookup using quesions.cache['foo']
.
Params
name
{String}returns
{Object}: Returns the question object.
Example
var name = questions.get('name');
Returns true if questions.cache
or questions.groups
has question name
.
returns
{String}: The name of the question to check
Example
var name = questions.has('name');
Delete the given question or any questions that have the given namespace using dot-notation.
returns
{String}: The name of the question to delete
Example
questions.del('name');
questions.get('name');
questions.del('author');
questions.get('author.name');
Clear all cached answers.
Example
questions.clearAnswers();
Clear all questions from the cache.
Example
questions.clearQuestions();
Clear all cached questions and answers.
Example
questions.clear();
Ask one or more questions, with the given options
and callback.
Params
queue
{String|Array}: Name or array of question names.options
{Object|Function}: Question options or callback functioncallback
{Function}: callback function
Example
questions.ask(['name', 'description'], function(err, answers) {
console.log(answers);
});
Normalize the given value to return an array of question keys.
Params
- {[type]}: key
returns
{[type]}
Create new Question
store name
, with the given options
.
Params
name
{String}: The question property name.options
{Object}: Store options
Example
var question = new Question(name, options);
Optionally define the next question to ask by setting a custom next
function on the question object.
Params
answer
{Object}questions
{Object}answers
{Object}next
{Function}returns
{Function}
Example
questions.choice('deps', 'Where?')
questions.set('install', 'Want to install?', {
type: 'confirm',
next: function(answer, questions, answers, cb) {
if (answer === true) {
questions.ask('config.other', cb);
} else {
cb(null, answers);
}
}
});
Resolve the answer for the question from the given data sources, then set the question's default
value with any stored hints or answers if not already defined.
Params
answers
{Object}data
{Object}store
{Object}hints
{Object}returns
{Object}
Example
question.answer(answers, data, store, hints);
Force the question to be asked.
Example
question.force();
Related projects
- answer-store: Store answers to user prompts, based on locale and/or current working directory. | homepage
- inquirer: A collection of common interactive command line user interfaces. | homepage
- question-cache: A wrapper around inquirer that makes it easy to create and selectively reuse questions. | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Building docs
Generate readme and API documentation with verb:
$ npm install verb && npm run docs
Or, if verb is installed globally:
$ verb
Running tests
Install dev dependencies:
$ npm install -d && npm test
Author
Jon Schlinkert
License
Copyright © 2016 Jon Schlinkert
Released under the MIT license.
This file was generated by verb, v0.9.0, on March 02, 2016.