-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
79 lines (66 loc) · 2.11 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
'use strict';
const consoleColumns = require('./lib/console-columns');
const fetchAllSitemaps = require('./lib/fetch-from-sitemaps');
const fs = require('fs');
const generateExcel = require('./lib/generate-excel');
const httpList = require('./lib/http-list');
/**
* Main function that handles config, and trigger functions according to the
* config.
*/
async function checkHttpStatus(config) {
const allowedExportTypes = [
'csv',
'xlsx',
];
var skip200 = false;
var urlsList = [];
if (config.skip200) {
skip200 = true;
}
if (!config) {
console.error('\x1b[31m%s\x1b[0m', 'Error: Missing required Parameters.');
process.exit();
} else if (!config.options) {
config.options = {};
}
if (config.sitemaps) {
urlsList = await fetchAllSitemaps(config.sitemaps);
}
if (config.urls && Array.isArray(config.urls)) {
urlsList = urlsList.concat(config.urls);
}
if (urlsList.length === 0) {
console.error('\x1b[31m%s\x1b[0m', 'Error: No URL(s) found.');
process.exit();
} else if (config.export && !config.export.location) {
console.error('\x1b[31m%s\x1b[0m', 'Error: Missing export location.');
process.exit();
} else if (config.export && !fs.existsSync(config.export.location)) {
console.error('\x1b[31m%s\x1b[0m', 'Error: Export Location is undefined.');
process.exit();
}
const httpStatusList = await httpList(urlsList, config.options, skip200);
if (config.export && !config.export.format) {
config.export.format = 'xlsx';
}
if (skip200 && httpStatusList.length === 0) {
// Add empty line
console.log();
console.log('\x1b[32m%s\x1b[0m', 'All the URLs are 200 and there is nothing to worry about!');
} else if (config.export && allowedExportTypes.includes(config.export.format)) {
const urlLength = Math.max(...urlsList.map((el) => el.length));
const rowLength = {
'errorMessage': 50,
'requestedUrl': urlLength,
'url': urlLength + 20
};
generateExcel(httpStatusList, rowLength, config.export);
} else {
consoleColumns(httpStatusList);
}
// Add empty line
console.log();
console.log('\x1b[32m%s\x1b[0m', 'HTTP Status check completed!');
}
module.exports = checkHttpStatus;