3 * Base prompt implementation
4 * Should be extended by prompt types.
7 assign: require('lodash/assign'),
8 defaults: require('lodash/defaults'),
9 clone: require('lodash/clone'),
11 var chalk = require('chalk');
12 var runAsync = require('run-async');
13 var { filter, flatMap, share, take, takeUntil } = require('rxjs/operators');
14 var Choices = require('../objects/choices');
15 var ScreenManager = require('../utils/screen-manager');
18 constructor(question, rl, answers) {
19 // Setup instance defaults property
25 // Set defaults prompt options
26 this.opt = _.defaults(_.clone(question), {
31 prefix: chalk.green('?'),
34 // Make sure name is present
36 this.throwParamError('name');
39 // Set default message if no message defined
40 if (!this.opt.message) {
41 this.opt.message = this.opt.name + ':';
45 if (Array.isArray(this.opt.choices)) {
46 this.opt.choices = new Choices(this.opt.choices, answers);
50 this.screen = new ScreenManager(this.rl);
54 * Start the Inquiry session and manage output value filtering
59 return new Promise((resolve, reject) => {
61 (value) => resolve(value),
62 (error) => reject(error)
67 // Default noop (this one should be overwritten in prompts)
73 * Throw an error telling a required parameter is missing
74 * @param {String} name Name of the missing param
75 * @return {Throw Error}
78 throwParamError(name) {
79 throw new Error('You must provide a `' + name + '` parameter');
83 * Called when the UI closes. Override to do any specific cleanup necessary
86 this.screen.releaseCursor();
90 * Run the provided validation method each time a submit event occur.
91 * @param {Rx.Observable} submit - submit event flow
92 * @return {Object} Object containing two observables: `success` and `error`
94 handleSubmitEvents(submit) {
96 var validate = runAsync(this.opt.validate);
97 var asyncFilter = runAsync(this.opt.filter);
98 var validation = submit.pipe(
100 asyncFilter(value, self.answers).then(
102 validate(filteredValue, self.answers).then(
103 (isValid) => ({ isValid: isValid, value: filteredValue }),
104 (err) => ({ isValid: err, value: filteredValue })
106 (err) => ({ isValid: err })
112 var success = validation.pipe(
113 filter((state) => state.isValid === true),
116 var error = validation.pipe(
117 filter((state) => state.isValid !== true),
128 * Generate the prompt question string
129 * @return {String} prompt question string
136 chalk.bold(this.opt.message) +
140 // Append the default if available, and if question isn't answered
141 if (this.opt.default != null && this.status !== 'answered') {
142 // If default password is supplied, hide it
143 if (this.opt.type === 'password') {
144 message += chalk.italic.dim('[hidden] ');
146 message += chalk.dim('(' + this.opt.default + ') ');
154 module.exports = Prompt;