forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
258 lines (215 loc) · 5.55 KB
/
cli.js
File metadata and controls
258 lines (215 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
const _ = require('underscore');
const colors = require('colors');
const prompt = require('prompt');
const spinner = require('node-spinner')();
const cursor = require('cli-cursor');
const progress = require('progress');
spinner.set('|/-\\');
/**
* cli helper
*/
class cli {
constructor(args) {
this.args = args;
this.data = [];
}
/*
* emojis
*/
static get print() {
return '🖨';
}
static get look() {
return '🔍';
}
static get fetch() {
return '📦';
}
static get process() {
return '⚙️';
}
static get output() {
return '📃';
}
static get party() {
return '🥑';
// return '🍺';
}
/**
* ask
* @param message
* @returns {Promise}
*/
static ask(message) {
return new Promise((resolve, reject) => {
prompt.start();
let question = {
name: 'yesno',
message: message,
validator: /y[es]*|n[o]?/,
warning: 'Must respond yes or no',
default: 'yes'
};
prompt.get(question, function (error, result) {
if (error || result.yesno === 'no' || result.yesno === 'n') return reject(error);
resolve();
});
});
}
/**
* print
* @param string
*/
static out(string) {
if (cli.quiet) return;
process.stdout.write(string);
}
/**
* print done message
*/
static done() {
cli.out(`\n${cli.party} Finished!\n`.green);
}
/**
* print a warning
* @param message
*/
static warn(message) {
cli.out(` Warning: ${message} `.bgWhite.black + "\n");
}
/**
* create a new bar
* @param message
* @param total
* @returns {*}
*/
static bar(message, total) {
cli.resolve(false);
if (cli.quiet) return cli.promise();
this.active = {
started: new Date(),
message: `\r${message}... `.bold.grey,
bar: new progress(`${message} (:current/:total) [:bar] :percent - :minutesm left`, {
total,
clear: true,
width: 40,
renderThrottle: 100
}),
interval: setInterval(() => {
if (!cli.active.bar || cli.active.bar.complete) return clearInterval(cli.active.interval);
cli.tick(0);
}, 1000)
};
this.tick();
return cli.promise();
}
/**
* bar tick
* @param amount
*/
static tick(amount = 0) {
if (!cli.active.bar || !cli.active.started) return;
let left;
if (cli.active.bar.curr > 0) {
let elapsed = Math.ceil((new Date() - cli.active.started) / 1000);
left = ((elapsed / cli.active.bar.curr) * (cli.active.bar.total - cli.active.bar.curr)) / 60;
left = left < 1 ? `<1` : Math.ceil(left);
} else {
left = 0;
}
cli.active.bar.tick(amount, {
minutes: left
});
}
/**
* advance an existing bar
*/
static advance() {
cli.tick(1);
}
/**
* create a new list, including a spinner
* @param message
* @returns {*}
*/
static list(message) {
cli.resolve(false);
this.active = {message: `\r${message}... `.bold.grey};
this.active.interval = setInterval(() => {
cli.out(cli.active.message + spinner.next().bold.blue);
}, 100);
return cli.promise();
}
/**
* stop a list item with a check mark
* @returns {*}
*/
static mark() {
cli.resolve();
cli.out(`${cli.active.message}` + `✓\n`.green);
return cli.promise();
}
/**
* stop a list item with an x
* @param message
* @param error
* @returns {*}
*/
static x(message = false, error = false) {
cli.resolve();
cli.out(`${cli.active.message}` + `✗\n`.red);
if (message) cli.error(message, error);
return cli.promise();
}
/**
* stop and resolve a list or bar
*/
static resolve(show = true) {
cursor.toggle(show);
if (cli.active && cli.active.interval) clearInterval(cli.active.interval);
}
/**
* show an error message
* @param message
* @param error
* @returns {*}
*/
static error(message, error) {
cli.resolve();
cli.out(`Error: ${message.red}` + '\n');
if (error) console.log(error);
process.exit(1);
}
/**
* get a promise (for chaining promises)
* @returns {Promise}
*/
static promise() {
return new Promise(resolve => {
resolve();
});
}
/**
* parse the args and return the project argument
* @returns {*}
*/
project() {
if (!this.args[0] && !this.data.project) return null;
if (this.data.project) return this.data.project;
return this.data.project = this.args.splice(0, 1).toString();
}
/**
* parse the args and return an array of issues
* @returns {*}
*/
iids() {
if (this.data.iids) return this.data.iids;
this.data.iids = _.uniq(_.flatten(_.map(this.args, (issue) => {
if (issue.indexOf(',') === -1) return issue;
return issue.split(',');
})));
if (this.data.iids.length === 0) return null;
return this.data.iids;
}
}
module.exports = cli;