JSPM

  • Created
  • Published
  • Downloads 32294
  • Score
    100M100P100Q146623F
  • License MIT

Ask questions, persist the answers. Basic support for i18n and storing answers based on current working directory.

Package Exports

  • question-store

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-store) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

question-store NPM version

Ask questions, persist the answers. Basic support for i18n and storing answers based on current working directory.

Similar to question-cache, but persists answers to disk and supports locales and storing answers based on current working directory.

Install

Install with npm

$ npm i question-store --save

Usage

var Questions = require('question-store');

API

Questions

Questions

Create an instance of Questions with the given options.

Params

  • options {Object}: question store options

Example

var Questions = new Questions(options);

.set

Cache a question to be asked at a later point. Creates an instance of Question, so any Question options or settings may be used.

Params

  • value {Object|String}: Question object, message (string), or options object.
  • locale {String}: Optionally pass the locale to use, otherwise the default locale is used.

Example

questions.set('drink', 'What is your favorite beverage?');
// or
questions.set('drink', {
  type: 'input',
  message: 'What is your favorite beverage?'
});
// or
questions.set({
  name: 'drink'
  type: 'input',
  message: 'What is your favorite beverage?'
});

.setDefault

Add a question that will have its answer stored as a default value.

Params

  • name {String}
  • val {Object}
  • options {Object}

Example

questions.setDefault('author.name', 'What is your name?');

.get

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');
//=> question object

.del

Delete the answer for question name for the current (or given) locale.

Params

  • name {String}: Question name
  • locale {String}: Optionally pass a locale

Example

question.del(locale);

.getAnswer

Get the answer for question name at the current cwd.

Optionally specify a locale to get, otherwise the default locale's answer is returend.

Params

  • name {String}
  • locale {String}
  • returns {Object}: Returns the question object.

Example

var name = questions.getAnswer('name');
//=> {name: 'Jon'}

// specify a locale
var name = questions.getAnswer('name', 'fr');
//=> {name: 'Jean'}

.getDefaultAnswer

Get the default answer object for question name for the current locale. Optionally specify a locale to get the default answer for that locale.

Params

  • name {String}
  • locale {String}
  • returns {Object}: Returns the question object.

Example

var name = questions.getDefaultAnswer('name');
//=> {name: 'Jon'}

// specify a locale
var name = questions.getDefaultAnswer('name', 'fr');
//=> {name: 'Jean'}

.isAnswered

Return true if question name has been answered for the current locale and the current working directory.

Params

  • name {String}: Question name
  • locale {String}: Optionally pass a locale

Example

question.isAnswered(locale);

.setData

Set data to be used for answering questions, or as default answers when force is true.

Params

  • key {String|Object}: Property name to set, or object to extend onto questions.data
  • val {any}: The value to assign to key

Example

questions.setData('foo', 'bar');
// or
questions.setData({foo: 'bar'});

.hasData

Return true if property key has a value on questions.data.

Params

  • key {String}: The property to lookup.
  • returns {Boolean}

Example

questions.hasData('abc');

.getData

Get the value of property key from questions.data.

Params

  • key {String}: The property to get.
  • returns {any}: Returns the value of property key

Example

questions.setData('foo', 'bar');
questions.getData('foo');
//=> 'bar'

.question

Get the question instance stored for the given name. This is the entire Question object, with all answers for all locales and directories.

Params

  • name {String}
  • returns {Object}: Returns the question instance.

Example

var name = questions.question('name');

.delQuestion

Delete a question.

Params

  • name {String}: The question to delete.

Example

question.deleteQuestion(name);

.getGroup

Get a group with the given key. If key has a dot, only the substring before the dot is used for the lookup.

Params

  • key {String}
  • returns {Object}

Example

questions
  .set('author.name', 'Author name?')
  .set('author.url', 'Author url?')
  .set('project.name', 'Project name?')
  .set('project.url', 'Project url?')

var group = questions.getGroup('author');
//=> ['author.name', 'author.url']

questions.ask(group, function(err, answers) {
  // do stuff with answers
});

.deleteAll

Delete answers for all questions for the current (or given) locale.

Params

  • locale {String}: Optionally pass a locale

Example

question.deleteAll(locale);

.erase

Erase all answers for question name from the file system.

Params

  • name {String}: Question name

Example

question.erase(name);

.ask

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 function
  • callback {Function}: callback function

Example

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

.getIndex

Get a the index of question name from the queue.

Params

  • name {String}
  • returns {Object}

Example

questions.getIndex('author');
//=> 1

.unqueue

Remove a question from the queue.

Params

  • items {Object}: Object of views

Example

console.log(questions.queue);
//=> ['a', 'b', 'c'];
questions.unqueue('a');

Question

Question

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

.set

Set the answer to the question for the current (or given) locale, at the current working directory.

Params

  • locale {String}: Optionally pass a locale

Example

question.set('foo');

.get

Get the answer for the current (or given) locale for the current working directory.

Params

  • locale {String}: Optionally pass a locale

Example

question.get(locale);

.del

Delete the answer for the current (or given) locale and the current working directory.

Params

  • locale {String}: Optionally pass a locale

Example

question.del(locale);

.erase

Delete the answer store (all answers for the question) from the file system.

Example

question.erase();

.isAnswered

Return true if the question has been answered for the current locale and the current working directory.

Params

  • locale {String}: Optionally pass a locale

Example

question.isAnswered(locale);

.setDefault

Set the default answer to use for the current (or given) locale, at the current working directory.

Params

  • locale {String}: Optionally pass a locale

Example

question.setDefault('foo');

.getDefault

Get the default answer for the current (or given) locale

Params

  • locale {String}: Optionally pass a locale

Example

question.getDefault();

.hasDefault

Return true if the question has been given a default answer for the current (or given) locale, at the current working directory.

Params

  • locale {String}: Optionally pass a locale

Example

question.hasDefault('foo');

.ask

Ask the question.

  • If an answer has already been stored for the current locale and cwd it will be returned directly without asking the question.
  • If options.force is true, the answer will be asked asked even if the answer is already stored.
  • If options.save is false, the answer will not be persisted to the file system, and the question will be re-asked each time .ask() is called (which means it's also not necessary to define force when save is false).

Params

  • options {Object|Function}: Question options or callback function
  • callback {Function}: callback function

Example

question.ask({force: true}, function(err, answer) {
  console.log(answer);
});

.save

Persist the answer for the current locale and cwd to disk.

Example

question.save();
  • 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

Running tests

Install dev dependencies:

$ npm i -d && npm test

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 on December 13, 2015.