Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports = function (rl) {
var keypress = rx.Observable.fromEvent(rl.input, 'keypress', normalizeKeypressEvents)
.filter(function (e) {
// Ignore `enter` key. On the readline, we only care about the `line` event.
return e.key.name !== 'enter' && e.key.name !== 'return';
});
return {
line: rx.Observable.fromEvent(rl, 'line'),
keypress: keypress,
normalizedUpKey: keypress.filter(function (e) {
return e.key.name === 'up' || e.key.name === 'k' || (e.key.name === 'p' && e.key.ctrl);
}).share(),
normalizedDownKey: keypress.filter(function (e) {
return e.key.name === 'down' || e.key.name === 'j' || (e.key.name === 'n' && e.key.ctrl);
}).share(),
numberKey: keypress.filter(function (e) {
return e.value && '123456789'.indexOf(e.value) >= 0;
}).map(function (e) {
return Number(e.value);
}).share(),
PromptUI.prototype.run = function (questions) {
// Keep global reference to the answers
this.answers = {};
// 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` creates a hot Observable. It prevents duplicating prompts.
.publish();
this.process.connect();
return this.process
.reduce(function (answers, answer) {
_.set(this.answers, answer.name, answer.answer);
return this.answers;
}.bind(this), {})
.toPromise(Promise)
.then(this.onCompletion.bind(this));
};
module.exports = function (rl) {
var keypress = rx.Observable.fromEvent(rl.input, 'keypress', normalizeKeypressEvents)
.filter(function (e) {
// Ignore `enter` key. On the readline, we only care about the `line` event.
return e.key.name !== 'enter' && e.key.name !== 'return';
});
return {
line: rx.Observable.fromEvent(rl, 'line'),
keypress: keypress,
normalizedUpKey: keypress.filter(function (e) {
return e.key.name === 'up' || e.key.name === 'k' || (e.key.name === 'p' && e.key.ctrl);
}).share(),
normalizedDownKey: keypress.filter(function (e) {
return e.key.name === 'down' || e.key.name === 'j' || (e.key.name === 'n' && e.key.ctrl);
}).share(),
return rx.Observable.defer(function () {
return rx.Observable.return(question);
});
};
return rx.Observable.defer(function () {
var obs = rx.Observable.of(question);
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.filterIfRunnable = function (question) {
if (question.when === false) {
return rx.Observable.empty();
}
if (!_.isFunction(question.when)) {
return rx.Observable.return(question);
}
var answers = this.answers;
return rx.Observable.defer(function () {
return rx.Observable.fromPromise(
runAsync(question.when)(answers).then(function (shouldRun) {
if (shouldRun) {
return question;
}
})
).filter(function (val) {
return val != null;
exports.fetchAsyncQuestionProperty = function (question, prop, answers) {
if (!_.isFunction(question[prop])) {
return rx.Observable.return(question);
}
return rx.Observable.fromPromise(runAsync(question[prop])(answers)
.then(function (value) {
question[prop] = value;
return question;
})
);
};