1 | # question-cache [![NPM version](https://badge.fury.io/js/question-cache.svg)](http://badge.fury.io/js/question-cache)
|
2 |
|
3 | > A tiny wrapper around inquirer that makes it easy to create and selectively reuse questions. Useful for project generators like yeoman or generate.
|
4 |
|
5 | ## Install
|
6 |
|
7 | Install with [npm](https://www.npmjs.com/)
|
8 |
|
9 | ```sh
|
10 | $ npm i question-cache --save
|
11 | ```
|
12 |
|
13 | ## Usage
|
14 |
|
15 | ```js
|
16 | var Questions = require('question-cache');
|
17 |
|
18 | // you must pass your own instance of inquirer!
|
19 | var questions = new Questions({
|
20 | inquirer: require('inquirer')
|
21 | });
|
22 | ```
|
23 |
|
24 | ## API
|
25 |
|
26 | ### [Questions](index.js#L23)
|
27 |
|
28 | Create an instance of `Questions` with the given `options`.
|
29 |
|
30 | **Params**
|
31 |
|
32 | * `options` **{Object}**: Pass your instance of [inquirer](undefined)on the `inquirer` option.
|
33 |
|
34 | **Example**
|
35 |
|
36 | ```js
|
37 | var inquirer = require('inquirer')
|
38 | var questions = new Questions({inquirer: inquirer});
|
39 | ```
|
40 |
|
41 | ### [.set](index.js#L47)
|
42 |
|
43 | Store a question object by `key`.
|
44 |
|
45 | **Params**
|
46 |
|
47 | * `key` **{String}**: Unique question id.
|
48 | * `value` **{Object}**: Question object that follows [inquirer](undefined)conventions.
|
49 |
|
50 | **Example**
|
51 |
|
52 | ```js
|
53 | questions.set('name', {
|
54 | type: 'input',
|
55 | message: 'Project name?',
|
56 | default: 'undefined'
|
57 | });
|
58 | ```
|
59 |
|
60 | ### [.get](index.js#L67)
|
61 |
|
62 | Get a question by `key`.
|
63 |
|
64 | **Params**
|
65 |
|
66 | * `key` **{String}**: Unique question id.
|
67 | * `value` **{Object}**: Question object that follows [inquirer](undefined)conventions.
|
68 |
|
69 | **Example**
|
70 |
|
71 | ```js
|
72 | questions.get('name');
|
73 | //=> {type: 'input', message: 'What is your name?', default: ''}
|
74 | ```
|
75 |
|
76 | ### [.ask](index.js#L84)
|
77 |
|
78 | Ask a question or array of questions.
|
79 |
|
80 | **Params**
|
81 |
|
82 | * `key` **{String}**: Unique question id.
|
83 | * `value` **{Object}**: Question object that follows [inquirer](undefined)conventions.
|
84 |
|
85 | **Example**
|
86 |
|
87 | ```js
|
88 | questions.ask(['name', 'homepage']);
|
89 | //=> { name: 'foo', homepage: 'https://github/foo' }
|
90 | ```
|
91 |
|
92 | ### [.prompt](index.js#L125)
|
93 |
|
94 | Exposes the `prompt` method on [inquirer](undefined)as a convenience.
|
95 |
|
96 | **Params**
|
97 |
|
98 | * `question` **{Object|Array}**: Question object or array of question objects.
|
99 | * `callback` **{Object}**: Callback function.
|
100 |
|
101 | **Example**
|
102 |
|
103 | ```js
|
104 | questions.prompt({
|
105 | type: 'list',
|
106 | name: 'chocolate',
|
107 | message: 'What\'s your favorite chocolate?',
|
108 | choices: ['Mars', 'Oh Henry', 'Hershey']
|
109 | }, function(answers) {
|
110 | //=> {chocolate: 'Hershey'}
|
111 | });
|
112 | ```
|
113 |
|
114 | ## Example
|
115 |
|
116 | ```js
|
117 | var Questions = require('question-cache');
|
118 | var inquirer = require('inquirer');
|
119 | var questions = new Questions({inquirer: inquirer});
|
120 |
|
121 | questions
|
122 | .set('first', {
|
123 | type: 'input',
|
124 | message: 'What is your first name?',
|
125 | default: ''
|
126 | })
|
127 | .set('last', {
|
128 | type: 'input',
|
129 | message: 'What is your last name?',
|
130 | default: ''
|
131 | })
|
132 | .ask(function (err, answers) {
|
133 | console.log(answers);
|
134 | //=> { first: 'Jon', last: 'Schlinkert' }
|
135 | });
|
136 |
|
137 | /**
|
138 | * Nested questions
|
139 | */
|
140 |
|
141 | questions.prompt({
|
142 | type: 'list',
|
143 | name: 'chocolate',
|
144 | message: 'What\'s your favorite chocolate?',
|
145 | choices: ['Mars', 'Oh Henry', 'Hershey']
|
146 | }, function (answers) {
|
147 | questions.prompt({
|
148 | type: 'list',
|
149 | name: 'beverage',
|
150 | message: 'And your favorite beverage?',
|
151 | choices: ['Pepsi', 'Coke', '7up', 'Mountain Dew', 'Red Bull']
|
152 | }, function (answers) {
|
153 | console.log(answers);
|
154 | });
|
155 | });
|
156 | ```
|
157 |
|
158 | **Screen capture**
|
159 |
|
160 | <img width="669" alt="screen shot 2015-07-13 at 8 05 20 am" src="https://cloud.githubusercontent.com/assets/383994/8649221/00ecc908-2936-11e5-88d2-b1ab75a53ba0.png">
|
161 |
|
162 | ## Other awesome projects
|
163 |
|
164 | * [generate](https://github.com/generate/generate): Project generator, for node.js.
|
165 | * [option-cache](https://github.com/jonschlinkert/option-cache): Simple API for managing options in JavaScript applications.
|
166 | * [verb](https://github.com/assemble/verb): Documentation generator for GitHub projects. Extremely powerful, easy to use, can generate anything from API… [more](https://github.com/assemble/verb)
|
167 |
|
168 | ## Contributing
|
169 |
|
170 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/question-cache/issues/new)
|
171 |
|
172 | ## Author
|
173 |
|
174 | **Jon Schlinkert**
|
175 |
|
176 | + [github/jonschlinkert](https://github.com/jonschlinkert)
|
177 | + [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
|
178 |
|
179 | ## License
|
180 |
|
181 | Copyright © 2015 Jon Schlinkert
|
182 | Released under the MIT license.
|
183 |
|
184 | ***
|
185 |
|
186 | _This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on July 13, 2015._ |
\ | No newline at end of file |