11/* eslint-disable no-console */
22
3- import type { DtsGenerationConfig , ProcessingContext } from './types'
3+ import type { DtsGenerationConfig , GenerationStats , ProcessingContext } from './types'
44import { Glob } from 'bun'
55import { mkdir , readFile } from 'node:fs/promises'
66import { dirname , relative , resolve } from 'node:path'
@@ -12,9 +12,22 @@ import { writeToFile } from './utils'
1212/**
1313 * Generate DTS files from TypeScript source files
1414 */
15- export async function generate ( options ?: Partial < DtsGenerationConfig > ) : Promise < void > {
15+ export async function generate ( options ?: Partial < DtsGenerationConfig > ) : Promise < GenerationStats > {
16+ const startTime = Date . now ( )
1617 const config = { ...defaultConfig , ...options }
1718
19+ // Statistics tracking
20+ const stats : GenerationStats = {
21+ filesProcessed : 0 ,
22+ filesGenerated : 0 ,
23+ filesFailed : 0 ,
24+ declarationsFound : 0 ,
25+ importsProcessed : 0 ,
26+ exportsProcessed : 0 ,
27+ durationMs : 0 ,
28+ errors : [ ] ,
29+ }
30+
1831 // Log start if verbose
1932 if ( config . verbose ) {
2033 console . log ( 'Starting DTS generation...' )
@@ -32,27 +45,78 @@ export async function generate(options?: Partial<DtsGenerationConfig>): Promise<
3245 for ( const file of files ) {
3346 try {
3447 const outputPath = getOutputPath ( file , config )
35- const dtsContent = await processFile ( file , config )
36-
37- // Ensure output directory exists
38- await mkdir ( dirname ( outputPath ) , { recursive : true } )
48+ const { content : dtsContent , declarationCount, importCount, exportCount } = await processFileWithStats ( file , config )
49+
50+ stats . filesProcessed ++
51+ stats . declarationsFound += declarationCount
52+ stats . importsProcessed += importCount
53+ stats . exportsProcessed += exportCount
54+
55+ if ( config . dryRun ) {
56+ // Dry run - just show what would be generated
57+ console . log ( `[dry-run] Would generate: ${ outputPath } ` )
58+ if ( config . verbose ) {
59+ console . log ( '--- Content preview ---' )
60+ console . log ( dtsContent . slice ( 0 , 500 ) + ( dtsContent . length > 500 ? '\n...' : '' ) )
61+ console . log ( '--- End preview ---' )
62+ }
63+ }
64+ else {
65+ // Ensure output directory exists
66+ await mkdir ( dirname ( outputPath ) , { recursive : true } )
3967
40- // Write the DTS file
41- await writeToFile ( outputPath , dtsContent )
68+ // Write the DTS file
69+ await writeToFile ( outputPath , dtsContent )
70+ stats . filesGenerated ++
4271
43- if ( config . verbose ) {
44- console . log ( `Generated: ${ outputPath } ` )
72+ if ( config . verbose ) {
73+ console . log ( `Generated: ${ outputPath } ` )
74+ }
4575 }
4676 }
4777 catch ( error ) {
48- console . error ( `Error processing ${ file } :` , error )
49- throw error
78+ const errorMessage = error instanceof Error ? error . message : String ( error )
79+ stats . filesFailed ++
80+ stats . errors . push ( { file, error : errorMessage } )
81+
82+ if ( config . continueOnError ) {
83+ console . error ( `[warning] Error processing ${ file } : ${ errorMessage } ` )
84+ }
85+ else {
86+ console . error ( `Error processing ${ file } :` , error )
87+ throw error
88+ }
89+ }
90+ }
91+
92+ stats . durationMs = Date . now ( ) - startTime
93+
94+ // Show stats if enabled
95+ if ( config . stats ) {
96+ console . log ( '\n--- Generation Statistics ---' )
97+ console . log ( `Files processed: ${ stats . filesProcessed } ` )
98+ console . log ( `Files generated: ${ stats . filesGenerated } ` )
99+ if ( stats . filesFailed > 0 ) {
100+ console . log ( `Files failed: ${ stats . filesFailed } ` )
101+ }
102+ console . log ( `Declarations found: ${ stats . declarationsFound } ` )
103+ console . log ( `Imports processed: ${ stats . importsProcessed } ` )
104+ console . log ( `Exports processed: ${ stats . exportsProcessed } ` )
105+ console . log ( `Duration: ${ stats . durationMs } ms` )
106+ if ( stats . errors . length > 0 ) {
107+ console . log ( '\nErrors:' )
108+ for ( const { file, error } of stats . errors ) {
109+ console . log ( ` - ${ file } : ${ error } ` )
110+ }
50111 }
112+ console . log ( '-----------------------------\n' )
51113 }
52114
53115 if ( config . verbose ) {
54116 console . log ( 'DTS generation complete!' )
55117 }
118+
119+ return stats
56120}
57121
58122/**
@@ -118,12 +182,27 @@ export async function processFile(
118182 filePath : string ,
119183 config : DtsGenerationConfig ,
120184) : Promise < string > {
185+ const result = await processFileWithStats ( filePath , config )
186+ return result . content
187+ }
188+
189+ /**
190+ * Process a single TypeScript file and return DTS with statistics
191+ */
192+ async function processFileWithStats (
193+ filePath : string ,
194+ config : DtsGenerationConfig ,
195+ ) : Promise < { content : string , declarationCount : number , importCount : number , exportCount : number } > {
121196 // Read the source file
122197 const sourceCode = await readFile ( filePath , 'utf-8' )
123198
124199 // Extract declarations
125200 const declarations = extractDeclarations ( sourceCode , filePath , config . keepComments )
126201
202+ // Count imports and exports
203+ const importCount = declarations . filter ( d => d . kind === 'import' ) . length
204+ const exportCount = declarations . filter ( d => d . kind === 'export' || d . isExported ) . length
205+
127206 // Create processing context
128207 const context : ProcessingContext = {
129208 filePath,
@@ -137,5 +216,10 @@ export async function processFile(
137216 // Process declarations to generate DTS
138217 const dtsContent = processDeclarations ( declarations , context , config . keepComments , config . importOrder )
139218
140- return dtsContent
219+ return {
220+ content : dtsContent ,
221+ declarationCount : declarations . length ,
222+ importCount,
223+ exportCount,
224+ }
141225}
0 commit comments