Skip to content

Commit 6384066

Browse files
authored
Update update-readme.js
1 parent 89e11fd commit 6384066

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

update-readme.js

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
const fs = require('fs');
22

33
// Load README
4-
const readmeContent = fs.readFileSync('README.md', 'utf8');
4+
let readmeContent = fs.readFileSync('README.md', 'utf8');
55

66
// Regex to match the plugins list section in the README
77
const sectionRegex = /<!-- START PLUGINS LIST -->([\s\S]*?)<!-- END PLUGINS LIST -->/;
88
const sectionMatch = sectionRegex.exec(readmeContent);
9-
109
if (!sectionMatch) {
1110
console.error('Error: Could not find the plugins list section in the README.');
1211
process.exit(1);
@@ -17,16 +16,61 @@ const pluginsListContent = sectionMatch[1].trim();
1716
// Regex to match individual plugin entries
1817
const pluginRegex = /- ### \[(.+?)\]/g;
1918
const pluginNames = [];
20-
2119
let match;
2220
while ((match = pluginRegex.exec(pluginsListContent)) !== null) {
2321
pluginNames.push(match[1].trim());
2422
}
2523

26-
// Log parsed plugin names
24+
// Log parsed plugin names from README
2725
console.log('Parsed plugin names from README:', pluginNames);
28-
2926
if (pluginNames.length === 0) {
3027
console.error('No plugins were found. Please check the README format.');
3128
process.exit(1);
3229
}
30+
31+
// Load parsed badges
32+
const parsedBadges = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
33+
34+
// Map plugin names from the README for easier matching
35+
const readmePlugins = new Map(pluginNames.map(name => [name.toLowerCase(), name]));
36+
37+
// Flag to track changes
38+
let changesMade = false;
39+
40+
parsedBadges.forEach(({ name, repo, latestReleaseUrl }) => {
41+
const normalizedPluginName = name.toLowerCase();
42+
console.log(`Checking plugin: ${name}`);
43+
console.log(`Latest release URL: ${latestReleaseUrl}`);
44+
45+
if (readmePlugins.has(normalizedPluginName)) {
46+
console.log(`Matching plugin found in README for ${name}`);
47+
48+
// Construct the expected badge URL
49+
const expectedBadgeUrl = `https://img.shields.io/github/downloads/${repo}/total`;
50+
51+
// Regex to find and replace badge and download URL
52+
const badgeRegex = new RegExp(
53+
`\\[!\\[GitHub Downloads \\(all releases\\)\\]\\(https://img\\.shields\\.io/github/downloads/${repo}/total\\)\\]\\(.*?\\)`,
54+
'g'
55+
);
56+
const newBadge = `[![GitHub Downloads (all releases)](${expectedBadgeUrl})](${latestReleaseUrl})`;
57+
58+
const updatedContent = readmeContent.replace(badgeRegex, newBadge);
59+
60+
if (updatedContent !== readmeContent) {
61+
readmeContent = updatedContent;
62+
changesMade = true;
63+
}
64+
} else {
65+
console.log(`No matching plugin found in README for ${name}`);
66+
}
67+
});
68+
69+
// Write updated README if changes were made
70+
if (changesMade) {
71+
console.log('Changes detected, updating README...');
72+
fs.writeFileSync('README.md', readmeContent, 'utf8');
73+
console.log('README updated successfully.');
74+
} else {
75+
console.log('No changes made to README.');
76+
}

0 commit comments

Comments
 (0)