|
| 1 | +#!/usr/bin/env tsx |
| 2 | + |
| 3 | +import fs from 'fs' |
| 4 | +import path from 'path' |
| 5 | + |
| 6 | +const README_PATH = path.join(process.cwd(), 'README.md') |
| 7 | +const DOCS_PATH = path.join(process.cwd(), 'docs', 'README.md') |
| 8 | + |
| 9 | +function extractCategorizedSections(docsContent: string): string { |
| 10 | + const sections = [ |
| 11 | + { name: 'Schema', newName: 'Schema Types' }, |
| 12 | + { name: 'File Operations', newName: 'File Operations Methods' }, |
| 13 | + { name: 'Report Processing', newName: 'Report Processing Methods' }, |
| 14 | + { name: 'Validation', newName: 'Validation Methods' }, |
| 15 | + { name: 'Tree Operations', newName: 'Tree Operations Methods' }, |
| 16 | + { name: 'Enumerations', newName: 'Utility Types' }, |
| 17 | + { name: 'Interfaces', newName: 'Utility Types' }, |
| 18 | + { name: 'Type Aliases', newName: 'Utility Types' }, |
| 19 | + ] |
| 20 | + |
| 21 | + let apiContent = '## API Reference\n' |
| 22 | + let utilityTypesAdded = false |
| 23 | + |
| 24 | + for (const section of sections) { |
| 25 | + const sectionStart = docsContent.indexOf(`## ${section.name}`) |
| 26 | + if (sectionStart === -1) continue |
| 27 | + |
| 28 | + const nextSectionStart = docsContent.indexOf('## ', sectionStart + 1) |
| 29 | + const sectionEnd = |
| 30 | + nextSectionStart === -1 ? docsContent.length : nextSectionStart |
| 31 | + |
| 32 | + let sectionContent = docsContent.substring(sectionStart, sectionEnd).trim() |
| 33 | + |
| 34 | + sectionContent = sectionContent.replace(/\]\(([^)]+)\)/g, (match, url) => { |
| 35 | + if (url.startsWith('http') || url.startsWith('docs/')) { |
| 36 | + return match |
| 37 | + } |
| 38 | + return `](docs/${url})` |
| 39 | + }) |
| 40 | + |
| 41 | + if (['Enumerations', 'Interfaces', 'Type Aliases'].includes(section.name)) { |
| 42 | + if (!utilityTypesAdded) { |
| 43 | + apiContent += '\n### Utility Types\n\n' |
| 44 | + utilityTypesAdded = true |
| 45 | + } |
| 46 | + const lines = sectionContent.split('\n') |
| 47 | + let listItems = lines.filter(line => line.startsWith('- [')) |
| 48 | + if (section.name === 'Enumerations' && listItems.length > 0) { |
| 49 | + listItems = listItems.map(item => item + ' (enumeration)') |
| 50 | + } |
| 51 | + if (listItems.length > 0) { |
| 52 | + apiContent += listItems.join('\n') + '\n' |
| 53 | + } |
| 54 | + } else { |
| 55 | + sectionContent = sectionContent.replace( |
| 56 | + `## ${section.name}`, |
| 57 | + `### ${section.newName}` |
| 58 | + ) |
| 59 | + apiContent += '\n' + sectionContent + '\n' |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return apiContent.trim() |
| 64 | +} |
| 65 | + |
| 66 | +function updateReadmeWithDocs() { |
| 67 | + try { |
| 68 | + const originalReadme = fs.readFileSync(README_PATH, 'utf-8') |
| 69 | + |
| 70 | + const generatedDocs = fs.readFileSync(DOCS_PATH, 'utf-8') |
| 71 | + |
| 72 | + const apiReferenceContent = extractCategorizedSections(generatedDocs) |
| 73 | + |
| 74 | + const originalApiReferenceStart = originalReadme.indexOf('## API Reference') |
| 75 | + const nextSectionStart = originalReadme.indexOf( |
| 76 | + '## ', |
| 77 | + originalApiReferenceStart + 1 |
| 78 | + ) |
| 79 | + |
| 80 | + if (originalApiReferenceStart === -1) { |
| 81 | + console.error('Could not find "## API Reference" section in README.md') |
| 82 | + process.exit(1) |
| 83 | + } |
| 84 | + |
| 85 | + const beforeApiReference = originalReadme.substring( |
| 86 | + 0, |
| 87 | + originalApiReferenceStart |
| 88 | + ) |
| 89 | + |
| 90 | + const afterApiReference = |
| 91 | + nextSectionStart !== -1 ? originalReadme.substring(nextSectionStart) : '' |
| 92 | + |
| 93 | + const newReadme = |
| 94 | + beforeApiReference + |
| 95 | + apiReferenceContent + |
| 96 | + (afterApiReference ? '\n\n' + afterApiReference : '') |
| 97 | + |
| 98 | + fs.writeFileSync(README_PATH, newReadme) |
| 99 | + |
| 100 | + console.log('✅ README.md updated with categorized API documentation') |
| 101 | + } catch (error) { |
| 102 | + console.error('❌ Error updating README:', error) |
| 103 | + process.exit(1) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +updateReadmeWithDocs() |
0 commit comments