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

128 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-01-21 08:28:41 +00:00
'use strict';
/**
* `password` type prompt
*/
2022-04-07 07:06:43 +00:00
const chalk = require('chalk');
const { map, takeUntil } = require('rxjs/operators');
const Base = require('./base');
const observe = require('../utils/events');
2022-01-21 08:28:41 +00:00
function mask(input, maskChar) {
input = String(input);
maskChar = typeof maskChar === 'string' ? maskChar : '*';
if (input.length === 0) {
return '';
}
return new Array(input.length + 1).join(maskChar);
}
class PasswordPrompt extends Base {
/**
* Start the Inquiry session
* @param {Function} cb Callback when prompt is done
* @return {this}
*/
_run(cb) {
this.done = cb;
2022-04-07 07:06:43 +00:00
const events = observe(this.rl);
2022-01-21 08:28:41 +00:00
// Once user confirm (enter key)
2022-04-07 07:06:43 +00:00
const submit = events.line.pipe(map(this.filterInput.bind(this)));
2022-01-21 08:28:41 +00:00
2022-04-07 07:06:43 +00:00
const validation = this.handleSubmitEvents(submit);
2022-01-21 08:28:41 +00:00
validation.success.forEach(this.onEnd.bind(this));
validation.error.forEach(this.onError.bind(this));
events.keypress
.pipe(takeUntil(validation.success))
.forEach(this.onKeypress.bind(this));
// Init
this.render();
return this;
}
/**
* Render the prompt to screen
* @return {PasswordPrompt} self
*/
render(error) {
2022-04-07 07:06:43 +00:00
let message = this.getQuestion();
let bottomContent = '';
2022-01-21 08:28:41 +00:00
if (this.status === 'answered') {
2022-04-07 07:06:43 +00:00
message += this.getMaskedValue(this.answer);
2022-01-21 08:28:41 +00:00
} else {
2022-04-07 07:06:43 +00:00
message += this.getMaskedValue(this.rl.line || '');
2022-01-21 08:28:41 +00:00
}
if (error) {
bottomContent = '\n' + chalk.red('>> ') + error;
}
this.screen.render(message, bottomContent);
}
2022-04-07 07:06:43 +00:00
getMaskedValue(value) {
if (this.status === 'answered') {
return this.opt.mask
? chalk.cyan(mask(value, this.opt.mask))
: chalk.italic.dim('[hidden]');
}
return this.opt.mask
? mask(value, this.opt.mask)
: chalk.italic.dim('[input is hidden] ');
}
/**
* Mask value during async filter/validation.
*/
getSpinningValue(value) {
return this.getMaskedValue(value);
}
2022-01-21 08:28:41 +00:00
/**
* When user press `enter` key
*/
filterInput(input) {
if (!input) {
return this.opt.default == null ? '' : this.opt.default;
}
return input;
}
onEnd(state) {
this.status = 'answered';
this.answer = state.value;
// Re-render prompt
this.render();
this.screen.done();
this.done(state.value);
}
onError(state) {
this.render(state.isValid);
}
onKeypress() {
// If user press a key, just clear the default value
if (this.opt.default) {
this.opt.default = undefined;
}
this.render();
}
}
module.exports = PasswordPrompt;