UI for Zipcoin Blue

prompt.js 3.2KB

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