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
169 lines (141 loc) · 3.72 KB
/
cli.js
File metadata and controls
169 lines (141 loc) · 3.72 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
const _ = require('underscore');
const colors = require('colors');
const spinner = require('node-spinner')();
const cursor = require('cli-cursor');
const progress = require('progress');
spinner.set('|/-\\');
class cli {
/**
* pass arguments to cli helper
* @param args
*/
constructor(args) {
this.args = args;
this.data = [];
}
static done() {
console.log();
console.log(`🍺 finished!`.green);
}
/**
* print a warning
* @param message
*/
static warn(message) {
console.log(` Warning: ${message} `.bgWhite.black);
}
/**
* create a new bar
* @param message
* @param total
* @returns {*}
*/
static bar(message, total) {
cli.resolve(false);
let options = {
total,
clear: true,
width: 40,
renderThrottle: 100
};
this.active = {
message: `\r${message}... `.bold.grey,
bar: new progress(`${message} (:current/:total) [:bar] :percent - :etas left`, options)
};
this.active.bar.tick(0);
return cli.promise();
}
/**
* advance an existing bar
*/
static advance() {
if (!this.active.bar) return;
this.active.bar.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(() => {
process.stdout.write(this.active.message + spinner.next().bold.blue);
}, 100);
return cli.promise();
}
/**
* stop a list item with a check mark
* @returns {*}
*/
static mark() {
process.stdout.write(`${this.active.message}` + `✓\n`.green);
cli.resolve();
return cli.promise();
}
/**
* stop a list item with an x
* @param message
* @param error
* @returns {*}
*/
static x(message = false, error = false) {
process.stdout.write(`${this.active.message}` + `✗\n`.red);
cli.resolve();
if (message) cli.error(message, error);
return cli.promise();
}
/**
* stop and resolve a list or bar
*/
static resolve(show = true) {
cursor.toggle(show);
if (this.active && this.active.interval) clearInterval(this.active.interval);
if (this.active) this.active = false;
}
/**
* show an error message
* @param message
* @param error
* @returns {*}
*/
static error(message, error) {
cli.resolve();
console.log(` Error: ${message} `.bgRed.white);
if (error) console.log(error);
return cli.promise();
}
/**
* 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;