source: trip-planner-front/node_modules/inquirer/lib/prompts/password.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.3 KB
Line 
1'use strict';
2/**
3 * `password` type prompt
4 */
5
6const chalk = require('chalk');
7const { map, takeUntil } = require('rxjs/operators');
8const Base = require('./base');
9const observe = require('../utils/events');
10
11function mask(input, maskChar) {
12 input = String(input);
13 maskChar = typeof maskChar === 'string' ? maskChar : '*';
14 if (input.length === 0) {
15 return '';
16 }
17
18 return new Array(input.length + 1).join(maskChar);
19}
20
21class PasswordPrompt extends Base {
22 /**
23 * Start the Inquiry session
24 * @param {Function} cb Callback when prompt is done
25 * @return {this}
26 */
27
28 _run(cb) {
29 this.done = cb;
30
31 const events = observe(this.rl);
32
33 // Once user confirm (enter key)
34 const submit = events.line.pipe(map(this.filterInput.bind(this)));
35
36 const validation = this.handleSubmitEvents(submit);
37 validation.success.forEach(this.onEnd.bind(this));
38 validation.error.forEach(this.onError.bind(this));
39
40 events.keypress
41 .pipe(takeUntil(validation.success))
42 .forEach(this.onKeypress.bind(this));
43
44 // Init
45 this.render();
46
47 return this;
48 }
49
50 /**
51 * Render the prompt to screen
52 * @return {PasswordPrompt} self
53 */
54
55 render(error) {
56 let message = this.getQuestion();
57 let bottomContent = '';
58
59 if (this.status === 'answered') {
60 message += this.opt.mask
61 ? chalk.cyan(mask(this.answer, this.opt.mask))
62 : chalk.italic.dim('[hidden]');
63 } else if (this.opt.mask) {
64 message += mask(this.rl.line || '', this.opt.mask);
65 } else {
66 message += chalk.italic.dim('[input is hidden] ');
67 }
68
69 if (error) {
70 bottomContent = '\n' + chalk.red('>> ') + error;
71 }
72
73 this.screen.render(message, bottomContent);
74 }
75
76 /**
77 * When user press `enter` key
78 */
79
80 filterInput(input) {
81 if (!input) {
82 return this.opt.default == null ? '' : this.opt.default;
83 }
84
85 return input;
86 }
87
88 onEnd(state) {
89 this.status = 'answered';
90 this.answer = state.value;
91
92 // Re-render prompt
93 this.render();
94
95 this.screen.done();
96 this.done(state.value);
97 }
98
99 onError(state) {
100 this.render(state.isValid);
101 }
102
103 onKeypress() {
104 // If user press a key, just clear the default value
105 if (this.opt.default) {
106 this.opt.default = undefined;
107 }
108
109 this.render();
110 }
111}
112
113module.exports = PasswordPrompt;
Note: See TracBrowser for help on using the repository browser.