-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (75 loc) · 2.08 KB
/
index.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
const meow = require('meow');
const isonline = require('is-online');
const readline = require('readline');
const fs = require('fs');
// Current iteration count
let iteration = 0;
// Number of times successfully connected to internet
let connectedTries = 0;
const cli = meow(`
Usage
$ is-online-stats <input>
Options
--wait, -w Number of minutes to wait between checks (Default: 10 minutes)
--iterations, -i Number of iterations (Default: infinite)
--timeout, -t Timeout in seconds to wait for a server to respond (Default: 2 seconds)
`, {
flags: {
wait: {
type: 'integer',
alias: 'w',
default: 10
},
iterations: {
type: 'integer',
alias: 'i',
default: -1
},
timeout: {
type: 'integer',
alias: 't',
default: 2
},
file: {
type: 'string',
alias: 'f',
default: 'log.txt'
}
}
});
// Number of iterations to try, -1 will cause it to continually run
const iterations = cli.flags.iterations;
// Amount of time to wait between checks
const waitTimeout = cli.flags.wait * 1000 * 60;
// Logfile for keeping track
let logFile = -1;
function next() {
iteration++;
// Check to see if we are online or not
isonline({
timeout: cli.flags.timeout * 1000
}).then(online => {
// Keep track of successful connections and log percentage to console
connectedTries += online ? 1 : 0;
readline.cursorTo(process.stdout, 0);
process.stdout.write(((connectedTries / iteration) * 100) + '% up-time')
// And keep track of each in file
fs.writeSync(logFile, iteration + ',' + (online ? 1 : 0) + '\n');
// If we are not done, will call the next one on the wait timeout
if (iterations === -1 || iteration < iterations) {
setTimeout(next, waitTimeout);
} else {
fs.closeSync(logFile);
}
});
}
try {
logFile = fs.openSync(cli.flags.file, 'w');
fs.writeSync(logFile, 'iteration,online\n');
next();
} catch (err) {
console.log('Failed due to err: %s', err);
if (logFile) {
fs.closeSync(logFile);
}
}