|
| 1 | +import { readFileSync } from 'fs'; |
| 2 | +import { join } from 'path'; |
| 3 | +import { getNewGitCommits } from './get-commit-list'; |
| 4 | + |
| 5 | +type EntryType = 'important' | 'other' | 'internal'; |
| 6 | + |
| 7 | +interface ChangelogEntry { |
| 8 | + type: EntryType; |
| 9 | + content: string; |
| 10 | + sortKey: string; |
| 11 | + prNumber: string | null; |
| 12 | +} |
| 13 | + |
| 14 | +// ============================================================================ |
| 15 | +// Changelog Parsing |
| 16 | +// ============================================================================ |
| 17 | + |
| 18 | +interface ParsedChangelog { |
| 19 | + importantChanges: ChangelogEntry[]; |
| 20 | + otherChanges: ChangelogEntry[]; |
| 21 | + internalChanges: ChangelogEntry[]; |
| 22 | + changelogPRs: Set<string>; |
| 23 | + contributorsLine: string; |
| 24 | +} |
| 25 | + |
| 26 | +function getUnreleasedSection(content: string): string[] { |
| 27 | + const lines = content.split('\n'); |
| 28 | + |
| 29 | + const unreleasedIndex = lines.findIndex(line => line.trim() === '## Unreleased'); |
| 30 | + if (unreleasedIndex === -1) { |
| 31 | + // eslint-disable-next-line no-console |
| 32 | + console.error('Could not find "## Unreleased" section in CHANGELOG.md'); |
| 33 | + process.exit(1); |
| 34 | + } |
| 35 | + |
| 36 | + const nextVersionIndex = lines.findIndex((line, index) => index > unreleasedIndex && /^## \d+\.\d+\.\d+/.test(line)); |
| 37 | + if (nextVersionIndex === -1) { |
| 38 | + // eslint-disable-next-line no-console |
| 39 | + console.error('Could not find next version section after "## Unreleased"'); |
| 40 | + process.exit(1); |
| 41 | + } |
| 42 | + |
| 43 | + return lines.slice(unreleasedIndex + 1, nextVersionIndex); |
| 44 | +} |
| 45 | + |
| 46 | +function createEntry(content: string, type: EntryType): ChangelogEntry { |
| 47 | + const firstLine = content.split('\n')[0] ?? content; |
| 48 | + const prNumber = extractPRNumber(firstLine); |
| 49 | + return { |
| 50 | + type, |
| 51 | + content, |
| 52 | + sortKey: extractSortKey(firstLine), |
| 53 | + prNumber, |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +function parseChangelog(unreleasedLines: string[]): ParsedChangelog { |
| 58 | + const importantChanges: ChangelogEntry[] = []; |
| 59 | + const otherChanges: ChangelogEntry[] = []; |
| 60 | + const internalChanges: ChangelogEntry[] = []; |
| 61 | + const changelogPRs = new Set<string>(); |
| 62 | + let contributorsLine = ''; |
| 63 | + |
| 64 | + let currentEntry: string[] = []; |
| 65 | + let currentType: EntryType | null = null; |
| 66 | + let inDetailsBlock = false; |
| 67 | + let detailsContent: string[] = []; |
| 68 | + |
| 69 | + const addEntry = (entry: ChangelogEntry): void => { |
| 70 | + if (entry.prNumber) { |
| 71 | + changelogPRs.add(entry.prNumber); |
| 72 | + } |
| 73 | + |
| 74 | + if (entry.type === 'important') { |
| 75 | + importantChanges.push(entry); |
| 76 | + } else if (entry.type === 'internal') { |
| 77 | + internalChanges.push(entry); |
| 78 | + } else { |
| 79 | + otherChanges.push(entry); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + const flushCurrentEntry = (): void => { |
| 84 | + if (currentEntry.length === 0 || !currentType) return; |
| 85 | + |
| 86 | + // Remove trailing empty lines from the entry |
| 87 | + while (currentEntry.length > 0 && !currentEntry[currentEntry.length - 1]?.trim()) { |
| 88 | + currentEntry.pop(); |
| 89 | + } |
| 90 | + |
| 91 | + if (currentEntry.length === 0) return; |
| 92 | + |
| 93 | + const entry = createEntry(currentEntry.join('\n'), currentType); |
| 94 | + addEntry(entry); |
| 95 | + |
| 96 | + currentEntry = []; |
| 97 | + currentType = null; |
| 98 | + }; |
| 99 | + |
| 100 | + const processDetailsContent = (): void => { |
| 101 | + for (const line of detailsContent) { |
| 102 | + const trimmed = line.trim(); |
| 103 | + if (trimmed.startsWith('-') && trimmed.includes('(#')) { |
| 104 | + const entry = createEntry(trimmed, 'internal'); |
| 105 | + addEntry(entry); |
| 106 | + } |
| 107 | + } |
| 108 | + detailsContent = []; |
| 109 | + }; |
| 110 | + |
| 111 | + for (const line of unreleasedLines) { |
| 112 | + // Skip undefined/null lines |
| 113 | + if (line == null) continue; |
| 114 | + |
| 115 | + // Skip empty lines at the start of an entry |
| 116 | + if (!line.trim() && currentEntry.length === 0) continue; |
| 117 | + |
| 118 | + // Skip quote lines |
| 119 | + if (isQuoteLine(line)) continue; |
| 120 | + |
| 121 | + // Capture contributors line |
| 122 | + if (isContributorsLine(line)) { |
| 123 | + contributorsLine = line; |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + // Skip section headings |
| 128 | + if (isSectionHeading(line)) { |
| 129 | + flushCurrentEntry(); |
| 130 | + continue; |
| 131 | + } |
| 132 | + |
| 133 | + // Handle details block |
| 134 | + if (line.includes('<details>')) { |
| 135 | + inDetailsBlock = true; |
| 136 | + detailsContent = []; |
| 137 | + continue; |
| 138 | + } |
| 139 | + |
| 140 | + if (line.includes('</details>')) { |
| 141 | + inDetailsBlock = false; |
| 142 | + processDetailsContent(); |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + if (inDetailsBlock) { |
| 147 | + if (!line.includes('<summary>')) { |
| 148 | + detailsContent.push(line); |
| 149 | + } |
| 150 | + continue; |
| 151 | + } |
| 152 | + |
| 153 | + // Handle regular entries |
| 154 | + if (line.trim().startsWith('- ')) { |
| 155 | + flushCurrentEntry(); |
| 156 | + currentEntry = [line]; |
| 157 | + currentType = determineEntryType(line); |
| 158 | + } else if (currentEntry.length > 0) { |
| 159 | + currentEntry.push(line); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + flushCurrentEntry(); |
| 164 | + |
| 165 | + return { importantChanges, otherChanges, internalChanges, changelogPRs, contributorsLine }; |
| 166 | +} |
| 167 | + |
| 168 | +// ============================================================================ |
| 169 | +// Output Generation |
| 170 | +// ============================================================================ |
| 171 | + |
| 172 | +export function sortEntries(entries: ChangelogEntry[]): void { |
| 173 | + entries.sort((a, b) => a.sortKey.localeCompare(b.sortKey)); |
| 174 | +} |
| 175 | + |
| 176 | +function generateOutput( |
| 177 | + importantChanges: ChangelogEntry[], |
| 178 | + otherChanges: ChangelogEntry[], |
| 179 | + internalChanges: ChangelogEntry[], |
| 180 | + contributorsLine: string, |
| 181 | +): string { |
| 182 | + const output: string[] = []; |
| 183 | + |
| 184 | + if (importantChanges.length > 0) { |
| 185 | + output.push('### Important Changes', ''); |
| 186 | + for (const entry of importantChanges) { |
| 187 | + output.push(entry.content, ''); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + if (otherChanges.length > 0) { |
| 192 | + output.push('### Other Changes', ''); |
| 193 | + for (const entry of otherChanges) { |
| 194 | + output.push(entry.content); |
| 195 | + } |
| 196 | + output.push(''); |
| 197 | + } |
| 198 | + |
| 199 | + if (internalChanges.length > 0) { |
| 200 | + output.push('<details>', ' <summary><strong>Internal Changes</strong></summary>', ''); |
| 201 | + for (const entry of internalChanges) { |
| 202 | + output.push(entry.content); |
| 203 | + } |
| 204 | + output.push('', '</details>', ''); |
| 205 | + } |
| 206 | + |
| 207 | + if (contributorsLine) { |
| 208 | + output.push(contributorsLine); |
| 209 | + } |
| 210 | + |
| 211 | + return output.join('\n'); |
| 212 | +} |
| 213 | + |
| 214 | +// ============================================================================ |
| 215 | +// Main |
| 216 | +// ============================================================================ |
| 217 | + |
| 218 | +function run(): void { |
| 219 | + const changelogPath = join(__dirname, '..', 'CHANGELOG.md'); |
| 220 | + const changelogContent = readFileSync(changelogPath, 'utf-8'); |
| 221 | + const unreleasedLines = getUnreleasedSection(changelogContent); |
| 222 | + |
| 223 | + // Parse existing changelog entries |
| 224 | + const { importantChanges, otherChanges, internalChanges, changelogPRs, contributorsLine } = |
| 225 | + parseChangelog(unreleasedLines); |
| 226 | + |
| 227 | + // Add new git commits that aren't already in the changelog |
| 228 | + for (const commit of getNewGitCommits()) { |
| 229 | + const prNumber = extractPRNumber(commit); |
| 230 | + |
| 231 | + // Skip duplicates |
| 232 | + if (prNumber && changelogPRs.has(prNumber)) { |
| 233 | + continue; |
| 234 | + } |
| 235 | + |
| 236 | + const entry = createEntry(commit, isInternalCommit(commit) ? 'internal' : 'other'); |
| 237 | + |
| 238 | + if (entry.type === 'internal') { |
| 239 | + internalChanges.push(entry); |
| 240 | + } else { |
| 241 | + otherChanges.push(entry); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + // Sort all categories |
| 246 | + sortEntries(importantChanges); |
| 247 | + sortEntries(otherChanges); |
| 248 | + sortEntries(internalChanges); |
| 249 | + |
| 250 | + // eslint-disable-next-line no-console |
| 251 | + console.log(generateOutput(importantChanges, otherChanges, internalChanges, contributorsLine)); |
| 252 | +} |
| 253 | + |
| 254 | +// ============================================================================ |
| 255 | +// Helper Functions |
| 256 | +// ============================================================================ |
| 257 | + |
| 258 | +function extractPRNumber(line: string): string | null { |
| 259 | + const match = line.match(/#(\d+)/); |
| 260 | + return match?.[1] ?? null; |
| 261 | +} |
| 262 | + |
| 263 | +function extractSortKey(line: string): string { |
| 264 | + return line |
| 265 | + .trim() |
| 266 | + .replace(/^- /, '') |
| 267 | + .replace(/\*\*/g, '') |
| 268 | + .replace(/\s*\(\[#\d+\].*?\)\s*$/, '') |
| 269 | + .toLowerCase(); |
| 270 | +} |
| 271 | + |
| 272 | +function isQuoteLine(line: string): boolean { |
| 273 | + return line.includes('—') && (line.includes('Wayne Gretzky') || line.includes('Michael Scott')); |
| 274 | +} |
| 275 | + |
| 276 | +function isContributorsLine(line: string): boolean { |
| 277 | + return line.includes('Work in this release was contributed by'); |
| 278 | +} |
| 279 | + |
| 280 | +function isSectionHeading(line: string): boolean { |
| 281 | + const trimmed = line.trim(); |
| 282 | + return trimmed === '### Important Changes' || trimmed === '### Other Changes'; |
| 283 | +} |
| 284 | + |
| 285 | +function isInternalCommit(line: string): boolean { |
| 286 | + return /^- (chore|ref|test|meta)/.test(line.trim()); |
| 287 | +} |
| 288 | + |
| 289 | +function isImportantEntry(line: string): boolean { |
| 290 | + return line.includes('**feat') || line.includes('**fix'); |
| 291 | +} |
| 292 | + |
| 293 | +function determineEntryType(line: string): EntryType { |
| 294 | + if (isImportantEntry(line)) { |
| 295 | + return 'important'; |
| 296 | + } |
| 297 | + if (isInternalCommit(line)) { |
| 298 | + return 'internal'; |
| 299 | + } |
| 300 | + return 'other'; |
| 301 | +} |
| 302 | + |
| 303 | +run(); |
0 commit comments