1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | var util = require('util');
|
11 | var debug = require('debug')('questions:question');
|
12 | var utils = require('./utils');
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function Question(name, message, options) {
|
26 | if (utils.isObject(name)) {
|
27 | options = utils.merge({}, message, name);
|
28 | message = options.message;
|
29 | name = options.name;
|
30 | }
|
31 |
|
32 | if (utils.isObject(message)) {
|
33 | options = utils.merge({}, options, message);
|
34 | message = options.message;
|
35 | }
|
36 |
|
37 | utils.define(this, 'isQuestion', true);
|
38 | utils.define(this, 'cache', {});
|
39 |
|
40 | this.options = options || {};
|
41 | this.type = this.options.type || 'input';
|
42 | this.message = message || this.options.message;
|
43 | this.name = name || this.options.name;
|
44 |
|
45 | if (!this.message) {
|
46 | this.message = this.name;
|
47 | }
|
48 |
|
49 | utils.merge(this, this.options);
|
50 | createNext(this);
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function createNext(question) {
|
61 | if (!question.options.next) return;
|
62 |
|
63 | if (typeof question.options.next === 'function') {
|
64 | question.next = function() {
|
65 | question.options.next.apply(question, arguments);
|
66 | };
|
67 | return;
|
68 | }
|
69 |
|
70 | if (typeof question.options.next === 'string') {
|
71 | question.type = 'confirm';
|
72 | question.next = function(answer, questions, answers, next) {
|
73 | if (answer === true) {
|
74 | questions.ask(question.options.next, next);
|
75 | } else {
|
76 | next(null, answers);
|
77 | }
|
78 | };
|
79 | }
|
80 | }
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | Question.prototype.next = function(answer, questions, answers, next) {
|
109 | next(null, answers);
|
110 | };
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | Question.prototype.opts = function(options) {
|
121 | var args = [].slice.call(arguments);
|
122 | options = [].concat.apply([], args);
|
123 |
|
124 | var opts = utils.omitEmpty(utils.merge.apply(utils.merge, [{}].concat(options)));
|
125 | utils.decorate(opts);
|
126 |
|
127 | if (typeof opts.default !== 'undefined') {
|
128 | this.default = opts.default;
|
129 | }
|
130 | if (opts.persist === false) {
|
131 | opts.global = false;
|
132 | opts.hint = false;
|
133 | opts.save = false;
|
134 | }
|
135 | if (opts.save === false) {
|
136 | opts.force = true;
|
137 | }
|
138 | return opts;
|
139 | };
|
140 |
|
141 | Question.prototype.setAnswer = function(val) {
|
142 | this.answer = val;
|
143 | return this;
|
144 | };
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | Question.prototype.getAnswer = function(answers, data) {
|
163 | return utils.get(answers, this.name) || utils.get(data, this.name);
|
164 | };
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | Question.prototype.force = function() {
|
177 | this.options.force = true;
|
178 | return this;
|
179 | };
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | module.exports = Question;
|