forked from CartoDB/mobile-tile-packager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
75 lines (63 loc) · 2.01 KB
/
helpers.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
var fs = require('fs');
var util = require('util');
function fileExists(filePath){
try {
var file = fs.statSync(filePath);
return file['size'];
}
catch (err) {return false;}
}
function currdatetime() {
return new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
}
function report(stats, p) {
console.log(util.format('\r\033[K[%s] %s%% speed %s/s | done %s skipped %s | %s left',
pad(formatDuration(process.uptime()), 4, true),
pad((p.percentage).toFixed(4), 8, true),
pad(formatNumber(p.speed),4,true),
formatNumber(stats.done - stats.skipped),
formatNumber(stats.skipped),
formatDuration(p.eta)
));
}
function formatDuration(duration) {
var seconds = duration % 60;
duration -= seconds;
var minutes = (duration % 3600) / 60;
duration -= minutes * 60;
var hours = (duration % 86400) / 3600;
duration -= hours * 3600;
var days = duration / 86400;
return (days > 0 ? days + 'd ' : '') +
(hours > 0 || days > 0 ? hours + 'h ' : '') +
(minutes > 0 || hours > 0 || days > 0 ? minutes + 'm ' : '') +
seconds + 's';
}
function pad(str, len, r) {
while (str.length < len) str = r ? ' ' + str : str + ' ';
return str;
}
function formatNumber(num) {
num = num || 0;
if (num >= 1e6)
return (num / 1e6).toFixed(2) + 'm';
else if (num >= 1e3)
return (num / 1e3).toFixed(1) + 'k';
else
return num.toFixed(0);
return num.join('.');
}
function msToTime(duration) {
var milliseconds = parseInt((duration%1000)/100),
seconds = parseInt((duration/1000)%60),
minutes = parseInt((duration/(1000*60))%60),
hours = parseInt((duration/(1000*60*60))%24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
return hours + ":" + minutes + ":" + seconds + "." + milliseconds;
}
module.exports.msToTime = msToTime;
module.exports.report = report;
module.exports.currdatetime = currdatetime;
module.exports.fileExists = fileExists;