forked from Asin-Junior-Honore/DevPortfolioHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateReadme.js
50 lines (38 loc) · 1.55 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
const fs = require("fs");
try {
if (!fs.existsSync("contributors.json")) {
throw new Error("contributors.json file not found.");
}
const contributorsData = JSON.parse(
fs.readFileSync("contributors.json", "utf8")
);
if (!Array.isArray(contributorsData)) {
throw new Error("Contributors data is not an array.");
}
contributorsData.sort((a, b) => a.name.localeCompare(b.name));
const contributorsMarkdown = contributorsData
.map((contributor) => {
// Check if the portfolio URL already includes http:// or https:// prefix
const portfolioUrl = /^https?:\/\//.test(contributor.portfolio)
? contributor.portfolio
: `https://${contributor.portfolio}`;
// Encode special characters in the contributor name
const encodedName = encodeURIComponent(contributor.name);
return `* [${encodedName}](${portfolioUrl})`;
})
.join("\n");
let readme = fs.readFileSync("README.md", "utf8");
// Find <!-- CONTRIBUTORS_START --> and <!-- CONTRIBUTORS_END --> placeholders in README.md
const startPattern =
/<!-- CONTRIBUTORS_START -->[\s\S]*?<!-- CONTRIBUTORS_END -->/;
const endPattern = /<!-- CONTRIBUTORS_END -->/;
// Replace content between placeholders with updated contributors list
readme = readme.replace(
startPattern,
`<!-- CONTRIBUTORS_START -->\n${contributorsMarkdown}\n<!-- CONTRIBUTORS_END -->`
);
fs.writeFileSync("README.md", readme, "utf8");
console.log("Readme updated successfully!");
} catch (error) {
console.error("Error updating readme:", error.message);
}