Giant blob of minor changes
[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), this.onError.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   onError(error) {
72     this.close();
73     return Promise.reject(error);
74   }
75
76   processQuestion(question) {
77     question = _.clone(question);
78     return defer(() => {
79       var obs = of(question);
80
81       return obs.pipe(
82         concatMap(this.setDefaultType.bind(this)),
83         concatMap(this.filterIfRunnable.bind(this)),
84         concatMap(() =>
85           utils.fetchAsyncQuestionProperty(question, 'message', this.answers)
86         ),
87         concatMap(() =>
88           utils.fetchAsyncQuestionProperty(question, 'default', this.answers)
89         ),
90         concatMap(() =>
91           utils.fetchAsyncQuestionProperty(question, 'choices', this.answers)
92         ),
93         concatMap(this.fetchAnswer.bind(this))
94       );
95     });
96   }
97
98   fetchAnswer(question) {
99     var Prompt = this.prompts[question.type];
100     this.activePrompt = new Prompt(question, this.rl, this.answers);
101     return defer(() =>
102       from(
103         this.activePrompt
104           .run()
105           .then((answer) => ({ name: question.name, answer: answer }))
106       )
107     );
108   }
109
110   setDefaultType(question) {
111     // Default type to input
112     if (!this.prompts[question.type]) {
113       question.type = 'input';
114     }
115
116     return defer(() => of(question));
117   }
118
119   filterIfRunnable(question) {
120     if (question.askAnswered !== true && this.answers[question.name] !== undefined) {
121       return empty();
122     }
123
124     if (question.when === false) {
125       return empty();
126     }
127
128     if (!_.isFunction(question.when)) {
129       return of(question);
130     }
131
132     var answers = this.answers;
133     return defer(() =>
134       from(
135         runAsync(question.when)(answers).then((shouldRun) => {
136           if (shouldRun) {
137             return question;
138           }
139         })
140       ).pipe(filter((val) => val != null))
141     );
142   }
143 }
144
145 module.exports = PromptUI;