-
Notifications
You must be signed in to change notification settings - Fork 12
/
cli.js
executable file
·115 lines (95 loc) · 2.42 KB
/
cli.js
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
#!/usr/bin/env node
'use strict';
const updateNotifier = require('update-notifier');
const meow = require('meow');
const moment = require('moment-timezone');
const devTime = require('dev-time');
const logSymbols = require('log-symbols');
const chalk = require('chalk');
const elegantSpinner = require('elegant-spinner');
const logUpdate = require('log-update');
const cli = meow(`
Usage
$ dev-time <user> <user2> ...
Options
-v, --verbose Show UTC offset.
--format The moment format of the output. [Default: HH:mm - D MMM. YYYY]
--token The GitHub authentication token.
Examples
$ dev-time SamVerschueren
19:47 - 8 Dec. 2015
$ dev-time SamVerschueren sindresorhus
SamVerschueren
19:47 - 8 Dec. 2015
sindresorhus
18:47 - 8 Dec. 2015
$ dev-time SamVerschueren -v
19:47 - 8 Dec. 2015 - UTC+1
$ dev-time SamVerschueren --format DD-MM-YYYY
07-12-2015
`, {
string: ['_'],
boolean: ['verbose'],
alias: {
v: 'verbose'
},
default: {
format: 'HH:mm - D MMM. YYYY'
}
});
updateNotifier({pkg: cli.pkg}).notify();
if (cli.input.length === 0) {
console.error('Provide a GitHub user');
process.exit(1);
}
const result = {};
const frames = {};
cli.input.forEach(user => {
frames[user] = elegantSpinner();
});
function parseError(err) {
if (/no contributions/.test(err.message) || /Rate limit exceeded/.test(err.message)) {
return ` ${logSymbols.error} ${chalk.red('error')} ${err.message.toLowerCase()}`;
}
return err.stack;
}
function render() {
const output = [];
cli.input.forEach(user => {
if (cli.input.length === 1) {
output.push(result[user].stack ? parseError(result[user]) : result[user]);
return;
}
output.push(user);
if (result[user]) {
if (result[user] instanceof Error) {
output.push(parseError(result[user]));
} else {
output.push(` ${result[user]}`);
}
} else {
output.push(` ${chalk.gray.dim(frames[user]())}`);
}
});
logUpdate(output.join('\n'));
}
if (cli.input.length > 1) {
setInterval(render, 50);
}
Promise.all(cli.input.map(user =>
devTime(user, cli.flags)
.then(time => {
const date = moment.utc(time).utcOffset(time);
result[user] = date.format(cli.flags.format);
if (cli.flags.verbose) {
const sign = date.utcOffset() >= 0 ? '+' : '';
result[user] += ` - UTC${sign}${date.utcOffset() / 60}`;
}
})
.catch(err => {
result[user] = err;
})
)).then(() => {
render();
process.exit();
});