|
1 | | -// scripts/generateChangelog.ts |
2 | | - |
3 | 1 | import fs from 'fs'; |
4 | 2 | import path from 'path'; |
5 | | -import { extractTodosFromDir } from '../src/parser/extractTodosFromDir'; |
6 | | -import { labelsFromTodo } from '../src/core/labelManager'; |
7 | | -import { TodoItem } from '../src/parser/types'; |
| 3 | +import { extractTodosFromDir } from './core/extractTodosFromDir'; |
| 4 | +import { classifyTodoText } from './core/classifier'; |
| 5 | +import { TodoItem } from './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 | +} |
8 | 13 |
|
9 | | -// Util: agrupador por chave composta |
10 | | -function groupTodos(todos: TodoItem[]): Record<string, TodoItem[]> { |
11 | | - const groups: Record<string, TodoItem[]> = {}; |
| 14 | +function generateChangelogContent(todos: TodoItem[]): string { |
| 15 | + type GroupKey = string; |
| 16 | + const groups: Record<GroupKey, TodoItem[]> = {}; |
12 | 17 |
|
13 | 18 | for (const todo of todos) { |
14 | | - const semanticLabels = labelsFromTodo(todo); |
15 | | - const meta = todo.metadata ?? {}; |
16 | | - |
17 | | - for (const label of semanticLabels) { |
18 | | - const metaKeys = Object.entries(meta) |
19 | | - .map(([k, v]) => `${k}:${v}`) |
20 | | - .join(', '); |
21 | | - |
22 | | - const key = `[${todo.tag}] + [${label}]${metaKeys ? ` + [${metaKeys}]` : ''}`; |
23 | | - |
24 | | - if (!groups[key]) groups[key] = []; |
25 | | - groups[key].push(todo); |
| 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 | + } |
26 | 29 | } |
27 | 30 | } |
28 | 31 |
|
29 | | - return groups; |
30 | | -} |
31 | | - |
32 | | -// Util: gera string formatada para cada grupo |
33 | | -function formatChangelog(groups: Record<string, TodoItem[]>): string { |
34 | | - const lines: string[] = []; |
| 32 | + const output: string[] = ['# 📝 Changelog (from TODOs)', '']; |
35 | 33 |
|
36 | | - for (const key of Object.keys(groups).sort()) { |
37 | | - lines.push(`## ${key}\n`); |
| 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)); |
38 | 37 |
|
39 | 38 | for (const todo of groups[key]) { |
40 | | - lines.push(`- (${todo.file}:${todo.line}) ${todo.text}`); |
| 39 | + output.push(`- ${todo.text} (\`${todo.file}:${todo.line}\`)`); |
41 | 40 | } |
42 | 41 |
|
43 | | - lines.push(''); |
| 42 | + output.push(''); |
44 | 43 | } |
45 | 44 |
|
46 | | - return lines.join('\n'); |
| 45 | + return output.join('\n'); |
47 | 46 | } |
48 | 47 |
|
49 | 48 | async function main() { |
50 | | - const todos = await extractTodosFromDir('./'); |
51 | | - const grouped = groupTodos(todos); |
52 | | - const changelog = formatChangelog(grouped); |
53 | | - |
54 | | - const filePath = path.resolve('CHANGELOG.md'); |
55 | | - fs.writeFileSync(filePath, changelog, 'utf-8'); |
56 | | - |
57 | | - console.log(`📦 Changelog gerado com ${todos.length} TODOs agrupados → ${filePath}`); |
| 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'); |
58 | 53 | } |
59 | 54 |
|
60 | 55 | main(); |
61 | | - |
0 commit comments