forked from govindkrishna03/First_PR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
82 lines (65 loc) · 1.91 KB
/
script.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
const express = require("express");
const fs = require("fs");
const path = require("path");
const cheerio = require("cheerio");
const chokidar = require("chokidar");
const app = express();
const port = process.env.PORT || 3000;
const directoryPath = "./contributions/";
function updateReadme() {
fs.readdir(directoryPath, (err, files) => {
if (err) {
console.error("Error reading directory:", err);
return;
}
const tableRows = files
.filter((file) => file.endsWith(".html"))
.map((file) => {
const fileName = path.parse(file).name;
const filePath = path.join(directoryPath, file);
try {
const fileContent = fs.readFileSync(filePath, "utf8");
const $ = cheerio.load(fileContent);
const imgSrc = $("img").attr("src");
const name = $(".name1").text();
const message = $(".message1").text();
return `
<tr>
<td align="center"><img src="${imgSrc}" width="200px" height="100px" alt=""/><br /><sub><b>${name}</b></sub><br /></td>
<td align="center"><i>${message}</i></td>
</tr>`;
} catch (err) {
console.error(`Error reading file ${file}:`, err);
return `| Error reading file | |`;
}
});
const tableContent = `
<table align="center" >
<tr>
<th>Contributor</th>
<th>Contribution</th>
</tr>
${tableRows.join("\n")}
</table>`;
const readmeContent = `
# Contributions
${tableContent}
`;
const readmeFilePath = path.join(directoryPath, "readme.md");
fs.writeFileSync(readmeFilePath, readmeContent);
});
}
const watcher = chokidar.watch(directoryPath, {
ignored: /(^|[\/\\])\../,
persistent: true,
});
watcher.on("add", () => {
updateReadme();
});
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, directoryPath, "readme.md"));
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
updateReadme();
});