Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. 'use strict';
  2. /**
  3. * Base prompt implementation
  4. * Should be extended by prompt types.
  5. */
  6. var _ = require('lodash');
  7. var chalk = require('chalk');
  8. var runAsync = require('run-async');
  9. var { filter, flatMap, share, take, takeUntil } = require('rxjs/operators');
  10. var Choices = require('../objects/choices');
  11. var ScreenManager = require('../utils/screen-manager');
  12. class Prompt {
  13. constructor(question, rl, answers) {
  14. // Setup instance defaults property
  15. _.assign(this, {
  16. answers: answers,
  17. status: 'pending'
  18. });
  19. // Set defaults prompt options
  20. this.opt = _.defaults(_.clone(question), {
  21. validate: () => true,
  22. filter: val => val,
  23. when: () => true,
  24. suffix: '',
  25. prefix: chalk.green('?')
  26. });
  27. // Make sure name is present
  28. if (!this.opt.name) {
  29. this.throwParamError('name');
  30. }
  31. // Set default message if no message defined
  32. if (!this.opt.message) {
  33. this.opt.message = this.opt.name + ':';
  34. }
  35. // Normalize choices
  36. if (Array.isArray(this.opt.choices)) {
  37. this.opt.choices = new Choices(this.opt.choices, answers);
  38. }
  39. this.rl = rl;
  40. this.screen = new ScreenManager(this.rl);
  41. }
  42. /**
  43. * Start the Inquiry session and manage output value filtering
  44. * @return {Promise}
  45. */
  46. run() {
  47. return new Promise(resolve => {
  48. this._run(value => resolve(value));
  49. });
  50. }
  51. // Default noop (this one should be overwritten in prompts)
  52. _run(cb) {
  53. cb();
  54. }
  55. /**
  56. * Throw an error telling a required parameter is missing
  57. * @param {String} name Name of the missing param
  58. * @return {Throw Error}
  59. */
  60. throwParamError(name) {
  61. throw new Error('You must provide a `' + name + '` parameter');
  62. }
  63. /**
  64. * Called when the UI closes. Override to do any specific cleanup necessary
  65. */
  66. close() {
  67. this.screen.releaseCursor();
  68. }
  69. /**
  70. * Run the provided validation method each time a submit event occur.
  71. * @param {Rx.Observable} submit - submit event flow
  72. * @return {Object} Object containing two observables: `success` and `error`
  73. */
  74. handleSubmitEvents(submit) {
  75. var self = this;
  76. var validate = runAsync(this.opt.validate);
  77. var asyncFilter = runAsync(this.opt.filter);
  78. var validation = submit.pipe(
  79. flatMap(value =>
  80. asyncFilter(value, self.answers).then(
  81. filteredValue =>
  82. validate(filteredValue, self.answers).then(
  83. isValid => ({ isValid: isValid, value: filteredValue }),
  84. err => ({ isValid: err })
  85. ),
  86. err => ({ isValid: err })
  87. )
  88. ),
  89. share()
  90. );
  91. var success = validation.pipe(
  92. filter(state => state.isValid === true),
  93. take(1)
  94. );
  95. var error = validation.pipe(
  96. filter(state => state.isValid !== true),
  97. takeUntil(success)
  98. );
  99. return {
  100. success: success,
  101. error: error
  102. };
  103. }
  104. /**
  105. * Generate the prompt question string
  106. * @return {String} prompt question string
  107. */
  108. getQuestion() {
  109. var message =
  110. this.opt.prefix +
  111. ' ' +
  112. chalk.bold(this.opt.message) +
  113. this.opt.suffix +
  114. chalk.reset(' ');
  115. // Append the default if available, and if question isn't answered
  116. if (this.opt.default != null && this.status !== 'answered') {
  117. // If default password is supplied, hide it
  118. if (this.opt.type === 'password') {
  119. message += chalk.italic.dim('[hidden] ');
  120. } else {
  121. message += chalk.dim('(' + this.opt.default + ') ');
  122. }
  123. }
  124. return message;
  125. }
  126. }
  127. module.exports = Prompt;