prompt.js 3.3 KB

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