|
| 1 | +#!/usr/bin/env node |
| 2 | +// kilocode_change - new file: Build-time script to parse CHANGELOG.md and generate releases.json |
| 3 | + |
| 4 | +import fs from 'fs' |
| 5 | +import path from 'path' |
| 6 | +import process from 'process' |
| 7 | +import { fileURLToPath } from 'url' |
| 8 | + |
| 9 | +const __filename = fileURLToPath(import.meta.url) |
| 10 | +const __dirname = path.dirname(__filename) |
| 11 | + |
| 12 | +console.log('🚀 Starting changelog parsing...') |
| 13 | + |
| 14 | +// Paths |
| 15 | +const changelogPath = path.resolve(__dirname, '../../CHANGELOG.md') |
| 16 | +const outputDir = path.resolve(__dirname, '../src/generated/releases') |
| 17 | + |
| 18 | +console.log('📖 Reading changelog from:', changelogPath) |
| 19 | +console.log('📁 Output directory:', outputDir) |
| 20 | + |
| 21 | +// Check if changelog exists |
| 22 | +if (!fs.existsSync(changelogPath)) { |
| 23 | + console.error('❌ Changelog not found at:', changelogPath) |
| 24 | + process.exit(1) |
| 25 | +} |
| 26 | + |
| 27 | +// Read changelog |
| 28 | +const changelogContent = fs.readFileSync(changelogPath, 'utf-8') |
| 29 | +console.log('📄 Changelog loaded, length:', changelogContent.length, 'characters') |
| 30 | + |
| 31 | +// Parse releases using split-based approach for better content extraction |
| 32 | +const releases = [] |
| 33 | + |
| 34 | +// Split changelog into sections by version headers |
| 35 | +const versionSections = changelogContent.split(/^## \[v(\d+\.\d+\.\d+)\]/gm) |
| 36 | + |
| 37 | +// Process each version section (skip first empty element) |
| 38 | +for (let i = 1; i < versionSections.length; i += 2) { |
| 39 | + const version = versionSections[i] |
| 40 | + const sectionContent = versionSections[i + 1] || '' |
| 41 | + |
| 42 | + const changes = extractChangesFromSection(sectionContent) |
| 43 | + |
| 44 | + if (changes.length > 0) { |
| 45 | + const release = { |
| 46 | + version, |
| 47 | + changes |
| 48 | + } |
| 49 | + |
| 50 | + releases.push(release) |
| 51 | + console.log(`📝 Parsed release v${version} with ${changes.length} changes`) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function extractChangesFromSection(sectionContent) { |
| 56 | + const lines = sectionContent.split('\n') |
| 57 | + const changes = [] |
| 58 | + let currentChange = null |
| 59 | + let collectingMultilineDescription = false |
| 60 | + |
| 61 | + for (const line of lines) { |
| 62 | + const trimmedLine = line.trim() |
| 63 | + |
| 64 | + // Skip empty lines and section headers |
| 65 | + if (!trimmedLine || trimmedLine.startsWith('###')) { |
| 66 | + // Stop collecting multiline content when we hit a section break |
| 67 | + if (trimmedLine.startsWith('###')) { |
| 68 | + collectingMultilineDescription = false |
| 69 | + } |
| 70 | + continue |
| 71 | + } |
| 72 | + |
| 73 | + // Check if this is a new change entry |
| 74 | + if (trimmedLine.startsWith('- [#')) { |
| 75 | + // Save previous change if exists |
| 76 | + if (currentChange) { |
| 77 | + changes.push(currentChange) |
| 78 | + } |
| 79 | + |
| 80 | + // Create new change entry |
| 81 | + currentChange = parseChangeEntry(trimmedLine) |
| 82 | + collectingMultilineDescription = true |
| 83 | + } else if (collectingMultilineDescription && currentChange) { |
| 84 | + // Check if this is indented content (part of the multiline description) |
| 85 | + if (line.startsWith(' ') || line.startsWith('\t')) { |
| 86 | + // This is indented content, part of the description |
| 87 | + currentChange.description += ' ' + trimmedLine |
| 88 | + } else if (trimmedLine.startsWith('- ') && !trimmedLine.startsWith('- [#')) { |
| 89 | + // This is a bullet point in the description, not a new change |
| 90 | + currentChange.description += ' ' + trimmedLine.replace(/^- /, '') |
| 91 | + } else { |
| 92 | + // Any other content that might be part of the description |
| 93 | + currentChange.description += ' ' + trimmedLine |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Don't forget the last change |
| 99 | + if (currentChange) { |
| 100 | + changes.push(currentChange) |
| 101 | + } |
| 102 | + |
| 103 | + return changes |
| 104 | +} |
| 105 | + |
| 106 | +function parseChangeEntry(line) { |
| 107 | + const item = { |
| 108 | + description: line.replace(/^- /, '').trim(), |
| 109 | + category: 'other' |
| 110 | + } |
| 111 | + |
| 112 | + // Extract PR number |
| 113 | + const prMatch = line.match(/\[#(\d+)\]/) |
| 114 | + if (prMatch) { |
| 115 | + item.prNumber = parseInt(prMatch[1]) |
| 116 | + } |
| 117 | + |
| 118 | + // Extract commit hash |
| 119 | + const commitMatch = line.match(/\[`([a-f0-9]+)`\]/) |
| 120 | + if (commitMatch) { |
| 121 | + item.commitHash = commitMatch[1] |
| 122 | + } |
| 123 | + |
| 124 | + // Extract author - updated regex to handle hyphens and other valid GitHub username characters |
| 125 | + const authorMatch = line.match(/Thanks \[@([\w-]+)\]/) |
| 126 | + if (authorMatch) { |
| 127 | + item.author = authorMatch[1] |
| 128 | + } |
| 129 | + |
| 130 | + // Extract description after "! - " |
| 131 | + const descMatch = line.match(/! - (.+)$/) |
| 132 | + if (descMatch) { |
| 133 | + item.description = descMatch[1].trim() |
| 134 | + } |
| 135 | + |
| 136 | + return item |
| 137 | +} |
| 138 | + |
| 139 | +console.log(`✅ Found ${releases.length} releases`) |
| 140 | + |
| 141 | +// Limit to the last 10 releases to keep build size manageable |
| 142 | +const MAX_RELEASES = 10 |
| 143 | +const limitedReleases = releases.slice(0, MAX_RELEASES) |
| 144 | +console.log(`🔢 Limiting to ${limitedReleases.length} most recent releases (from ${releases.length} total)`) |
| 145 | + |
| 146 | +// Add improved categorization based on patterns and content keywords |
| 147 | +limitedReleases.forEach(release => { |
| 148 | + release.changes.forEach(change => { |
| 149 | + change.category = categorizeChange(change.description) |
| 150 | + }) |
| 151 | +}) |
| 152 | + |
| 153 | +// kilocode_change start: Simplified categorization - just 3 categories |
| 154 | +function categorizeChange(description) { |
| 155 | + const desc = description.toLowerCase().trim() |
| 156 | + |
| 157 | + // Simple keyword-based categorization |
| 158 | + if (desc.includes('fix') || desc.includes('bug') || desc.includes('correct') || desc.includes('resolve')) { |
| 159 | + return 'fix' |
| 160 | + } |
| 161 | + |
| 162 | + if (desc.includes('add') || desc.includes('new') || desc.includes('introduce')) { |
| 163 | + return 'feature' |
| 164 | + } |
| 165 | + |
| 166 | + // Everything else goes to "other" |
| 167 | + return 'other' |
| 168 | +} |
| 169 | +// kilocode_change end |
| 170 | + |
| 171 | +// Create output directory |
| 172 | +if (!fs.existsSync(outputDir)) { |
| 173 | + fs.mkdirSync(outputDir, { recursive: true }) |
| 174 | + console.log('📁 Created output directory') |
| 175 | +} |
| 176 | + |
| 177 | +// Generate single releases.json file with current version and all releases |
| 178 | +const releaseData = { |
| 179 | + currentVersion: limitedReleases[0]?.version || "0.0.0", |
| 180 | + releases: limitedReleases |
| 181 | +} |
| 182 | + |
| 183 | +const releasesPath = path.join(outputDir, 'releases.json') |
| 184 | +fs.writeFileSync(releasesPath, JSON.stringify(releaseData, null, 2)) |
| 185 | + |
| 186 | +console.log(`💾 Generated releases.json with ${limitedReleases.length} releases`) |
| 187 | +console.log(`📋 Current version: ${releaseData.currentVersion}`) |
| 188 | +console.log('🎉 Changelog parsing completed successfully!') |
0 commit comments