@jozsefsallai/userv
Version:
uCoz server status checker.
60 lines (59 loc) • 2.21 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const node_iro_1 = require("node-iro");
const axios_1 = require("axios");
class ValidationError extends Error {
constructor(message) {
super();
this.name = 'ValidationError';
this.message = message;
}
}
class Site extends core_1.Command {
validateUrl(url) {
url = url.toLowerCase();
if (!url.startsWith('http://') && !url.startsWith('https://')) {
url = `http://${url}`;
}
if (url
.split('://')[1]
.split('.')
.filter((component) => component.length && !component.includes('/')).length < 2) {
return false;
}
return url;
}
getServerNumber(serverString) {
const matches = serverString.match(/\(s([0-9]+)\)/g);
return matches && matches[0] && matches[0].slice(1, -1);
}
async run() {
const { args } = await this.parse(Site);
const url = this.validateUrl(args.url);
if (!url) {
return this.error('Please provide a valid URL.');
}
try {
const html = await axios_1.default.get(url).then((res) => res.data);
const matches = html.match(/<!-- [0-9].([0-9]+) \(s([0-9]+)\) -->/g);
if (!matches) {
throw new ValidationError('That is not a uCoz website.');
}
const server = this.getServerNumber(matches[0]);
return this.log((0, node_iro_1.default)(`✔️ The site ${args.url} is hosted on ${server}.`, node_iro_1.green));
}
catch (err) {
if (err.name === 'ValidationError') {
return this.error((0, node_iro_1.default)(`❌ ${err}`, node_iro_1.red));
}
return this.error((0, node_iro_1.default)('❌ The provided website is not available.', node_iro_1.red));
}
}
}
exports.default = Site;
Site.description = 'Check what server your website is hosted on.';
Site.examples = [
'$ userv site forum.ucoz.com\n✔️ The site forum.ucoz.com is hosted on s101.',
];
Site.args = [{ name: 'url', required: true }];
;