-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·159 lines (139 loc) · 4.26 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
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
#!/usr/bin/env node
// Imports
const chalk = require('chalk');
const cliTable = require('cli-table');
const fetch = require('node-fetch');
const fs = require('fs');
const ora = require('ora');
const os = require('os');
const yesno = require('yesno');
// Chalk
const green = chalk.green;
const error = chalk.bold.red;
const success = chalk.bold.green;
// Strings
const strings = {
"setupConfigCopied": `Config file created at ${os.homedir()}/.sentinel, edit this file for your desired config.`,
"setupConfigExists": `A config file already exists, do you want to overwrite it with the default? (y/n)`,
"initConnectionCheck": "Checking for an active Internet connection",
"initConnectionError": "✖ No active Internet connection found.",
"initNoConfigError": `✖ No config file found, run \`sentinel -s\` to setup.`,
"initAbsoluteURLError": `✖ Config contains non-absolute URLs, make sure all URLs are absolute (http or https) then try again.`
}
// Functions
function setup() {
// Check if config file already exists
fs.readFile(`${os.homedir()}/.sentinel`, (err, _) => {
if (!err) {
// Config file already exists, ask if user wants to overwrite with default
yesno.ask(strings.setupConfigExists, null, overwrite => {
overwrite ? setupCopy() : process.exit();
});
} else {
setupCopy();
}
});
}
function setupCopy() {
// Copy default config file to user's home directory
fs.copyFile(`${__dirname}/config.json`, `${os.homedir()}/.sentinel`, err => {
console.log(success(strings.setupConfigCopied));
process.exit();
});
}
function init() {
let config;
// Attempt to read config file
fs.readFile(`${os.homedir()}/.sentinel`, (err, data) => {
if (err) {
// No config file found
console.log(error(strings.initNoConfigError));
process.exit(1);
} else {
// Parse config file
config = JSON.parse(data);
// Check that all sites have absolute URLs
config.sites.forEach(site => {
if (!site.startsWith('http')) {
console.log(error(strings.initAbsoluteURLError));
process.exit(1);
}
});
// Check for an active Internet connection
const spinner = ora(strings.initConnectionCheck).start();
checkSite('http://www.google.com')
.then(site => {
spinner.stop();
if (site.up) {
main(config);
} else {
console.log(error(strings.initConnectionError));
process.exit(1);
}
});
}
});
}
function main(config) {
const startTime = new Date();
const spinner = ora('Checking sites').start();
Promise.all(config.sites.map(site => checkSite(site)))
.then(results => {
const timeTaken = (new Date() - startTime) / 1000;
spinner.succeed(`Checked ${results.length} ${results.length > 1 ? 'sites' : 'site'} in ${timeTaken} seconds`);
displayResults(results);
});
}
function checkSite(site) {
const startTime = new Date();
return fetch(site, { method: 'HEAD' })
.then(r => {
return {
url: site,
time: new Date() - startTime,
up: true,
}
})
.catch(e => {
return {
url: site,
time: new Date() - startTime,
up: false
}
});
}
function displayResults(results) {
const table = new cliTable({
chars: {
'top': '', 'top-mid': '', 'top-left': '', 'top-right': '',
'bottom': '', 'bottom-mid': '', 'bottom-left': '', 'bottom-right': '',
'left': '', 'left-mid': '', 'mid': '', 'mid-mid': '',
'right': '', 'right-mid': '', 'middle': ' '
},
style: {
'padding-left': 0,
'padding-right': 0
}
});
results.forEach(site => {
if (site.up) {
table.push([green('✔'), green(`${site.time}ms`), green(site.url)]);
} else {
table.push([error('✖'), error(`${site.time}ms`), error(site.url)]);
}
});
console.log(table.toString());
process.exit();
}
// Commander
const sentinel = require('commander');
sentinel
.version(require(`${__dirname}/package.json`).version)
.description(`A command-line site monitor\n Config file: ${os.homedir()}/.sentinel`)
.option('-s, --setup', 'setup config file in the home directory')
.parse(process.argv);
if (sentinel.setup) {
setup();
} else {
init();
}