-
Notifications
You must be signed in to change notification settings - Fork 485
/
updateReadme.js
60 lines (52 loc) · 2.23 KB
/
updateReadme.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
const axios = require("axios");
const fs = require("fs");
const os = require("os");
const owner = "kaal-coder";
const repo = "hacktoberfest2023";
const filePath = "./README.md";
const fetchDataAndUpdateReadme = async () => {
try {
// Fetch contributors' data from varunrmantri23/workflows
const response = await axios.get(
`https://api.github.com/repos/${owner}/${repo}/contributors?per_page=500`
);
const contributors = response.data;
// Build a new list of contributors with images and names
let contributorsList = "## Contributors to this Repository\n\n";
contributors.forEach((contributor) => {
contributorsList += `<a href="https://github.com/${contributor.login}" target="_blank"><img src="${contributor.avatar_url}" alt="${contributor.login}" style="border-radius: 50%; width: 50px; height: 50px;"></a>\n`;
});
// Read your existing README content with consistent line endings
let readmeContent = fs
.readFileSync(filePath, "utf8")
.replace(/\r\n/g, "\n");
// Check if the "Contributors to this Repository" section already exists
const sectionExists = readmeContent.includes(
"## Contributors to this Repository"
);
if (sectionExists) {
// If the section exists, replace it with the new contributors
const startIndex = readmeContent.indexOf(
"## Contributors to this Repository"
);
const endIndex = readmeContent.indexOf("##", startIndex + 1);
readmeContent =
readmeContent.slice(0, startIndex) +
contributorsList +
readmeContent.slice(endIndex);
} else {
// If the section doesn't exist, simply add it with the new contributors
readmeContent += contributorsList;
}
// Write the updated content with consistent line endings
fs.writeFileSync(
filePath,
readmeContent.replace(/\n/g, os.EOL),
"utf8"
);
console.log("Updated Readme.md with new contributors section");
} catch (error) {
console.error("Error:", error);
}
};
fetchDataAndUpdateReadme();