-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (43 loc) · 1.43 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
const https = require("https");
const fs = require("fs");
const { config } = require("dotenv");
const links = require("./links.json");
config();
async function checkPageStatus(link) {
return new Promise((resolve) => {
const request = https.get(link, (response) => {
resolve(response.statusCode);
});
request.on("error", (err) => {
console.error(`Error checking link: ${link}`);
resolve(null);
});
request.setTimeout(5000, () => {
// 5 seconds timeout
console.error(`Timeout checking link: ${link}`);
request.abort();
resolve(404); // Treat timeout as page not found
});
});
}
async function main() {
const filePath = "./TODO.md";
let content = "# TODO List\n\n";
const linkPromises = links.map(async (rawLink, index) => {
try {
const link = rawLink.replace("{{language}}", process.env.LANGUAGE_CODE);
const status = await checkPageStatus(link);
const isChecked = status === 200;
const title = link.split("/").pop();
console.log(`Processing link ${index + 1}/${links.length}: ${link}`);
return `## ${title}\n- [${isChecked ? "x" : " "}] ${link}\n\n`;
} catch (err) {
console.error(`Error processing link: ${rawLink}`, err);
return `## ${rawLink}\n- [ ] ${rawLink}\n\n`;
}
});
const results = await Promise.all(linkPromises);
content += results.join("");
fs.writeFileSync(filePath, content, "utf8");
}
main();