Skip to content

Commit 5c7d6f7

Browse files
authored
Update update-readme.js
1 parent 604ae58 commit 5c7d6f7

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

update-readme.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
const fs = require('fs');
22

3+
const parsedBadgesPath = process.argv[2];
4+
const readmePath = 'README.md';
5+
6+
if (!fs.existsSync(parsedBadgesPath)) {
7+
console.error(`Error: Parsed badges file not found at ${parsedBadgesPath}`);
8+
process.exit(1);
9+
}
10+
311
// Load parsed badges and README content
4-
const parsedBadges = JSON.parse(fs.readFileSync('parsed-badges.json', 'utf8'));
5-
let readmeContent = fs.readFileSync('README.md', 'utf8');
12+
const parsedBadges = JSON.parse(fs.readFileSync(parsedBadgesPath, 'utf8'));
13+
let readmeContent = fs.readFileSync(readmePath, 'utf8');
14+
15+
let changesMade = false;
616

7-
// Define regex to match each plugin's download badge in README
8-
parsedBadges.forEach(plugin => {
9-
const downloadBadgeRegex = new RegExp(
10-
`\\\!\GitHub Downloads \all releases\\\https:\\/\\/img\\.shields\\.io\\/github\\/downloads\\/${plugin.repo}\\/total\\\https:\\/\\/github\\.com\\/${plugin.repo}\\/releases\\/download.*?\`,
17+
parsedBadges.forEach((plugin) => {
18+
const badgeRegex = new RegExp(
19+
`\\\!\GitHub Downloads \all releases\\\https:\\/\\/img\\.shields\\.io\\/github\\/downloads\\/${plugin.repo}\\/total\\\https:\\/\\/github\\.com\\/${plugin.repo}\\/releases\\/download\\/.*\`,
1120
'g'
1221
);
1322

14-
const newDownloadBadge = `[![GitHub Downloads (all releases)](https://img.shields.io/github/downloads/${plugin.repo}/total)](${plugin.latestReleaseUrl})`;
23+
const newBadgeLine = `[![GitHub Downloads (all releases)](https://img.shields.io/github/downloads/${plugin.repo}/total)](${plugin.latestReleaseUrl})`;
1524

16-
// Replace the old badge with the new one if it differs
17-
readmeContent = readmeContent.replace(downloadBadgeRegex, newDownloadBadge);
25+
if (badgeRegex.test(readmeContent)) {
26+
readmeContent = readmeContent.replace(badgeRegex, newBadgeLine);
27+
changesMade = true;
28+
}
1829
});
1930

20-
// Check if changes were made
21-
if (readmeContent !== fs.readFileSync('README.md', 'utf8')) {
22-
fs.writeFileSync('README.md', readmeContent);
23-
console.log('README updated with the latest download URLs.');
31+
if (changesMade) {
32+
fs.writeFileSync(readmePath, readmeContent);
33+
console.log('README updated with latest download URLs.');
2434
process.exit(0); // Indicate changes were made
2535
} else {
2636
console.log('No changes made to README.');

0 commit comments

Comments
 (0)