1+ import { Bun } from 'bun'
2+
3+ const DEBUG = true // Set to false to disable debug logs
4+
15export async function extract ( filePath : string ) : Promise < string > {
26 try {
37 const sourceCode = await Bun . file ( filePath ) . text ( )
4-
58 return generateDtsTypes ( sourceCode )
69 }
710 catch ( error ) {
@@ -11,7 +14,8 @@ export async function extract(filePath: string): Promise<string> {
1114}
1215
1316function generateDtsTypes ( sourceCode : string ) : string {
14- console . log ( 'Starting generateDtsTypes' )
17+ if ( DEBUG )
18+ console . log ( 'Starting generateDtsTypes' )
1519 const lines = sourceCode . split ( '\n' )
1620 const dtsLines : string [ ] = [ ]
1721 const imports : string [ ] = [ ]
@@ -25,26 +29,30 @@ function generateDtsTypes(sourceCode: string): string {
2529
2630 for ( let i = 0 ; i < lines . length ; i ++ ) {
2731 const line = lines [ i ]
28- console . log ( `Processing line ${ i + 1 } : ${ line } ` )
32+ if ( DEBUG )
33+ console . log ( `Processing line ${ i + 1 } : ${ line } ` )
2934
3035 if ( line . trim ( ) . startsWith ( '/**' ) || line . trim ( ) . startsWith ( '*' ) || line . trim ( ) . startsWith ( '*/' ) ) {
3136 if ( line . trim ( ) . startsWith ( '/**' ) )
3237 lastCommentBlock = ''
3338 lastCommentBlock += `${ line } \n`
34- console . log ( 'Comment line added to lastCommentBlock' )
39+ if ( DEBUG )
40+ console . log ( 'Comment line added to lastCommentBlock' )
3541 continue
3642 }
3743
3844 if ( line . trim ( ) . startsWith ( 'import' ) ) {
3945 const processedImport = processImport ( line )
4046 imports . push ( processedImport )
41- console . log ( `Processed import: ${ processedImport } ` )
47+ if ( DEBUG )
48+ console . log ( `Processed import: ${ processedImport } ` )
4249 continue
4350 }
4451
4552 if ( line . trim ( ) . startsWith ( 'export default' ) ) {
4653 defaultExport = `${ line . trim ( ) } ;`
47- console . log ( `Default export found: ${ defaultExport } ` )
54+ if ( DEBUG )
55+ console . log ( `Default export found: ${ defaultExport } ` )
4856 continue
4957 }
5058
@@ -56,13 +64,15 @@ function generateDtsTypes(sourceCode: string): string {
5664 if ( ! isMultiLineDeclaration ) {
5765 if ( lastCommentBlock ) {
5866 dtsLines . push ( lastCommentBlock . trimEnd ( ) )
59- console . log ( `Comment block added to dtsLines: ${ lastCommentBlock . trimEnd ( ) } ` )
67+ if ( DEBUG )
68+ console . log ( `Comment block added to dtsLines: ${ lastCommentBlock . trimEnd ( ) } ` )
6069 lastCommentBlock = ''
6170 }
6271 const processed = processConstDeclaration ( currentDeclaration . trim ( ) )
6372 if ( processed ) {
6473 dtsLines . push ( processed )
65- console . log ( `Processed const declaration added to dtsLines: ${ processed } ` )
74+ if ( DEBUG )
75+ console . log ( `Processed const declaration added to dtsLines: ${ processed } ` )
6676 }
6777 currentDeclaration = ''
6878 bracketCount = 0
@@ -71,32 +81,37 @@ function generateDtsTypes(sourceCode: string): string {
7181 else if ( line . trim ( ) . startsWith ( 'export' ) ) {
7282 if ( lastCommentBlock ) {
7383 dtsLines . push ( lastCommentBlock . trimEnd ( ) )
74- console . log ( `Comment block added to dtsLines: ${ lastCommentBlock . trimEnd ( ) } ` )
84+ if ( DEBUG )
85+ console . log ( `Comment block added to dtsLines: ${ lastCommentBlock . trimEnd ( ) } ` )
7586 lastCommentBlock = ''
7687 }
7788 const processed = processDeclaration ( line )
7889 if ( processed ) {
7990 dtsLines . push ( processed )
80- console . log ( `Processed declaration added to dtsLines: ${ processed } ` )
91+ if ( DEBUG )
92+ console . log ( `Processed declaration added to dtsLines: ${ processed } ` )
8193 }
8294 }
8395 }
8496
8597 const result = cleanOutput ( [ ...imports , '' , ...dtsLines , '' , ...exports , defaultExport ] . filter ( Boolean ) . join ( '\n' ) )
86- console . log ( 'Final result:' , result )
98+ if ( DEBUG )
99+ console . log ( 'Final result:' , result )
87100 return result
88101}
89102
90103function processImport ( importLine : string ) : string {
91- console . log ( `Processing import: ${ importLine } ` )
104+ if ( DEBUG )
105+ console . log ( `Processing import: ${ importLine } ` )
92106 if ( importLine . includes ( 'type' ) ) {
93107 return importLine . replace ( 'import' , 'import type' ) . replace ( 'type type' , 'type' )
94108 }
95109 return importLine
96110}
97111
98112function processDeclaration ( declaration : string ) : string {
99- console . log ( `Processing declaration: ${ declaration } ` )
113+ if ( DEBUG )
114+ console . log ( `Processing declaration: ${ declaration } ` )
100115 if ( declaration . startsWith ( 'export const' ) ) {
101116 return processConstDeclaration ( declaration )
102117 }
@@ -112,12 +127,14 @@ function processDeclaration(declaration: string): string {
112127 else if ( declaration . startsWith ( 'export default' ) ) {
113128 return `${ declaration } ;`
114129 }
115- console . log ( `Declaration not processed: ${ declaration } ` )
130+ if ( DEBUG )
131+ console . log ( `Declaration not processed: ${ declaration } ` )
116132 return declaration
117133}
118134
119135function processConstDeclaration ( declaration : string ) : string {
120- console . log ( `Processing const declaration: ${ declaration } ` )
136+ if ( DEBUG )
137+ console . log ( `Processing const declaration: ${ declaration } ` )
121138 const lines = declaration . split ( '\n' )
122139 const firstLine = lines [ 0 ]
123140 const name = firstLine . split ( 'export const' ) [ 1 ] . split ( '=' ) [ 0 ] . trim ( ) . split ( ':' ) [ 0 ] . trim ( )
@@ -141,71 +158,53 @@ function processConstDeclaration(declaration: string): string {
141158}
142159
143160function processInterfaceDeclaration ( declaration : string ) : string {
144- console . log ( `Processing interface declaration: ${ declaration } ` )
161+ if ( DEBUG )
162+ console . log ( `Processing interface declaration: ${ declaration } ` )
145163 const lines = declaration . split ( '\n' )
146164 const interfaceName = lines [ 0 ] . split ( 'interface' ) [ 1 ] . split ( '{' ) [ 0 ] . trim ( )
147165 const interfaceBody = lines . slice ( 1 , - 1 ) . map ( line => ` ${ line . trim ( ) } ` ) . join ( '\n' )
148166 const result = `export declare interface ${ interfaceName } {\n${ interfaceBody } \n}`
149- console . log ( `Processed interface declaration: ${ result } ` )
167+ if ( DEBUG )
168+ console . log ( `Processed interface declaration: ${ result } ` )
150169 return result
151170}
152171
153172function processTypeDeclaration ( declaration : string ) : string {
154- console . log ( `Processing type declaration: ${ declaration } ` )
173+ if ( DEBUG )
174+ console . log ( `Processing type declaration: ${ declaration } ` )
155175 const result = declaration . replace ( 'export type' , 'export declare type' )
156- console . log ( `Processed type declaration: ${ result } ` )
176+ if ( DEBUG )
177+ console . log ( `Processed type declaration: ${ result } ` )
157178 return result
158179}
159180
160181function processFunctionDeclaration ( declaration : string ) : string {
161- console . log ( `Processing function declaration: ${ declaration } ` )
182+ if ( DEBUG )
183+ console . log ( `Processing function declaration: ${ declaration } ` )
162184 const functionSignature = declaration . split ( '{' ) [ 0 ] . trim ( )
163185 const result = `export declare ${ functionSignature . replace ( 'export ' , '' ) } ;`
164- console . log ( `Processed function declaration: ${ result } ` )
186+ if ( DEBUG )
187+ console . log ( `Processed function declaration: ${ result } ` )
165188 return result
166189}
167190
168- function parseObjectLiteral ( objectLiteral : string ) : string {
169- console . log ( `Parsing object literal: ${ objectLiteral } ` )
170- const content = objectLiteral . replace ( / ^ \{ | \} $ / g, '' ) . split ( ',' ) . map ( pair => pair . trim ( ) )
171- const parsedProperties = content . map ( ( pair ) => {
172- const [ key , value ] = pair . split ( ':' ) . map ( p => p . trim ( ) )
173- console . log ( `Parsing property: key=${ key } , value=${ value } ` )
174- return ` ${ key } : ${ value } ;`
175- } )
176- const result = `{\n${ parsedProperties . join ( '\n' ) } \n}`
177- console . log ( `Parsed object literal: ${ result } ` )
178- return result
179- }
180-
181- function inferType ( properties : string [ ] ) : string {
182- const types = properties . map ( ( prop ) => {
183- const value = prop . split ( ':' ) [ 1 ] . trim ( )
184- if ( value . startsWith ( '\'' ) || value . startsWith ( '"' ) )
185- return 'string'
186- if ( value === 'true' || value === 'false' )
187- return 'boolean'
188- if ( ! isNaN ( Number ( value ) ) )
189- return 'number'
190- return 'any'
191- } )
192- const uniqueTypes = [ ...new Set ( types ) ]
193- return uniqueTypes . length === 1 ? `{ [key: string]: ${ uniqueTypes [ 0 ] } }` : '{ [key: string]: any }'
194- }
195-
196191function cleanOutput ( output : string ) : string {
197- console . log ( 'Cleaning output' )
192+ if ( DEBUG )
193+ console . log ( 'Cleaning output' )
198194 const result = output
199195 . replace ( / \{ \s * \} / g, '{}' )
200196 . replace ( / \s * ; \s * (? = \} | $ ) / g, ';' )
201197 . replace ( / \n + / g, '\n' )
202198 . replace ( / ; \n \} / g, ';\n}' )
203199 . replace ( / \{ ; / g, '{' )
204- . replace ( / \} ; \n / g, '}\n\n' ) // Add an extra line break after each declaration
205- . replace ( / \} \n (? ! $ ) / g, '}\n\n' ) // Add an extra line break after closing braces, except for the last one
206- . replace ( / \n { 3 , } / g, '\n\n' ) // Replace three or more consecutive newlines with two newlines
207- . replace ( / ; \n ( \s * ) \} / g, ';\n$1\n$1}' ) // Ensure closing bracket is on its own line
200+ . replace ( / \} ; \n / g, '}\n\n' )
201+ . replace ( / \} \n (? ! $ ) / g, '}\n\n' )
202+ . replace ( / \n { 3 , } / g, '\n\n' )
203+ . replace ( / ; \n ( \s * ) \} / g, ';\n$1\n$1}' )
204+ . replace ( / , \n \s * ; / g, ';' ) // Remove unnecessary commas before semicolons
205+ . replace ( / ; \s * \/ \/ \s * / g, '; // ' ) // Ensure comments are properly formatted
208206 . trim ( )
209- console . log ( 'Cleaned output:' , result )
207+ if ( DEBUG )
208+ console . log ( 'Cleaned output:' , result )
210209 return result
211210}
0 commit comments