039ec1d3907d27f474ffd428c6f6c52f3c43edd8
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / inquirer / lib / ui / prompt.js
1 'use strict';
2 var _ = {
3   isPlainObject: require('lodash/isPlainObject'),
4   clone: require('lodash/clone'),
5   isArray: require('lodash/isArray'),
6   set: require('lodash/set'),
7   isFunction: require('lodash/isFunction'),
8 };
9 var { defer, empty, from, of } = require('rxjs');
10 var { concatMap, filter, publish, reduce } = require('rxjs/operators');
11 var runAsync = require('run-async');
12 var utils = require('../utils/utils');
13 var Base = require('./baseUI');
14
15 /**
16  * Base interface class other can inherits from
17  */
18
19 class PromptUI extends Base {
20   constructor(prompts, opt) {
21     super(opt);
22     this.prompts = prompts;
23   }
24
25   run(questions, answers) {
26     // Keep global reference to the answers
27     if (_.isPlainObject(answers)) {
28       this.answers = _.clone(answers);
29     } else {
30       this.answers = {};
31     }
32
33     // Make sure questions is an array.
34     if (_.isPlainObject(questions)) {
35       questions = [questions];
36     }
37
38     // Create an observable, unless we received one as parameter.
39     // Note: As this is a public interface, we cannot do an instanceof check as we won't
40     // be using the exact same object in memory.
41     var obs = _.isArray(questions) ? from(questions) : questions;
42
43     this.process = obs.pipe(
44       concatMap(this.processQuestion.bind(this)),
45       publish() // Creates a hot Observable. It prevents duplicating prompts.
46     );
47
48     this.process.connect();
49
50     return this.process
51       .pipe(
52         reduce((answers, answer) => {
53           _.set(answers, answer.name, answer.answer);
54           return answers;
55         }, this.answers)
56       )
57       .toPromise(Promise)
58       .then(this.onCompletion.bind(this));
59   }
60
61   /**
62    * Once all prompt are over
63    */
64
65   onCompletion() {
66     this.close();
67
68     return this.answers;
69   }
70
71   processQuestion(question) {
72     question = _.clone(question);
73     return defer(() => {
74       var obs = of(question);
75
76       return obs.pipe(
77         concatMap(this.setDefaultType.bind(this)),
78         concatMap(this.filterIfRunnable.bind(this)),
79         concatMap(() =>
80           utils.fetchAsyncQuestionProperty(question, 'message', this.answers)
81         ),
82         concatMap(() =>
83           utils.fetchAsyncQuestionProperty(question, 'default', this.answers)
84         ),
85         concatMap(() =>
86           utils.fetchAsyncQuestionProperty(question, 'choices', this.answers)
87         ),
88         concatMap(this.fetchAnswer.bind(this))
89       );
90     });
91   }
92
93   fetchAnswer(question) {
94     var Prompt = this.prompts[question.type];
95     this.activePrompt = new Prompt(question, this.rl, this.answers);
96     return defer(() =>
97       from(
98         this.activePrompt
99           .run()
100           .then((answer) => ({ name: question.name, answer: answer }))
101       )
102     );
103   }
104
105   setDefaultType(question) {
106     // Default type to input
107     if (!this.prompts[question.type]) {
108       question.type = 'input';
109     }
110
111     return defer(() => of(question));
112   }
113
114   filterIfRunnable(question) {
115     if (question.askAnswered !== true && this.answers[question.name] !== undefined) {
116       return empty();
117     }
118
119     if (question.when === false) {
120       return empty();
121     }
122
123     if (!_.isFunction(question.when)) {
124       return of(question);
125     }
126
127     var answers = this.answers;
128     return defer(() =>
129       from(
130         runAsync(question.when)(answers).then((shouldRun) => {
131           if (shouldRun) {
132             return question;
133           }
134         })
135       ).pipe(filter((val) => val != null))
136     );
137   }
138 }
139
140 module.exports = PromptUI;