UNPKG

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