|
1 | 1 | import { |
2 | 2 | type ExportedDeclarations, |
3 | | - type Project, |
| 3 | + Project, |
4 | 4 | type Symbol as TsSymbol, |
5 | 5 | ts, |
6 | 6 | type Type, |
7 | 7 | } from 'ts-morph'; |
8 | 8 | import { getProject, type TypescriptConfig } from '@/get-project'; |
| 9 | +import fs from 'node:fs'; |
9 | 10 |
|
10 | 11 | export interface GeneratedDoc { |
11 | 12 | name: string; |
@@ -49,44 +50,68 @@ export interface GenerateOptions { |
49 | 50 | transform?: Transformer; |
50 | 51 | } |
51 | 52 |
|
52 | | -export interface GenerateDocumentationOptions extends GenerateOptions { |
53 | | - /** |
54 | | - * Typescript configurations |
55 | | - */ |
56 | | - config?: TypescriptConfig; |
57 | | - project?: Project; |
| 53 | +export type Generator = ReturnType<typeof createGenerator>; |
| 54 | + |
| 55 | +export function createGenerator(config?: TypescriptConfig | Project) { |
| 56 | + const project = config instanceof Project ? config : getProject(config); |
| 57 | + |
| 58 | + return { |
| 59 | + generateDocumentation( |
| 60 | + file: { |
| 61 | + path: string; |
| 62 | + content?: string; |
| 63 | + }, |
| 64 | + name: string | undefined, |
| 65 | + options: GenerateOptions = {}, |
| 66 | + ) { |
| 67 | + const sourceFile = project.createSourceFile( |
| 68 | + file.path, |
| 69 | + file.content ?? fs.readFileSync(file.path).toString(), |
| 70 | + { |
| 71 | + overwrite: true, |
| 72 | + }, |
| 73 | + ); |
| 74 | + const out: GeneratedDoc[] = []; |
| 75 | + |
| 76 | + for (const [k, d] of sourceFile.getExportedDeclarations()) { |
| 77 | + if (name && name !== k) continue; |
| 78 | + |
| 79 | + if (d.length > 1) |
| 80 | + console.warn( |
| 81 | + `export ${k} should not have more than one type declaration.`, |
| 82 | + ); |
| 83 | + |
| 84 | + out.push(generate(project, k, d[0], options)); |
| 85 | + } |
| 86 | + |
| 87 | + return out; |
| 88 | + }, |
| 89 | + }; |
58 | 90 | } |
59 | 91 |
|
60 | 92 | /** |
61 | 93 | * Generate documentation for properties in an exported type/interface |
| 94 | + * |
| 95 | + * @deprecated use `createGenerator` instead |
62 | 96 | */ |
63 | 97 | export function generateDocumentation( |
64 | 98 | file: string, |
65 | 99 | name: string | undefined, |
66 | 100 | content: string, |
67 | | - options: GenerateDocumentationOptions = {}, |
| 101 | + options: GenerateOptions & { |
| 102 | + /** |
| 103 | + * Typescript configurations |
| 104 | + */ |
| 105 | + config?: TypescriptConfig; |
| 106 | + project?: Project; |
| 107 | + } = {}, |
68 | 108 | ): GeneratedDoc[] { |
69 | | - const project = options.project ?? getProject(options.config); |
70 | | - const sourceFile = project.createSourceFile(file, content, { |
71 | | - overwrite: true, |
72 | | - }); |
73 | | - const out: GeneratedDoc[] = []; |
74 | | - |
75 | | - for (const [k, d] of sourceFile.getExportedDeclarations()) { |
76 | | - if (name && name !== k) continue; |
77 | | - |
78 | | - if (d.length > 1) |
79 | | - console.warn( |
80 | | - `export ${k} should not have more than one type declaration.`, |
81 | | - ); |
82 | | - |
83 | | - out.push(generate(project, k, d[0], options)); |
84 | | - } |
| 109 | + const gen = createGenerator(options.project ?? options.config); |
85 | 110 |
|
86 | | - return out; |
| 111 | + return gen.generateDocumentation({ path: file, content }, name, options); |
87 | 112 | } |
88 | 113 |
|
89 | | -export function generate( |
| 114 | +function generate( |
90 | 115 | program: Project, |
91 | 116 | name: string, |
92 | 117 | declaration: ExportedDeclarations, |
|
0 commit comments