@@ -2,17 +2,29 @@ const fs = require('fs');
22
33// Load parsed badges and README
44const 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 = / < ! - - S T A R T P L U G I N S L I S T - - > \n ( [ \s \S ] * ?) \n < ! - - E N D P L U G I N S L I S T - - > / ;
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
820const pluginRegex = / - # # # ( .* ?) .* ? \s * < b r > \n \n \s * ! G i t H u b D o w n l o a d s a l l r e l e a s e s ( .* ?) ( .* ?) / g;
921
10- // Map to hold parsed plugin entries from the README
22+ // Map to hold parsed plugin entries from the extracted section
1123const readmePlugins = new Map ( ) ;
1224let 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
5264if ( 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