|
| 1 | +import { Project, type Type } from 'ts-morph'; |
| 2 | +import * as path from 'node:path'; |
| 3 | +import * as fs from 'node:fs/promises'; |
| 4 | +import { createHash } from 'node:crypto'; |
| 5 | +import { typeToNode } from './lib/type-tree'; |
| 6 | +import type { Cache } from './lib/cache'; |
| 7 | +import type { TypeNode } from './lib/types'; |
| 8 | + |
1 | 9 | export interface StoryOptions { |
2 | 10 | from: { |
3 | 11 | file: string; |
4 | 12 | export: string; |
5 | 13 | }; |
| 14 | + tsconfigPath?: string; |
| 15 | + cache?: Cache | false; |
6 | 16 | } |
7 | 17 |
|
8 | | -export function defineStory(options: StoryOptions) {} |
| 18 | +export * from './lib/types'; |
| 19 | +export * from './lib/cache'; |
| 20 | + |
| 21 | +export interface StoryResult { |
| 22 | + props: TypeNode; |
| 23 | +} |
| 24 | + |
| 25 | +export async function defineStory(options: StoryOptions): Promise<StoryResult> { |
| 26 | + const filePath = path.resolve(options.from.file); |
| 27 | + const fileContent = await fs.readFile(filePath, 'utf-8'); |
| 28 | + |
| 29 | + // Generate cache key based on file path, export name, and content hash |
| 30 | + const contentHash = createHash('MD5').update(fileContent).digest('hex').slice(0, 12); |
| 31 | + const cacheKey = `${filePath}:${options.from.export}:${contentHash}`; |
| 32 | + |
| 33 | + // Try to read from cache |
| 34 | + if (options.cache !== false) { |
| 35 | + const cached = await options.cache?.read(cacheKey); |
| 36 | + if (cached) { |
| 37 | + return cached as StoryResult; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + const project = new Project({ |
| 42 | + tsConfigFilePath: options.tsconfigPath ?? './tsconfig.json', |
| 43 | + skipAddingFilesFromTsConfig: true, |
| 44 | + }); |
| 45 | + |
| 46 | + const sourceFile = project.addSourceFileAtPath(filePath); |
| 47 | + |
| 48 | + const exportedDeclarations = sourceFile.getExportedDeclarations(); |
| 49 | + const declaration = exportedDeclarations.get(options.from.export)?.[0]; |
| 50 | + |
| 51 | + if (!declaration) { |
| 52 | + throw new Error(`Export "${options.from.export}" not found in file "${options.from.file}"`); |
| 53 | + } |
| 54 | + |
| 55 | + const type = declaration.getType(); |
| 56 | + const checker = project.getTypeChecker(); |
| 57 | + |
| 58 | + // Extract props type from React component |
| 59 | + let propsType: Type | undefined; |
| 60 | + |
| 61 | + // Check if it's a function component |
| 62 | + const callSignatures = type.getCallSignatures(); |
| 63 | + if (callSignatures.length > 0) { |
| 64 | + // Function component: props are the first parameter |
| 65 | + const firstParam = callSignatures[0]?.getParameters()[0]; |
| 66 | + if (firstParam) { |
| 67 | + propsType = firstParam.getTypeAtLocation(declaration); |
| 68 | + } |
| 69 | + } else if (type.isClassOrInterface()) { |
| 70 | + // Class component: look for props property or constructor parameter |
| 71 | + const propsProperty = type.getProperty('props'); |
| 72 | + if (propsProperty) { |
| 73 | + propsType = propsProperty.getTypeAtLocation(declaration); |
| 74 | + } else { |
| 75 | + // Try to get from constructor |
| 76 | + const constructSignatures = type.getConstructSignatures(); |
| 77 | + if (constructSignatures.length > 0) { |
| 78 | + const firstParam = constructSignatures[0]?.getParameters()[0]; |
| 79 | + if (firstParam) { |
| 80 | + propsType = firstParam.getTypeAtLocation(declaration); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } else if (type.isObject()) { |
| 85 | + // Already an object type, use it directly |
| 86 | + propsType = type; |
| 87 | + } |
| 88 | + |
| 89 | + if (!propsType) { |
| 90 | + // Fallback: use the type itself |
| 91 | + propsType = type; |
| 92 | + } |
| 93 | + |
| 94 | + const propsNode = typeToNode(propsType, checker, declaration); |
| 95 | + |
| 96 | + const result: StoryResult = { |
| 97 | + props: propsNode, |
| 98 | + }; |
| 99 | + |
| 100 | + // Write to cache |
| 101 | + if (options.cache !== false) { |
| 102 | + await options.cache?.write(cacheKey, result); |
| 103 | + } |
| 104 | + |
| 105 | + return result; |
| 106 | +} |
0 commit comments