/
index.js
106 lines (85 loc) · 2.42 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
const fs = require("fs");
const fetch = require("node-fetch");
const path = require("path");
const colors = require("colors");
// exitCode
process.on("SIGTERM", () => {
server.close(() => {
console.log("Program terminated");
});
});
// flag labeling each URL as a good or bad
const _label = {
good: "GOOD",
bad: "BAD",
};
// const getTelescope = fetch('http:/') data
async function getTelescopeData() {
// fetching data from local host
const response = await fetch("http://localhost:3000/posts");
const data = await response.json();
fs.truncateSync("telescopeData.txt", 0);
const postPromises = [];
//truncate the data and write to file
for (i = 0; i < data.length; i++) {
const promise = fetch(`http://localhost:3000${data[i].url}`)
.then((res) => {
return res.json();
})
.then((postObj) => {
return postObj.feed.url;
});
postPromises.push(promise);
}
const posts = await Promise.all(postPromises);
fs.writeFileSync("telescopeData.txt", posts);
}
//getTelescopeData(); //taking telescope URL data
if (process.argv.length == 2) {
console.log("Hello");
} else {
if (process.argv[2] == "v" || process.argv[2] == "version") {
console.log(packageJson.name + " Version " + packageJson.version);
} else {
const filePath = path.join(__dirname, process.argv[2]);
processLinks(filePath);
}
}
async function processLinks(p_filePath) {
let allGoodLinks = true;
let data;
try {
data = fs.readFileSync(p_filePath, "utf-8");
} catch (err) {
console.log("Fail to read file", err);
return;
}
const validUrl = data.match(
/(http|https)(:\/\/)([\w+\-&@`~#$%^*.=/?:]+)/gi,
);
// arr to store promises
const urlPromises = [];
validUrl.forEach((url) => {
// store each fetch call in arr
const promise = fetch(url, { method: "HEAD", timeout: 2000 })
.then((res) => {
if (res.status == 200) {
console.log(res.status, url.green, _label.good.rainbow);
}
if (res.status == 400 || res.status == 404) {
console.log(res.status, url).then.process(); // termination
allGoodLinks = false;
}
})
.catch((error) => {
console.log("404", url.red, _label.bad.bgRed);
});
urlPromises.push(promise);
});
// run all fetch call promises
const fetchResults = await Promise.all(urlPromises);
return allGoodLinks;
}
module.exports = {
processLinks,
};