|
| 1 | +const fs = require('fs') |
| 2 | +const path = require('path') |
| 3 | + |
| 4 | +/** |
| 5 | + * Extracts version number from module.lua file |
| 6 | + * @param {string} modulePath - Path to module.lua file |
| 7 | + * @returns {string|null} - Version string (e.g., "1.4") or null if not found |
| 8 | + */ |
| 9 | +function extractModuleVersion(modulePath) { |
| 10 | + try { |
| 11 | + const content = fs.readFileSync(modulePath, 'utf8') |
| 12 | + const match = content.match(/MODULE\.version\s*=\s*([0-9]+(?:\.[0-9]+)?)/) |
| 13 | + if (match) { |
| 14 | + return match[1] |
| 15 | + } |
| 16 | + } catch (error) { |
| 17 | + console.error(`Error reading ${modulePath}:`, error.message) |
| 18 | + } |
| 19 | + return null |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Extracts the latest version from changelog.md file |
| 24 | + * @param {string} changelogPath - Path to changelog.md file |
| 25 | + * @returns {string|null} - Version string (e.g., "1.4") or null if not found |
| 26 | + */ |
| 27 | +function extractChangelogVersion(changelogPath) { |
| 28 | + try { |
| 29 | + const content = fs.readFileSync(changelogPath, 'utf8') |
| 30 | + // Match the first version header (which should be the latest) |
| 31 | + const match = content.match(/###\s+Version\s+([0-9]+(?:\.[0-9]+)?)/i) |
| 32 | + if (match) { |
| 33 | + return match[1] |
| 34 | + } |
| 35 | + } catch (error) { |
| 36 | + // Changelog might not exist, that's okay |
| 37 | + return null |
| 38 | + } |
| 39 | + return null |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Updates the version in module.lua to match the changelog version |
| 44 | + * @param {string} modulePath - Path to module.lua file |
| 45 | + * @param {string} newVersion - New version string to set |
| 46 | + * @returns {boolean} - True if update was successful |
| 47 | + */ |
| 48 | +function updateModuleVersion(modulePath, newVersion) { |
| 49 | + try { |
| 50 | + let content = fs.readFileSync(modulePath, 'utf8') |
| 51 | + // Replace the version line, handling both integer and float versions |
| 52 | + content = content.replace( |
| 53 | + /(MODULE\.version\s*=\s*)([0-9]+(?:\.[0-9]+)?)/, |
| 54 | + `$1${newVersion}` |
| 55 | + ) |
| 56 | + fs.writeFileSync(modulePath, content, 'utf8') |
| 57 | + return true |
| 58 | + } catch (error) { |
| 59 | + console.error(`Error updating ${modulePath}:`, error.message) |
| 60 | + return false |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Main function to sync all module versions with their changelogs |
| 66 | + * @param {boolean} dryRun - If true, only report mismatches without updating files |
| 67 | + * @returns {boolean} - Returns false if there are mismatches (for CI validation) |
| 68 | + */ |
| 69 | +function syncVersions(dryRun = false) { |
| 70 | + const rootDir = __dirname |
| 71 | + const entries = fs.readdirSync(rootDir, { withFileTypes: true }) |
| 72 | + |
| 73 | + let totalModules = 0 |
| 74 | + let syncedModules = 0 |
| 75 | + let mismatchedModules = [] |
| 76 | + let missingChangelogs = [] |
| 77 | + |
| 78 | + for (const entry of entries) { |
| 79 | + if (!entry.isDirectory()) continue |
| 80 | + if (entry.name === '.git' || entry.name === 'node_modules' || entry.name === 'documentation') continue |
| 81 | + |
| 82 | + const moduleDir = path.join(rootDir, entry.name) |
| 83 | + const modulePath = path.join(moduleDir, 'module.lua') |
| 84 | + const changelogPath = path.join(moduleDir, 'docs', 'changelog.md') |
| 85 | + |
| 86 | + // Check if module.lua exists |
| 87 | + if (!fs.existsSync(modulePath)) continue |
| 88 | + |
| 89 | + totalModules++ |
| 90 | + |
| 91 | + const moduleVersion = extractModuleVersion(modulePath) |
| 92 | + const changelogVersion = extractChangelogVersion(changelogPath) |
| 93 | + |
| 94 | + if (!changelogVersion) { |
| 95 | + missingChangelogs.push(entry.name) |
| 96 | + continue |
| 97 | + } |
| 98 | + |
| 99 | + if (!moduleVersion) { |
| 100 | + console.warn(`⚠️ ${entry.name}: Could not extract version from module.lua`) |
| 101 | + continue |
| 102 | + } |
| 103 | + |
| 104 | + // Compare versions (as strings first, then as numbers if needed) |
| 105 | + if (moduleVersion !== changelogVersion) { |
| 106 | + // Try to compare as numbers to handle cases like "1.4" vs "1.40" |
| 107 | + const moduleVersionNum = parseFloat(moduleVersion) |
| 108 | + const changelogVersionNum = parseFloat(changelogVersion) |
| 109 | + |
| 110 | + if (moduleVersionNum !== changelogVersionNum) { |
| 111 | + console.log(`🔄 ${entry.name}: Version mismatch (module: ${moduleVersion}, changelog: ${changelogVersion})`) |
| 112 | + mismatchedModules.push({ |
| 113 | + name: entry.name, |
| 114 | + moduleVersion, |
| 115 | + changelogVersion |
| 116 | + }) |
| 117 | + |
| 118 | + if (dryRun) { |
| 119 | + console.log(` ⚠️ Would update module.lua to version ${changelogVersion} (dry run)`) |
| 120 | + } else { |
| 121 | + // Update module version to match changelog |
| 122 | + if (updateModuleVersion(modulePath, changelogVersion)) { |
| 123 | + console.log(` ✅ Updated module.lua to version ${changelogVersion}`) |
| 124 | + syncedModules++ |
| 125 | + } else { |
| 126 | + console.error(` ❌ Failed to update module.lua`) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } else { |
| 131 | + // Versions match, no action needed |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Summary |
| 136 | + console.log('\n' + '='.repeat(60)) |
| 137 | + console.log('Version Sync Summary') |
| 138 | + console.log('='.repeat(60)) |
| 139 | + console.log(`Total modules checked: ${totalModules}`) |
| 140 | + console.log(`Modules synced: ${syncedModules}`) |
| 141 | + console.log(`Modules already in sync: ${totalModules - syncedModules - mismatchedModules.length}`) |
| 142 | + |
| 143 | + if (missingChangelogs.length > 0) { |
| 144 | + console.log(`\n⚠️ Modules without changelog: ${missingChangelogs.length}`) |
| 145 | + missingChangelogs.forEach(name => console.log(` - ${name}`)) |
| 146 | + } |
| 147 | + |
| 148 | + if (mismatchedModules.length === 0 && missingChangelogs.length === 0) { |
| 149 | + console.log('\n✅ All module versions are in sync with their changelogs!') |
| 150 | + return true |
| 151 | + } else if (dryRun) { |
| 152 | + console.log(`\n❌ Found ${mismatchedModules.length} module(s) with version mismatches`) |
| 153 | + return false |
| 154 | + } else if (syncedModules > 0) { |
| 155 | + console.log(`\n✅ Successfully synced ${syncedModules} module(s)`) |
| 156 | + return true |
| 157 | + } |
| 158 | + |
| 159 | + return false |
| 160 | +} |
| 161 | + |
| 162 | +// Check command line arguments |
| 163 | +const args = process.argv.slice(2) |
| 164 | +const dryRun = args.includes('--dry-run') || args.includes('--validate') |
| 165 | + |
| 166 | +// Run the sync |
| 167 | +const success = syncVersions(dryRun) |
| 168 | +process.exit(success ? 0 : 1) |
| 169 | + |
0 commit comments