1
+ import { Bun } from 'bun'
2
+
3
+ const DEBUG = true // Set to false to disable debug logs
4
+
1
5
export async function extract ( filePath : string ) : Promise < string > {
2
6
try {
3
7
const sourceCode = await Bun . file ( filePath ) . text ( )
4
-
5
8
return generateDtsTypes ( sourceCode )
6
9
}
7
10
catch ( error ) {
@@ -11,7 +14,8 @@ export async function extract(filePath: string): Promise<string> {
11
14
}
12
15
13
16
function generateDtsTypes ( sourceCode : string ) : string {
14
- console . log ( 'Starting generateDtsTypes' )
17
+ if ( DEBUG )
18
+ console . log ( 'Starting generateDtsTypes' )
15
19
const lines = sourceCode . split ( '\n' )
16
20
const dtsLines : string [ ] = [ ]
17
21
const imports : string [ ] = [ ]
@@ -25,26 +29,30 @@ function generateDtsTypes(sourceCode: string): string {
25
29
26
30
for ( let i = 0 ; i < lines . length ; i ++ ) {
27
31
const line = lines [ i ]
28
- console . log ( `Processing line ${ i + 1 } : ${ line } ` )
32
+ if ( DEBUG )
33
+ console . log ( `Processing line ${ i + 1 } : ${ line } ` )
29
34
30
35
if ( line . trim ( ) . startsWith ( '/**' ) || line . trim ( ) . startsWith ( '*' ) || line . trim ( ) . startsWith ( '*/' ) ) {
31
36
if ( line . trim ( ) . startsWith ( '/**' ) )
32
37
lastCommentBlock = ''
33
38
lastCommentBlock += `${ line } \n`
34
- console . log ( 'Comment line added to lastCommentBlock' )
39
+ if ( DEBUG )
40
+ console . log ( 'Comment line added to lastCommentBlock' )
35
41
continue
36
42
}
37
43
38
44
if ( line . trim ( ) . startsWith ( 'import' ) ) {
39
45
const processedImport = processImport ( line )
40
46
imports . push ( processedImport )
41
- console . log ( `Processed import: ${ processedImport } ` )
47
+ if ( DEBUG )
48
+ console . log ( `Processed import: ${ processedImport } ` )
42
49
continue
43
50
}
44
51
45
52
if ( line . trim ( ) . startsWith ( 'export default' ) ) {
46
53
defaultExport = `${ line . trim ( ) } ;`
47
- console . log ( `Default export found: ${ defaultExport } ` )
54
+ if ( DEBUG )
55
+ console . log ( `Default export found: ${ defaultExport } ` )
48
56
continue
49
57
}
50
58
@@ -56,13 +64,15 @@ function generateDtsTypes(sourceCode: string): string {
56
64
if ( ! isMultiLineDeclaration ) {
57
65
if ( lastCommentBlock ) {
58
66
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 ( ) } ` )
60
69
lastCommentBlock = ''
61
70
}
62
71
const processed = processConstDeclaration ( currentDeclaration . trim ( ) )
63
72
if ( processed ) {
64
73
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 } ` )
66
76
}
67
77
currentDeclaration = ''
68
78
bracketCount = 0
@@ -71,32 +81,37 @@ function generateDtsTypes(sourceCode: string): string {
71
81
else if ( line . trim ( ) . startsWith ( 'export' ) ) {
72
82
if ( lastCommentBlock ) {
73
83
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 ( ) } ` )
75
86
lastCommentBlock = ''
76
87
}
77
88
const processed = processDeclaration ( line )
78
89
if ( processed ) {
79
90
dtsLines . push ( processed )
80
- console . log ( `Processed declaration added to dtsLines: ${ processed } ` )
91
+ if ( DEBUG )
92
+ console . log ( `Processed declaration added to dtsLines: ${ processed } ` )
81
93
}
82
94
}
83
95
}
84
96
85
97
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 )
87
100
return result
88
101
}
89
102
90
103
function processImport ( importLine : string ) : string {
91
- console . log ( `Processing import: ${ importLine } ` )
104
+ if ( DEBUG )
105
+ console . log ( `Processing import: ${ importLine } ` )
92
106
if ( importLine . includes ( 'type' ) ) {
93
107
return importLine . replace ( 'import' , 'import type' ) . replace ( 'type type' , 'type' )
94
108
}
95
109
return importLine
96
110
}
97
111
98
112
function processDeclaration ( declaration : string ) : string {
99
- console . log ( `Processing declaration: ${ declaration } ` )
113
+ if ( DEBUG )
114
+ console . log ( `Processing declaration: ${ declaration } ` )
100
115
if ( declaration . startsWith ( 'export const' ) ) {
101
116
return processConstDeclaration ( declaration )
102
117
}
@@ -112,12 +127,14 @@ function processDeclaration(declaration: string): string {
112
127
else if ( declaration . startsWith ( 'export default' ) ) {
113
128
return `${ declaration } ;`
114
129
}
115
- console . log ( `Declaration not processed: ${ declaration } ` )
130
+ if ( DEBUG )
131
+ console . log ( `Declaration not processed: ${ declaration } ` )
116
132
return declaration
117
133
}
118
134
119
135
function processConstDeclaration ( declaration : string ) : string {
120
- console . log ( `Processing const declaration: ${ declaration } ` )
136
+ if ( DEBUG )
137
+ console . log ( `Processing const declaration: ${ declaration } ` )
121
138
const lines = declaration . split ( '\n' )
122
139
const firstLine = lines [ 0 ]
123
140
const name = firstLine . split ( 'export const' ) [ 1 ] . split ( '=' ) [ 0 ] . trim ( ) . split ( ':' ) [ 0 ] . trim ( )
@@ -141,71 +158,53 @@ function processConstDeclaration(declaration: string): string {
141
158
}
142
159
143
160
function processInterfaceDeclaration ( declaration : string ) : string {
144
- console . log ( `Processing interface declaration: ${ declaration } ` )
161
+ if ( DEBUG )
162
+ console . log ( `Processing interface declaration: ${ declaration } ` )
145
163
const lines = declaration . split ( '\n' )
146
164
const interfaceName = lines [ 0 ] . split ( 'interface' ) [ 1 ] . split ( '{' ) [ 0 ] . trim ( )
147
165
const interfaceBody = lines . slice ( 1 , - 1 ) . map ( line => ` ${ line . trim ( ) } ` ) . join ( '\n' )
148
166
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 } ` )
150
169
return result
151
170
}
152
171
153
172
function processTypeDeclaration ( declaration : string ) : string {
154
- console . log ( `Processing type declaration: ${ declaration } ` )
173
+ if ( DEBUG )
174
+ console . log ( `Processing type declaration: ${ declaration } ` )
155
175
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 } ` )
157
178
return result
158
179
}
159
180
160
181
function processFunctionDeclaration ( declaration : string ) : string {
161
- console . log ( `Processing function declaration: ${ declaration } ` )
182
+ if ( DEBUG )
183
+ console . log ( `Processing function declaration: ${ declaration } ` )
162
184
const functionSignature = declaration . split ( '{' ) [ 0 ] . trim ( )
163
185
const result = `export declare ${ functionSignature . replace ( 'export ' , '' ) } ;`
164
- console . log ( `Processed function declaration: ${ result } ` )
186
+ if ( DEBUG )
187
+ console . log ( `Processed function declaration: ${ result } ` )
165
188
return result
166
189
}
167
190
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
-
196
191
function cleanOutput ( output : string ) : string {
197
- console . log ( 'Cleaning output' )
192
+ if ( DEBUG )
193
+ console . log ( 'Cleaning output' )
198
194
const result = output
199
195
. replace ( / \{ \s * \} / g, '{}' )
200
196
. replace ( / \s * ; \s * (? = \} | $ ) / g, ';' )
201
197
. replace ( / \n + / g, '\n' )
202
198
. replace ( / ; \n \} / g, ';\n}' )
203
199
. 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
208
206
. trim ( )
209
- console . log ( 'Cleaned output:' , result )
207
+ if ( DEBUG )
208
+ console . log ( 'Cleaned output:' , result )
210
209
return result
211
210
}
0 commit comments