wishthis/node_modules/inquirer/lib/prompts/base.js

181 lines
4.5 KiB
JavaScript
Raw Normal View History

2022-01-21 08:28:41 +00:00
'use strict';
/**
* Base prompt implementation
* Should be extended by prompt types.
*/
2022-04-07 07:06:43 +00:00
const _ = {
defaults: require('lodash/defaults'),
clone: require('lodash/clone'),
};
const chalk = require('chalk');
const runAsync = require('run-async');
const { filter, flatMap, share, take, takeUntil } = require('rxjs/operators');
const Choices = require('../objects/choices');
const ScreenManager = require('../utils/screen-manager');
2022-01-21 08:28:41 +00:00
class Prompt {
constructor(question, rl, answers) {
// Setup instance defaults property
2022-04-07 07:06:43 +00:00
Object.assign(this, {
answers,
status: 'pending',
2022-01-21 08:28:41 +00:00
});
// Set defaults prompt options
this.opt = _.defaults(_.clone(question), {
validate: () => true,
2022-04-07 07:06:43 +00:00
validatingText: '',
filter: (val) => val,
filteringText: '',
2022-01-21 08:28:41 +00:00
when: () => true,
suffix: '',
2022-04-07 07:06:43 +00:00
prefix: chalk.green('?'),
2022-01-21 08:28:41 +00:00
});
// Make sure name is present
if (!this.opt.name) {
this.throwParamError('name');
}
// Set default message if no message defined
if (!this.opt.message) {
this.opt.message = this.opt.name + ':';
}
// Normalize choices
if (Array.isArray(this.opt.choices)) {
this.opt.choices = new Choices(this.opt.choices, answers);
}
this.rl = rl;
this.screen = new ScreenManager(this.rl);
}
/**
* Start the Inquiry session and manage output value filtering
* @return {Promise}
*/
run() {
2022-04-07 07:06:43 +00:00
return new Promise((resolve, reject) => {
this._run(
(value) => resolve(value),
(error) => reject(error)
);
2022-01-21 08:28:41 +00:00
});
}
// Default noop (this one should be overwritten in prompts)
_run(cb) {
cb();
}
/**
* Throw an error telling a required parameter is missing
* @param {String} name Name of the missing param
* @return {Throw Error}
*/
throwParamError(name) {
throw new Error('You must provide a `' + name + '` parameter');
}
/**
* Called when the UI closes. Override to do any specific cleanup necessary
*/
close() {
this.screen.releaseCursor();
}
/**
* Run the provided validation method each time a submit event occur.
* @param {Rx.Observable} submit - submit event flow
* @return {Object} Object containing two observables: `success` and `error`
*/
handleSubmitEvents(submit) {
2022-04-07 07:06:43 +00:00
const self = this;
const validate = runAsync(this.opt.validate);
const asyncFilter = runAsync(this.opt.filter);
const validation = submit.pipe(
flatMap((value) => {
this.startSpinner(value, this.opt.filteringText);
return asyncFilter(value, self.answers).then(
(filteredValue) => {
this.startSpinner(filteredValue, this.opt.validatingText);
return validate(filteredValue, self.answers).then(
(isValid) => ({ isValid, value: filteredValue }),
(err) => ({ isValid: err, value: filteredValue })
);
},
(err) => ({ isValid: err })
);
}),
2022-01-21 08:28:41 +00:00
share()
);
2022-04-07 07:06:43 +00:00
const success = validation.pipe(
filter((state) => state.isValid === true),
2022-01-21 08:28:41 +00:00
take(1)
);
2022-04-07 07:06:43 +00:00
const error = validation.pipe(
filter((state) => state.isValid !== true),
2022-01-21 08:28:41 +00:00
takeUntil(success)
);
return {
2022-04-07 07:06:43 +00:00
success,
error,
2022-01-21 08:28:41 +00:00
};
}
2022-04-07 07:06:43 +00:00
startSpinner(value, bottomContent) {
value = this.getSpinningValue(value);
// If the question will spin, cut off the prefix (for layout purposes)
const content = bottomContent
? this.getQuestion() + value
: this.getQuestion().slice(this.opt.prefix.length + 1) + value;
this.screen.renderWithSpinner(content, bottomContent);
}
/**
* Allow override, e.g. for password prompts
* See: https://github.com/SBoudrias/Inquirer.js/issues/1022
*
* @return {String} value to display while spinning
*/
getSpinningValue(value) {
return value;
}
2022-01-21 08:28:41 +00:00
/**
* Generate the prompt question string
* @return {String} prompt question string
*/
getQuestion() {
2022-04-07 07:06:43 +00:00
let message =
(this.opt.prefix ? this.opt.prefix + ' ' : '') +
2022-01-21 08:28:41 +00:00
chalk.bold(this.opt.message) +
this.opt.suffix +
chalk.reset(' ');
2022-04-07 07:06:43 +00:00
// Append the default if available, and if question isn't touched/answered
if (
this.opt.default != null &&
this.status !== 'touched' &&
this.status !== 'answered'
) {
2022-01-21 08:28:41 +00:00
// If default password is supplied, hide it
if (this.opt.type === 'password') {
message += chalk.italic.dim('[hidden] ');
} else {
message += chalk.dim('(' + this.opt.default + ') ');
}
}
return message;
}
}
module.exports = Prompt;