|
| 1 | +import * as fs from 'node:fs'; |
| 2 | +import * as path from 'node:path'; |
| 3 | + |
| 4 | +import type { CommandHandler } from '../../utils/types'; |
| 5 | +import type { MetadataArgs, MetadataOutput } from './impl/types'; |
| 6 | +import { resolveEntry, readPackageInfo } from './impl/entry-resolver'; |
| 7 | +import { parseDtsEntry } from './impl/dts-parser'; |
| 8 | +import { loadDependencyMetadata, buildCrossPackageRef } from './impl/cross-package-resolver'; |
| 9 | +import { formatMetadataAsMarkdown } from './impl/markdown-formatter'; |
| 10 | +import { formatMetadataAsHtml } from './impl/html-formatter'; |
| 11 | + |
| 12 | +const LEGEND = { |
| 13 | + components: { name: 'Components', description: 'React components (ForwardRef, FC, class)' }, |
| 14 | + hooks: { name: 'Hooks', description: 'React hooks (use* convention)' }, |
| 15 | + types: { name: 'Types', description: 'Interfaces, type aliases, and enums' }, |
| 16 | + others: { name: 'Others', description: 'Constants, render functions, and utilities' }, |
| 17 | +}; |
| 18 | + |
| 19 | +export const handler: CommandHandler<MetadataArgs> = async argv => { |
| 20 | + const { entry, reporter = 'json', output } = argv; |
| 21 | + |
| 22 | + // 1. Resolve entry .d.ts |
| 23 | + const entryPath = resolveEntry(entry); |
| 24 | + const packageInfo = readPackageInfo(path.dirname(entryPath)); |
| 25 | + |
| 26 | + // 2. Parse the .d.ts |
| 27 | + const parseResult = parseDtsEntry(entryPath); |
| 28 | + |
| 29 | + // 3. Resolve cross-package $refs |
| 30 | + const cwd = process.cwd(); |
| 31 | + const depMetadataCache = new Map<string, MetadataOutput | null>(); |
| 32 | + |
| 33 | + for (const pkgSpec of parseResult.importedPackages) { |
| 34 | + if (!depMetadataCache.has(pkgSpec)) { |
| 35 | + depMetadataCache.set(pkgSpec, loadDependencyMetadata(pkgSpec, cwd)); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + // Enhance component propsType refs with cross-package resolution |
| 40 | + for (const comp of Object.values(parseResult.components)) { |
| 41 | + if (comp.propsType && '$ref' in comp.propsType) { |
| 42 | + const localRef = comp.propsType.$ref; |
| 43 | + const symbolName = localRef.split('/').pop()!; |
| 44 | + |
| 45 | + // If the type isn't in our own types, check dependencies |
| 46 | + if (!(symbolName in parseResult.types)) { |
| 47 | + for (const [pkgSpec, depMetadata] of depMetadataCache) { |
| 48 | + if (depMetadata) { |
| 49 | + const crossRef = buildCrossPackageRef(pkgSpec, symbolName, depMetadata); |
| 50 | + if (crossRef) { |
| 51 | + comp.propsType = crossRef; |
| 52 | + break; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // 4. Assemble output |
| 61 | + const metadataOutput: MetadataOutput = { |
| 62 | + package: packageInfo, |
| 63 | + legend: LEGEND, |
| 64 | + categories: { |
| 65 | + components: parseResult.components, |
| 66 | + hooks: parseResult.hooks, |
| 67 | + types: parseResult.types, |
| 68 | + others: parseResult.others, |
| 69 | + }, |
| 70 | + }; |
| 71 | + |
| 72 | + // 5. Format |
| 73 | + let formatted: string; |
| 74 | + switch (reporter) { |
| 75 | + case 'markdown': |
| 76 | + formatted = formatMetadataAsMarkdown(metadataOutput); |
| 77 | + break; |
| 78 | + case 'html': |
| 79 | + formatted = formatMetadataAsHtml(metadataOutput); |
| 80 | + break; |
| 81 | + case 'json': |
| 82 | + default: |
| 83 | + formatted = JSON.stringify(metadataOutput, null, 2); |
| 84 | + break; |
| 85 | + } |
| 86 | + |
| 87 | + // 6. Output |
| 88 | + if (output) { |
| 89 | + const outputPath = path.resolve(cwd, output); |
| 90 | + fs.mkdirSync(path.dirname(outputPath), { recursive: true }); |
| 91 | + fs.writeFileSync(outputPath, formatted, 'utf-8'); |
| 92 | + console.log(`Metadata written to ${outputPath}`); |
| 93 | + } else { |
| 94 | + console.log(formatted); |
| 95 | + } |
| 96 | +}; |
0 commit comments