password.js 2.6 KB

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