-
Notifications
You must be signed in to change notification settings - Fork 7
/
check-urls.js
79 lines (75 loc) · 2.01 KB
/
check-urls.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
const glob = require('glob');
const fs = require('fs');
let axios = require('axios');
let i = 0;
const hasError = [];
/**
* Verify all url
* @param {*} urls
*/
async function verifyUrls(urls) {
try {
const res = await axios.get(urls[i]);
if (res.status === 404) {
hasError.push(urls[i]);
throw new Error(`${res.status} ${res.statusText} : ${urls[i]}`);
} else if (res.status === 200) {
console.log('\x1b[32m', `${res.status} ${res.statusText} : ${urls[i]}`);
} else {
console.log('\x1b[35m', `${res.status} ${res.statusText} : ${urls[i]}`);
}
} catch (error) {
console.log('\x1b[31m', error.message);
}
i++;
if (i < urls.length) {
verifyUrls(urls);
} else if (hasError.length) {
throw new Error('Please fix 404 Not Found errors.');
}
}
/**
* Return all urls in a string
* @param {*} message
* @returns
*/
function detectURLs(message) {
var urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
return message.match(urlRegex);
}
const ignoreDomain = [
'localhost',
'twitter',
'discord',
'api.badger.com',
'rpc.fantom.tools',
'rpc.xdaichain.com',
'bsc-dataseed.binance.org',
'rpc-mainnet.matic.quiknode.pro',
'arb1.arbitrum.io/rpc',
];
/**
* Get path of all files under folder src
* @param {*} src
* @param {*} callback
*/
const getDirectories = function (src, callback) {
glob(src + '/**/*', callback);
};
getDirectories('src', function (err, res) {
if (err) {
console.log('Error', err);
} else {
const allFiles = res.filter((r) => r.includes('.ts') || r.includes('.tsx'));
let allUrls = [];
allFiles.forEach((url) => {
const text = fs.readFileSync(url, 'utf8');
const urls = detectURLs(text);
urls && allUrls.push(...urls);
});
allUrls = Array.from(new Set(allUrls)).map((url) => url.replace(/[,'"><(){};`]/gi, ''));
allUrls = allUrls.filter((url) => !ignoreDomain.some((domain) => url.includes(domain)));
console.log('\x1b[34m', `Total URLS: ${allUrls.length} `);
verifyUrls(allUrls);
}
});