wishthis/node_modules/inquirer/lib/ui/prompt.js

148 lines
3.7 KiB
JavaScript
Raw Normal View History

2022-01-21 08:28:41 +00:00
'use strict';
2022-06-08 10:36:39 +00:00
const _ = {
isPlainObject: require('lodash/isPlainObject'),
get: require('lodash/get'),
set: require('lodash/set'),
};
const { defer, empty, from, of } = require('rxjs');
const { concatMap, filter, publish, reduce } = require('rxjs/operators');
const runAsync = require('run-async');
const utils = require('../utils/utils');
const Base = require('./baseUI');
2022-01-21 08:28:41 +00:00
/**
* Base interface class other can inherits from
*/
class PromptUI extends Base {
constructor(prompts, opt) {
super(opt);
this.prompts = prompts;
}
2022-06-08 10:36:39 +00:00
run(questions, answers) {
2022-01-21 08:28:41 +00:00
// Keep global reference to the answers
2022-06-08 10:36:39 +00:00
if (_.isPlainObject(answers)) {
this.answers = { ...answers };
} else {
this.answers = {};
}
2022-01-21 08:28:41 +00:00
// Make sure questions is an array.
if (_.isPlainObject(questions)) {
2022-06-08 10:36:39 +00:00
// It's either an object of questions or a single question
questions = Object.values(questions).every(
(v) => _.isPlainObject(v) && v.name === undefined
)
? Object.entries(questions).map(([name, question]) => ({ name, ...question }))
: [questions];
2022-01-21 08:28:41 +00:00
}
// 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.
2022-06-08 10:36:39 +00:00
const obs = Array.isArray(questions) ? from(questions) : questions;
2022-01-21 08:28:41 +00:00
this.process = obs.pipe(
concatMap(this.processQuestion.bind(this)),
publish() // Creates a hot Observable. It prevents duplicating prompts.
);
this.process.connect();
return this.process
.pipe(
reduce((answers, answer) => {
2022-06-08 10:36:39 +00:00
_.set(answers, answer.name, answer.answer);
return answers;
}, this.answers)
2022-01-21 08:28:41 +00:00
)
.toPromise(Promise)
2022-06-08 10:36:39 +00:00
.then(this.onCompletion.bind(this), this.onError.bind(this));
2022-01-21 08:28:41 +00:00
}
/**
* Once all prompt are over
*/
onCompletion() {
this.close();
return this.answers;
}
2022-06-08 10:36:39 +00:00
onError(error) {
this.close();
return Promise.reject(error);
}
2022-01-21 08:28:41 +00:00
processQuestion(question) {
2022-06-08 10:36:39 +00:00
question = { ...question };
2022-01-21 08:28:41 +00:00
return defer(() => {
2022-06-08 10:36:39 +00:00
const obs = of(question);
2022-01-21 08:28:41 +00:00
return obs.pipe(
concatMap(this.setDefaultType.bind(this)),
concatMap(this.filterIfRunnable.bind(this)),
concatMap(() =>
utils.fetchAsyncQuestionProperty(question, 'message', this.answers)
),
concatMap(() =>
utils.fetchAsyncQuestionProperty(question, 'default', this.answers)
),
concatMap(() =>
utils.fetchAsyncQuestionProperty(question, 'choices', this.answers)
),
concatMap(this.fetchAnswer.bind(this))
);
});
}
fetchAnswer(question) {
2022-06-08 10:36:39 +00:00
const Prompt = this.prompts[question.type];
2022-01-21 08:28:41 +00:00
this.activePrompt = new Prompt(question, this.rl, this.answers);
return defer(() =>
2022-06-08 10:36:39 +00:00
from(this.activePrompt.run().then((answer) => ({ name: question.name, answer })))
2022-01-21 08:28:41 +00:00
);
}
setDefaultType(question) {
// Default type to input
if (!this.prompts[question.type]) {
question.type = 'input';
}
return defer(() => of(question));
}
filterIfRunnable(question) {
2022-06-08 10:36:39 +00:00
if (
question.askAnswered !== true &&
_.get(this.answers, question.name) !== undefined
) {
return empty();
}
2022-01-21 08:28:41 +00:00
if (question.when === false) {
return empty();
}
2022-06-08 10:36:39 +00:00
if (typeof question.when !== 'function') {
2022-01-21 08:28:41 +00:00
return of(question);
}
2022-06-08 10:36:39 +00:00
const { answers } = this;
2022-01-21 08:28:41 +00:00
return defer(() =>
from(
2022-06-08 10:36:39 +00:00
runAsync(question.when)(answers).then((shouldRun) => {
2022-01-21 08:28:41 +00:00
if (shouldRun) {
return question;
}
})
2022-06-08 10:36:39 +00:00
).pipe(filter((val) => val != null))
2022-01-21 08:28:41 +00:00
);
}
}
module.exports = PromptUI;