Skip to content

Commit 83008cf

Browse files
authored
Update update-readme.js
1 parent c1acf8b commit 83008cf

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

update-readme.js

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,58 +15,54 @@ if (!sectionMatch) {
1515
const pluginsListContent = sectionMatch[1].trim();
1616

1717
// Regex to match individual plugin entries
18-
const pluginRegex = /- ### (.+?)\s*!GitHub Downloads all releases(https:\/\/img\.shields\.io\/github\/downloads\/.+?\/total)(.+?)/g;
19-
const readmePlugins = new Map();
18+
const pluginRegex = /- ### (.+?)/g;
19+
const pluginNames = [];
2020
let match;
2121

22-
// Extract plugin names and URLs from README
2322
while ((match = pluginRegex.exec(pluginsListContent)) !== null) {
24-
const pluginName = match[1].trim();
25-
const currentBadgeUrl = match[2].trim();
26-
const currentDownloadUrl = match[3].trim();
27-
readmePlugins.set(pluginName.toLowerCase(), { pluginName, currentBadgeUrl, currentDownloadUrl });
23+
pluginNames.push(match[1].trim());
2824
}
2925

3026
// Log parsed plugin names from README
31-
console.log('Parsed plugin names from README:', Array.from(readmePlugins.keys()));
27+
console.log('Parsed plugin names from README:', pluginNames);
3228

33-
if (readmePlugins.size === 0) {
29+
if (pluginNames.length === 0) {
3430
console.error('No plugins were found. Please check the README format.');
3531
process.exit(1);
3632
}
3733

3834
// Load parsed badges
3935
const parsedBadges = JSON.parse(fs.readFileSync(process.argv[2], 'utf8')).plugins;
4036

37+
// Map plugin names from the README for easier matching
38+
const readmePlugins = new Map(pluginNames.map(name => [name.toLowerCase(), name]));
39+
4140
// Flag to track changes
4241
let changesMade = false;
4342

4443
// Iterate over parsed badges to find matching plugins in the README
4544
parsedBadges.forEach(({ name, latestReleaseUrl }) => {
4645
const normalizedPluginName = name.toLowerCase();
47-
const pluginEntry = readmePlugins.get(normalizedPluginName);
48-
49-
if (pluginEntry) {
50-
console.log(`\nChecking plugin: ${name}`);
51-
console.log(`Current download URL in README: ${pluginEntry.currentDownloadUrl}`);
52-
console.log(`Latest release URL from badges.json: ${latestReleaseUrl}`);
46+
console.log(`Checking plugin: ${name}`);
47+
console.log(`Latest release URL: ${latestReleaseUrl}`);
5348

54-
// Check if the download URL needs updating
55-
if (pluginEntry.currentDownloadUrl !== latestReleaseUrl) {
56-
console.log(`Updating download URL for ${name}`);
49+
if (readmePlugins.has(normalizedPluginName)) {
50+
console.log(`Matching plugin found in README for ${name}`);
5751

58-
const expectedBadgeUrl = `https://img.shields.io/github/downloads/${name}/total`;
59-
const newBadge = `[![GitHub Downloads (all releases)](${expectedBadgeUrl})](${latestReleaseUrl})`;
52+
// Construct the expected badge URL
53+
const expectedBadgeUrl = `https://img.shields.io/github/downloads/${name}/total`;
6054

61-
// Construct the old and new plugin entries
62-
const oldEntry = `- ### [${pluginEntry.pluginName}] [![GitHub Downloads (all releases)](${pluginEntry.currentBadgeUrl})](${pluginEntry.currentDownloadUrl})`;
63-
const newEntry = `- ### [${pluginEntry.pluginName}] ${newBadge}`;
55+
// Regex to find and replace badge and download URL
56+
const badgeRegex = new RegExp(
57+
`\!\GitHub Downloads \all releases\\\https://img\\.shields\\.io/github/downloads/.+?/total\\\.*?\`,
58+
'g'
59+
);
60+
const newBadge = `[![GitHub Downloads (all releases)](${expectedBadgeUrl})](${latestReleaseUrl})`;
61+
const updatedContent = readmeContent.replace(badgeRegex, newBadge);
6462

65-
// Replace the old entry with the new one in the README
66-
readmeContent = readmeContent.replace(oldEntry, newEntry);
63+
if (updatedContent !== readmeContent) {
64+
readmeContent = updatedContent;
6765
changesMade = true;
68-
} else {
69-
console.log(`No update needed for ${name}`);
7066
}
7167
} else {
7268
console.log(`No matching plugin found in README for ${name}`);

0 commit comments

Comments
 (0)