11const 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
77const sectionRegex = / < ! - - S T A R T P L U G I N S L I S T - - > ( [ \s \S ] * ?) < ! - - E N D P L U G I N S L I S T - - > / ;
88const sectionMatch = sectionRegex . exec ( readmeContent ) ;
9-
109if ( ! 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
1817const pluginRegex = / - # # # \[ ( .+ ?) \] / g;
1918const pluginNames = [ ] ;
20-
2119let match ;
2220while ( ( 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
2725console . log ( 'Parsed plugin names from README:' , pluginNames ) ;
28-
2926if ( 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 = `[](${ 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