1 | (function() {
|
2 | var indexOf = [].indexOf;
|
3 |
|
4 | module.exports = function($) {
|
5 | var Prompt, _;
|
6 | ({_} = $);
|
7 | Prompt = (function() {
|
8 | class Prompt {
|
9 | async execute_(option) {
|
10 | var res, resRaw, type;
|
11 | type = $.type(option);
|
12 | if (type !== 'object') {
|
13 | throw new Error(`invalid type '${type}'`);
|
14 | }
|
15 | $.info.pause(this.namespace);
|
16 | option = _.cloneDeep(option);
|
17 | option = (await this.setOption_(option));
|
18 |
|
19 |
|
20 | this.fn_ || (this.fn_ = require('prompts'));
|
21 | resRaw = (await this.fn_(option));
|
22 | res = resRaw[option.name];
|
23 | await this.setCache_(option, res);
|
24 | $.info.resume(this.namespace);
|
25 |
|
26 |
|
27 | if (option.raw) {
|
28 | return resRaw;
|
29 | }
|
30 | return res;
|
31 | }
|
32 |
|
33 | async getCache_(option) {
|
34 | var cache, index, item, ref, type, value;
|
35 | if (!option.id) {
|
36 | return void 0;
|
37 | }
|
38 | if (ref = option.type, indexOf.call(this.listTypeCache, ref) < 0) {
|
39 | return void 0;
|
40 | }
|
41 | cache = (await $.read_(this.pathCache));
|
42 | if (!(item = _.get(cache, option.id))) {
|
43 | return void 0;
|
44 | }
|
45 | ({type, value} = item);
|
46 | if (type !== option.type) {
|
47 | return void 0;
|
48 | }
|
49 |
|
50 |
|
51 | if (type === 'select') {
|
52 | index = _.findIndex(option.choices, {value});
|
53 | if (!(index > -1)) {
|
54 | return void 0;
|
55 | }
|
56 | return index;
|
57 | }
|
58 | return value;
|
59 | }
|
60 |
|
61 | async setCache_(option, value) {
|
62 | var cache, id, type;
|
63 | ({id, type} = option);
|
64 | if (!(id && (value != null) && indexOf.call(this.listTypeCache, type) >= 0)) {
|
65 | return this;
|
66 | }
|
67 | cache = (await $.read_(this.pathCache));
|
68 | cache || (cache = {});
|
69 | cache[option.id] = {type, value};
|
70 | await $.write_(this.pathCache, cache);
|
71 | return this;
|
72 | }
|
73 |
|
74 | async setOption_(option) {
|
75 | var i, item, j, len, ref, ref1, ref2;
|
76 | if (ref = option.type, indexOf.call(this.listType, ref) < 0) {
|
77 | throw new Error(`invalid type '${option.type}'`);
|
78 | }
|
79 |
|
80 |
|
81 | option.message || (option.message = this.mapMessage[option.type] || 'input');
|
82 | option.name || (option.name = 'value');
|
83 | if ((ref1 = option.type) === 'autocomplete' || ref1 === 'multiselect' || ref1 === 'select') {
|
84 | if (!(option.choices || (option.choices = option.choice || option.list))) {
|
85 | throw new Error('got no choice(s)');
|
86 | }
|
87 | ref2 = option.choices;
|
88 | for (i = j = 0, len = ref2.length; j < len; i = ++j) {
|
89 | item = ref2[i];
|
90 | if ('object' === $.type(item)) {
|
91 | continue;
|
92 | }
|
93 | option.choices[i] = {
|
94 | title: item,
|
95 | value: item
|
96 | };
|
97 | }
|
98 | } else if (option.type === 'toggle') {
|
99 | option.active || (option.active = 'on');
|
100 | option.inactive || (option.inactive = 'off');
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 | option.initial || (option.initial = option.default || (await this.getCache_(option)));
|
106 | return option;
|
107 | }
|
108 |
|
109 | };
|
110 |
|
111 | |
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | Prompt.prototype.listType = ['autocomplete', 'confirm', 'multiselect', 'number', 'select', 'text', 'toggle'];
|
124 |
|
125 | Prompt.prototype.listTypeCache = ['autocomplete', 'confirm', 'number', 'select', 'text', 'toggle'];
|
126 |
|
127 | Prompt.prototype.mapMessage = {
|
128 | confirm: 'confirm',
|
129 | multiselect: 'select',
|
130 | number: 'input number',
|
131 | select: 'select',
|
132 | text: 'input text',
|
133 | toggle: 'toggle'
|
134 | };
|
135 |
|
136 | Prompt.prototype.namespace = '$.prompt_';
|
137 |
|
138 | Prompt.prototype.pathCache = './temp/cache-prompt.json';
|
139 |
|
140 | return Prompt;
|
141 |
|
142 | }).call(this);
|
143 |
|
144 |
|
145 | return $.prompt_ = async function(...arg) {
|
146 | var base, prompt_;
|
147 | prompt_ = (base = $.prompt_).fn_ || (base.fn_ = new Prompt());
|
148 | return (await prompt_.execute_(...arg));
|
149 | };
|
150 | };
|
151 |
|
152 | }).call(this);
|