|
1 | 1 | const fs = require('fs'); |
2 | 2 |
|
| 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 | + |
3 | 11 | // 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; |
6 | 16 |
|
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\\/.*\`, |
11 | 20 | 'g' |
12 | 21 | ); |
13 | 22 |
|
14 | | - const newDownloadBadge = `[](${plugin.latestReleaseUrl})`; |
| 23 | + const newBadgeLine = `[](${plugin.latestReleaseUrl})`; |
15 | 24 |
|
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 | + } |
18 | 29 | }); |
19 | 30 |
|
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.'); |
24 | 34 | process.exit(0); // Indicate changes were made |
25 | 35 | } else { |
26 | 36 | console.log('No changes made to README.'); |
|
0 commit comments