Skip to content

Commit 73f4013

Browse files
authored
Update update-readme.js
1 parent a0c301b commit 73f4013

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

update-readme.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,29 @@ const fs = require('fs');
22

33
// Load parsed badges and README
44
const parsedBadges = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
5-
const readmeContent = fs.readFileSync('README.md', 'utf8');
5+
let readmeContent = fs.readFileSync('README.md', 'utf8');
66

7-
// Regex to match plugins and their download badge URLs in the README
7+
// Regex to match the plugins list section in the README
8+
const sectionRegex = /<!-- START PLUGINS LIST -->\n([\s\S]*?)\n<!-- END PLUGINS LIST -->/;
9+
10+
// Extract existing plugins list section
11+
const sectionMatch = sectionRegex.exec(readmeContent);
12+
if (!sectionMatch) {
13+
console.error('Error: Could not find the plugins list section in the README.');
14+
process.exit(1);
15+
}
16+
17+
let pluginsListContent = sectionMatch[1].trim();
18+
19+
// Regex to match individual plugins in the extracted section
820
const pluginRegex = /- ### (.*?).*?\s*<br>\n\n\s*!GitHub Downloads all releases(.*?)(.*?)/g;
921

10-
// Map to hold parsed plugin entries from the README
22+
// Map to hold parsed plugin entries from the extracted section
1123
const readmePlugins = new Map();
1224
let match;
1325

14-
// Parse README content to extract plugin names and their download URLs
15-
while ((match = pluginRegex.exec(readmeContent)) !== null) {
26+
// Parse the plugins list to extract plugin names and their download URLs
27+
while ((match = pluginRegex.exec(pluginsListContent)) !== null) {
1628
const pluginName = match[1].trim();
1729
const badgeUrl = match[2].trim();
1830
const downloadUrl = match[3].trim();
@@ -29,27 +41,31 @@ parsedBadges.forEach(({ name, repo, latestReleaseUrl }) => {
2941

3042
// Construct expected badge URL
3143
const expectedBadgeUrl = `https://img.shields.io/github/downloads/${repo}/total`;
32-
44+
3345
// Check if the download URL needs updating
3446
if (downloadUrl !== latestReleaseUrl) {
3547
console.log(`Updating download URL for ${name}`);
36-
readmeContent.replace(downloadUrl, latestReleaseUrl);
48+
pluginsListContent = pluginsListContent.replace(downloadUrl, latestReleaseUrl);
3749
changesMade = true;
3850
}
3951

4052
// Check if the badge URL needs updating
4153
if (badgeUrl !== expectedBadgeUrl) {
4254
console.log(`Updating badge URL for ${name}`);
43-
readmeContent.replace(badgeUrl, expectedBadgeUrl);
55+
pluginsListContent = pluginsListContent.replace(badgeUrl, expectedBadgeUrl);
4456
changesMade = true;
4557
}
4658
} else {
4759
console.log(`No matching plugin found in README for ${name}`);
4860
}
4961
});
5062

51-
// Write updated README if changes were made
63+
// Write updated plugins list back into the README if changes were made
5264
if (changesMade) {
65+
readmeContent = readmeContent.replace(
66+
sectionRegex,
67+
`<!-- START PLUGINS LIST -->\n${pluginsListContent}\n<!-- END PLUGINS LIST -->`
68+
);
5369
fs.writeFileSync('README.md', readmeContent, 'utf8');
5470
console.log('README updated successfully.');
5571
process.exit(0); // Indicate success

0 commit comments

Comments
 (0)