prompt.js
3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use strict';
var _ = require('lodash');
var rx = require('rx-lite');
var util = require('util');
var runAsync = require('run-async');
var utils = require('../utils/utils');
var Base = require('./baseUI');
/**
* Base interface class other can inherits from
*/
var PromptUI = module.exports = function (prompts, opt) {
Base.call(this, opt);
this.prompts = prompts;
};
util.inherits(PromptUI, Base);
PromptUI.prototype.run = function (questions, allDone) {
// Keep global reference to the answers
this.answers = {};
this.completed = allDone;
// Make sure questions is an array.
if (_.isPlainObject(questions)) {
questions = [questions];
}
// Create an observable, unless we received one as parameter.
// Note: As this is a public interface, we cannot do an instanceof check as we won't
// be using the exact same object in memory.
var obs = _.isArray(questions) ? rx.Observable.from(questions) : questions;
this.process = obs
.concatMap(this.processQuestion.bind(this))
.publish(); // `publish` creates a hot Observable. It prevents duplicating prompts.
this.process.subscribe(
_.noop,
function (err) { throw err; },
this.onCompletion.bind(this)
);
return this.process.connect();
};
/**
* Once all prompt are over
*/
PromptUI.prototype.onCompletion = function () {
this.close();
if (_.isFunction(this.completed)) {
this.completed(this.answers);
}
};
PromptUI.prototype.processQuestion = function (question) {
return rx.Observable.defer(function () {
var obs = rx.Observable.create(function (obs) {
obs.onNext(question);
obs.onCompleted();
});
return obs
.concatMap(this.setDefaultType.bind(this))
.concatMap(this.filterIfRunnable.bind(this))
.concatMap(utils.fetchAsyncQuestionProperty.bind(null, question, 'message', this.answers))
.concatMap(utils.fetchAsyncQuestionProperty.bind(null, question, 'default', this.answers))
.concatMap(utils.fetchAsyncQuestionProperty.bind(null, question, 'choices', this.answers))
.concatMap(this.fetchAnswer.bind(this));
}.bind(this));
};
PromptUI.prototype.fetchAnswer = function (question) {
var Prompt = this.prompts[question.type];
var prompt = new Prompt(question, this.rl, this.answers);
var answers = this.answers;
return utils.createObservableFromAsync(function () {
var done = this.async();
prompt.run(function (answer) {
answers[question.name] = answer;
done({ name: question.name, answer: answer });
});
});
};
PromptUI.prototype.setDefaultType = function (question) {
// Default type to input
if (!this.prompts[question.type]) {
question.type = 'input';
}
return rx.Observable.defer(function () {
return rx.Observable.return(question);
});
};
PromptUI.prototype.filterIfRunnable = function (question) {
if (question.when == null) {
return rx.Observable.return(question);
}
var handleResult = function (obs, shouldRun) {
if (shouldRun) {
obs.onNext(question);
}
obs.onCompleted();
};
var answers = this.answers;
return rx.Observable.defer(function () {
return rx.Observable.create(function (obs) {
if (_.isBoolean(question.when)) {
handleResult(obs, question.when);
return;
}
runAsync(question.when, function (shouldRun) {
handleResult(obs, shouldRun);
}, answers);
});
});
};