-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (61 loc) · 1.42 KB
/
index.js
File metadata and controls
71 lines (61 loc) · 1.42 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
'use strict';
const ms = require('ms')
, Timer = require('timer.js')
, ProgressBar = require('progress')
, hdate = require('human-date')
, notifier = require('node-notifier')
, argv = require('minimist')(process.argv.slice(1), { boolean: 'd' })
, log = console.log
;
if (argv.d) {
log('Timer started');
require('daemon')();
}
if (process.env.NODE_ENV !== 'test') {
// find the time argument
const timeArg = argv._.find(arg => ms(arg));
if (!timeArg) {
throw new Error('Invalid time, try "5min"');
}
toZero(timeArg);
}
function toZero(timeArg, done) {
// convert to ms
const time = ms(timeArg);
let bar;
const tick = 0.5;
const timer = new Timer({
tick,
onstart,
ontick,
onend
}).start(time / 1000);
function onstart() {
const timeStr = hdate.relativeTime(time / 1000, {
futureSuffix: '…',
allUnits: true
});
log(`Counting down ${timeStr}`);
bar = new ProgressBar('[:bar]', {
complete: '=',
incomplete: ' ',
width: 60,
total: time
});
bar.tick(0);
}
function ontick(msLeft) {
bar.tick(tick * 1000);
}
function onend() {
if (typeof done === 'function') {
return done();
}
bar.tick(time);
const title = 'to-zero countdown complete';
const msg = { message: argv.m || title };
if (msg.message != title) msg.title = title;
notifier.notify(msg);
}
}
module.exports = toZero;