prompt.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict';
  2. var _ = require('lodash');
  3. var Rx = require('rxjs/Rx');
  4. var runAsync = require('run-async');
  5. var utils = require('../utils/utils');
  6. var Base = require('./baseUI');
  7. /**
  8. * Base interface class other can inherits from
  9. */
  10. class PromptUI extends Base {
  11. constructor(prompts, opt) {
  12. super(opt);
  13. this.prompts = prompts;
  14. }
  15. run(questions) {
  16. // Keep global reference to the answers
  17. this.answers = {};
  18. // Make sure questions is an array.
  19. if (_.isPlainObject(questions)) {
  20. questions = [questions];
  21. }
  22. // Create an observable, unless we received one as parameter.
  23. // Note: As this is a public interface, we cannot do an instanceof check as we won't
  24. // be using the exact same object in memory.
  25. var obs = _.isArray(questions) ? Rx.Observable.from(questions) : questions;
  26. this.process = obs
  27. .concatMap(this.processQuestion.bind(this))
  28. // `publish` creates a hot Observable. It prevents duplicating prompts.
  29. .publish();
  30. this.process.connect();
  31. return this.process
  32. .reduce((answers, answer) => {
  33. _.set(this.answers, answer.name, answer.answer);
  34. return this.answers;
  35. }, {})
  36. .toPromise(Promise)
  37. .then(this.onCompletion.bind(this));
  38. }
  39. /**
  40. * Once all prompt are over
  41. */
  42. onCompletion(answers) {
  43. this.close();
  44. return answers;
  45. }
  46. processQuestion(question) {
  47. question = _.clone(question);
  48. return Rx.Observable.defer(() => {
  49. var obs = Rx.Observable.of(question);
  50. return obs
  51. .concatMap(this.setDefaultType.bind(this))
  52. .concatMap(this.filterIfRunnable.bind(this))
  53. .concatMap(() =>
  54. utils.fetchAsyncQuestionProperty(question, 'message', this.answers)
  55. )
  56. .concatMap(() =>
  57. utils.fetchAsyncQuestionProperty(question, 'default', this.answers)
  58. )
  59. .concatMap(() =>
  60. utils.fetchAsyncQuestionProperty(question, 'choices', this.answers)
  61. )
  62. .concatMap(this.fetchAnswer.bind(this));
  63. });
  64. }
  65. fetchAnswer(question) {
  66. var Prompt = this.prompts[question.type];
  67. this.activePrompt = new Prompt(question, this.rl, this.answers);
  68. return Rx.Observable.defer(() =>
  69. Rx.Observable.fromPromise(
  70. this.activePrompt.run().then(answer => ({ name: question.name, answer: answer }))
  71. )
  72. );
  73. }
  74. setDefaultType(question) {
  75. // Default type to input
  76. if (!this.prompts[question.type]) {
  77. question.type = 'input';
  78. }
  79. return Rx.Observable.defer(() => Rx.Observable.of(question));
  80. }
  81. filterIfRunnable(question) {
  82. if (question.when === false) {
  83. return Rx.Observable.empty();
  84. }
  85. if (!_.isFunction(question.when)) {
  86. return Rx.Observable.of(question);
  87. }
  88. var answers = this.answers;
  89. return Rx.Observable.defer(() =>
  90. Rx.Observable.fromPromise(
  91. runAsync(question.when)(answers).then(shouldRun => {
  92. if (shouldRun) {
  93. return question;
  94. }
  95. })
  96. ).filter(val => val != null)
  97. );
  98. }
  99. }
  100. module.exports = PromptUI;