1 | /*!
|
2 | * question-cache <https://github.com/jonschlinkert/question-cache>
|
3 | *
|
4 | * Copyright (c) 2015, Jon Schlinkert.
|
5 | * Licensed under the MIT License.
|
6 | */
|
7 |
|
8 | ;
|
9 |
|
10 | /**
|
11 | * Create an instance of `Questions` with the
|
12 | * given `options`.
|
13 | *
|
14 | * ```js
|
15 | * var inquirer = require('inquirer')
|
16 | * var questions = new Questions({inquirer: inquirer});
|
17 | * ```
|
18 | *
|
19 | * @param {Object} `options` Pass your instance of [inquirer] on the `inquirer` option.
|
20 | * @api public
|
21 | */
|
22 |
|
23 | function Questions(options) {
|
24 | this.options = options || {};
|
25 | this.inquirer = this.options.inquirer;
|
26 | this.answers = {};
|
27 | this.cache = {};
|
28 | this.queue = [];
|
29 | }
|
30 |
|
31 | /**
|
32 | * Store a question object by `key`.
|
33 | *
|
34 | * ```js
|
35 | * questions.set('name', {
|
36 | * type: 'input',
|
37 | * message: 'Project name?',
|
38 | * default: 'undefined'
|
39 | * });
|
40 | * ```
|
41 | *
|
42 | * @param {String} `key` Unique question id.
|
43 | * @param {Object} `value` Question object that follows [inquirer] conventions.
|
44 | * @api public
|
45 | */
|
46 |
|
47 | Questions.prototype.set = function(key, value) {
|
48 | value.name = value.name || key;
|
49 | this.cache[key] = value;
|
50 | this.queue.push(value);
|
51 | return this;
|
52 | };
|
53 |
|
54 | /**
|
55 | * Get a question by `key`.
|
56 | *
|
57 | * ```js
|
58 | * questions.get('name');
|
59 | * //=> {type: 'input', message: 'What is your name?', default: ''}
|
60 | * ```
|
61 | *
|
62 | * @param {String} `key` Unique question id.
|
63 | * @param {Object} `value` Question object that follows [inquirer] conventions.
|
64 | * @api public
|
65 | */
|
66 |
|
67 | Questions.prototype.get = function(key) {
|
68 | return this.cache[key];
|
69 | };
|
70 |
|
71 | /**
|
72 | * Ask a question or array of questions.
|
73 | *
|
74 | * ```js
|
75 | * questions.ask(['name', 'homepage']);
|
76 | * //=> { name: 'foo', homepage: 'https://github/foo' }
|
77 | * ```
|
78 | *
|
79 | * @param {String} `key` Unique question id.
|
80 | * @param {Object} `value` Question object that follows [inquirer] conventions.
|
81 | * @api public
|
82 | */
|
83 |
|
84 | Questions.prototype.ask = function(keys, cb) {
|
85 | var questions = [];
|
86 | if (typeof keys === 'function') {
|
87 | cb = keys;
|
88 | questions = this.queue;
|
89 | } else {
|
90 | keys = Array.isArray(keys) ? keys : [keys];
|
91 | var len = keys.length, i = -1;
|
92 | while (++i < len) {
|
93 | questions.push(this.get[keys[i]]);
|
94 | }
|
95 | }
|
96 |
|
97 | try {
|
98 | this.prompt(questions, function(answers) {
|
99 | cb(null, answers);
|
100 | });
|
101 | } catch(err) {
|
102 | cb(err);
|
103 | }
|
104 | };
|
105 |
|
106 | /**
|
107 | * Exposes the `prompt` method on [inquirer] as a convenience.
|
108 | *
|
109 | * ```js
|
110 | * questions.prompt({
|
111 | * type: 'list',
|
112 | * name: 'chocolate',
|
113 | * message: 'What\'s your favorite chocolate?',
|
114 | * choices: ['Mars', 'Oh Henry', 'Hershey']
|
115 | * }, function(answers) {
|
116 | * //=> {chocolate: 'Hershey'}
|
117 | * });
|
118 | * ```
|
119 | *
|
120 | * @param {Object|Array} `question` Question object or array of question objects.
|
121 | * @param {Object} `callback` Callback function.
|
122 | * @api public
|
123 | */
|
124 |
|
125 | Questions.prototype.prompt = function() {
|
126 | return this.inquirer.prompt.apply(this.inquirer, arguments);
|
127 | };
|
128 |
|
129 | /**
|
130 | * Expose `Questions`
|
131 | */
|
132 |
|
133 | module.exports = Questions;
|