|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import { extractTodosFromDir } from '../src/parser/extractTodosFromDir'; |
| 4 | +import { classifyTodoText } from '../src/core/classifier'; |
| 5 | +import { TodoItem } from '../src/parser/types'; |
| 6 | + |
| 7 | +function formatGroupHeader(tag: string, semantic: string, metadataKey?: string, metadataValue?: string): string { |
| 8 | + const parts = [tag]; |
| 9 | + if (semantic) parts.push(semantic); |
| 10 | + if (metadataKey && metadataValue) parts.push(`${metadataKey}:${metadataValue}`); |
| 11 | + return `## ${parts.join(' · ')}`; |
| 12 | +} |
| 13 | + |
| 14 | +function generateChangelogContent(todos: TodoItem[]): string { |
| 15 | + type GroupKey = string; |
| 16 | + const groups: Record<GroupKey, TodoItem[]> = {}; |
| 17 | + |
| 18 | + for (const todo of todos) { |
| 19 | + const semantics = classifyTodoText(todo.text); |
| 20 | + const metadataEntries = Object.entries(todo.metadata || {}) || [['', '']]; |
| 21 | + const tag = todo.tag.toUpperCase(); |
| 22 | + |
| 23 | + for (const semantic of semantics.length ? semantics : ['']) { |
| 24 | + for (const [metaKey, metaValue] of metadataEntries.length ? metadataEntries : [['', '']]) { |
| 25 | + const key = JSON.stringify({ tag, semantic, metaKey, metaValue }); |
| 26 | + if (!groups[key]) groups[key] = []; |
| 27 | + groups[key].push(todo); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + const output: string[] = ['# 📝 Changelog (from TODOs)', '']; |
| 33 | + |
| 34 | + for (const key of Object.keys(groups)) { |
| 35 | + const { tag, semantic, metaKey, metaValue } = JSON.parse(key); |
| 36 | + output.push(formatGroupHeader(tag, semantic, metaKey, metaValue)); |
| 37 | + |
| 38 | + for (const todo of groups[key]) { |
| 39 | + output.push(`- ${todo.text} (\`${todo.file}:${todo.line}\`)`); |
| 40 | + } |
| 41 | + |
| 42 | + output.push(''); |
| 43 | + } |
| 44 | + |
| 45 | + return output.join('\n'); |
| 46 | +} |
| 47 | + |
| 48 | +async function main() { |
| 49 | + const todos = await extractTodosFromDir('src'); |
| 50 | + const changelog = generateChangelogContent(todos); |
| 51 | + fs.writeFileSync('CHANGELOG.md', changelog, 'utf8'); |
| 52 | + console.log('✅ Changelog saved to CHANGELOG.md'); |
| 53 | +} |
| 54 | + |
| 55 | +main(); |
0 commit comments