|
| 1 | +/** |
| 2 | + * Thin Driver_Adapter composition root for tag distribution validation. |
| 3 | + * |
| 4 | + * Wires the Infrastructure `FileSystemAdapter` and `FrontmatterAdapter` |
| 5 | + * into the Application `ValidateTagDistributionUseCase`, invokes it, and |
| 6 | + * prints the frequency table + violations. No business logic lives here — |
| 7 | + * scanning wiki/entities/, wiki/concepts/, wiki/sources/, extracting tags |
| 8 | + * from frontmatter, and computing the 60% frequency threshold is handled |
| 9 | + * entirely by the use case in @wiki/application-tag-validation. |
| 10 | + * |
| 11 | + * Returns the process exit code the caller should use: |
| 12 | + * - 0: All tags pass validation (no tag exceeds 60% threshold) |
| 13 | + * - 1: Validation failed (one or more tags exceed 60% threshold) |
| 14 | + */ |
| 15 | + |
| 16 | +import { FileSystemAdapter } from '@wiki/infrastructure-filesystem'; |
| 17 | +import { FrontmatterAdapter } from '@wiki/infrastructure-frontmatter'; |
| 18 | +import { ValidateTagDistributionUseCase } from '@wiki/application-tag-validation'; |
| 19 | + |
| 20 | +const MAX_TAG_FREQUENCY = 0.6; // 60% |
| 21 | + |
| 22 | +function formatFrequency(count: number, total: number): string { |
| 23 | + if (total === 0) return '0.0%'; |
| 24 | + return `${((count / total) * 100).toFixed(1)}%`; |
| 25 | +} |
| 26 | + |
| 27 | +export async function runValidateTags(workspaceRoot: string): Promise<number> { |
| 28 | + console.log('Tag Distribution Validation'); |
| 29 | + console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); |
| 30 | + console.log(''); |
| 31 | + |
| 32 | + const fsAdapter = new FileSystemAdapter({ |
| 33 | + rootDir: workspaceRoot, |
| 34 | + rawDir: 'raw', |
| 35 | + wikiDir: 'wiki', |
| 36 | + }); |
| 37 | + const frontmatterAdapter = new FrontmatterAdapter(); |
| 38 | + const useCase = new ValidateTagDistributionUseCase(fsAdapter, frontmatterAdapter); |
| 39 | + |
| 40 | + const result = await useCase.execute(); |
| 41 | + const { totalPages, tagCounts, violations, passed } = result; |
| 42 | + |
| 43 | + if (totalPages === 0) { |
| 44 | + console.log('⚠ No pages with tags found - nothing to validate'); |
| 45 | + return 0; |
| 46 | + } |
| 47 | + |
| 48 | + console.log(`📊 Tag Statistics:`); |
| 49 | + console.log(` Total pages: ${totalPages}`); |
| 50 | + console.log(` Unique tags: ${tagCounts.size}`); |
| 51 | + console.log(` Max threshold: ${(MAX_TAG_FREQUENCY * 100).toFixed(0)}%`); |
| 52 | + console.log(''); |
| 53 | + |
| 54 | + const sortedTags = Array.from(tagCounts.entries()).sort((a, b) => b[1] - a[1]); |
| 55 | + |
| 56 | + console.log('🏷️ Top 20 Most Frequent Tags:'); |
| 57 | + console.log(''); |
| 58 | + console.log('Tag Count Frequency Status'); |
| 59 | + console.log('───────────────────────────── ───── ───────── ──────'); |
| 60 | + |
| 61 | + const topTags = sortedTags.slice(0, 20); |
| 62 | + for (const [tag, count] of topTags) { |
| 63 | + const frequency = count / totalPages; |
| 64 | + const freqStr = formatFrequency(count, totalPages); |
| 65 | + const status = frequency > MAX_TAG_FREQUENCY ? '❌ FAIL' : '✅ PASS'; |
| 66 | + const paddedTag = tag.padEnd(30); |
| 67 | + const paddedCount = count.toString().padStart(5); |
| 68 | + const paddedFreq = freqStr.padStart(9); |
| 69 | + console.log(`${paddedTag} ${paddedCount} ${paddedFreq} ${status}`); |
| 70 | + } |
| 71 | + |
| 72 | + console.log(''); |
| 73 | + |
| 74 | + if (passed) { |
| 75 | + console.log('✅ VALIDATION PASSED'); |
| 76 | + console.log(''); |
| 77 | + console.log(`All ${tagCounts.size} tags are within the ${(MAX_TAG_FREQUENCY * 100).toFixed(0)}% frequency threshold.`); |
| 78 | + console.log(''); |
| 79 | + return 0; |
| 80 | + } |
| 81 | + |
| 82 | + console.log('❌ VALIDATION FAILED'); |
| 83 | + console.log(''); |
| 84 | + console.log(`${violations.length} tag(s) exceed the ${(MAX_TAG_FREQUENCY * 100).toFixed(0)}% frequency threshold:`); |
| 85 | + console.log(''); |
| 86 | + |
| 87 | + for (const violation of violations) { |
| 88 | + const freqStr = formatFrequency(violation.count, totalPages); |
| 89 | + console.log(` - "${violation.tag}": ${freqStr} (${violation.count}/${totalPages} pages)`); |
| 90 | + |
| 91 | + const pages = violation.pages; |
| 92 | + console.log(` Pages: ${pages.slice(0, 5).join(', ')}${pages.length > 5 ? ` ... and ${pages.length - 5} more` : ''}`); |
| 93 | + console.log(''); |
| 94 | + } |
| 95 | + |
| 96 | + console.log('💡 Recommendations:'); |
| 97 | + console.log(' - Distribute tags more evenly across pages'); |
| 98 | + console.log(' - Use more specific tags instead of broad categories'); |
| 99 | + console.log(' - Consider splitting high-frequency tags into sub-categories'); |
| 100 | + console.log(''); |
| 101 | + return 1; |
| 102 | +} |
0 commit comments