@@ -4,6 +4,18 @@ import { readFileSync, writeFileSync } from 'node:fs';
44import path from 'node:path' ;
55import { Project , type SourceFile } from 'ts-morph' ;
66
7+ const startTime = Date . now ( ) ;
8+ const repoRoot = path . resolve ( __dirname , '..' ) ;
9+
10+ function logDebug ( message : string ) {
11+ const elapsed = ( ( Date . now ( ) - startTime ) / 1000 ) . toFixed ( 3 ) ;
12+ console . log ( `[${ elapsed } s] ${ message } ` ) ;
13+ }
14+
15+ function prettyPath ( filePath : string ) {
16+ return path . relative ( repoRoot , filePath ) ;
17+ }
18+
719// Get target package from command line argument
820const targetPackage = process . argv [ 2 ] ;
921if ( ! targetPackage ) {
@@ -12,6 +24,8 @@ if (!targetPackage) {
1224 process . exit ( 1 ) ;
1325}
1426
27+ logDebug ( `Starting re-export generation for package "${ targetPackage } "` ) ;
28+
1529const SRC_FILE = path . resolve (
1630 __dirname ,
1731 `../packages/${ targetPackage } /src/index.ts` ,
@@ -21,6 +35,9 @@ const CMA_CLIENT_PATH = path.resolve(
2135 '../packages/cma-client/dist/types/index.d.ts' ,
2236) ;
2337
38+ logDebug ( `Source file path resolved to ${ prettyPath ( SRC_FILE ) } ` ) ;
39+ logDebug ( `CMA client types source at ${ prettyPath ( CMA_CLIENT_PATH ) } ` ) ;
40+
2441const START_MARKER = '// <AUTO-GENERATED-EXPORTS>' ;
2542const END_MARKER = '// </AUTO-GENERATED-EXPORTS>' ;
2643
@@ -49,8 +66,13 @@ const collected = {
4966const visitedFiles = new Set < string > ( ) ;
5067
5168function collectExports ( file : SourceFile ) {
52- if ( visitedFiles . has ( file . getFilePath ( ) ) ) return ;
53- visitedFiles . add ( file . getFilePath ( ) ) ;
69+ const currentPath = file . getFilePath ( ) ;
70+ if ( visitedFiles . has ( currentPath ) ) {
71+ logDebug ( `Skipping already visited file ${ prettyPath ( currentPath ) } ` ) ;
72+ return ;
73+ }
74+ visitedFiles . add ( currentPath ) ;
75+ logDebug ( `Collecting exports from ${ prettyPath ( currentPath ) } ` ) ;
5476
5577 for ( const stmt of file . getExportDeclarations ( ) ) {
5678 const moduleSpecifier = stmt . getModuleSpecifierSourceFile ( ) ;
@@ -73,7 +95,14 @@ function collectExports(file: SourceFile) {
7395
7496 if ( isWildcard && moduleSpecifier ) {
7597 // Case 1: moduleSpecifier is resolved by ts-morph
76- collectExports ( moduleSpecifier ) ;
98+ if ( moduleSpecifier ) {
99+ logDebug (
100+ `Discovered wildcard export in ${ prettyPath (
101+ currentPath ,
102+ ) } → walking ${ prettyPath ( moduleSpecifier . getFilePath ( ) ) } `,
103+ ) ;
104+ collectExports ( moduleSpecifier ) ;
105+ }
77106 } else if ( isWildcard && stmt . getModuleSpecifier ( ) ) {
78107 // Case 2: relative imports that ts-morph didn't resolve
79108 const moduleString = stmt . getModuleSpecifier ( ) ?. getLiteralValue ( ) ;
@@ -84,6 +113,11 @@ function collectExports(file: SourceFile) {
84113
85114 try {
86115 const targetFile = project . addSourceFileAtPath ( resolvedPath ) ;
116+ logDebug (
117+ `Attempting manual resolution for wildcard export in ${ prettyPath (
118+ currentPath ,
119+ ) } → ${ prettyPath ( resolvedPath ) } `,
120+ ) ;
87121 collectExports ( targetFile ) ;
88122 } catch ( e ) {
89123 console . warn (
@@ -143,11 +177,20 @@ function collectExports(file: SourceFile) {
143177 collected . value . add ( e . getName ( ) ) ;
144178 }
145179 }
180+
181+ logDebug (
182+ `Finished ${ prettyPath ( currentPath ) } → ${ collected . value . size } value exports, ${ collected . type . size } type exports accumulated` ,
183+ ) ;
146184}
147185
148186const cmaFile = project . addSourceFileAtPath ( CMA_CLIENT_PATH ) ;
187+ logDebug ( `Loaded root CMA client declaration file ${ prettyPath ( cmaFile . getFilePath ( ) ) } ` ) ;
149188collectExports ( cmaFile ) ;
150189
190+ logDebug (
191+ `Export collection completed across ${ visitedFiles . size } files → ${ collected . value . size } values, ${ collected . type . size } types` ,
192+ ) ;
193+
151194const valueExports = Array . from ( collected . value ) . sort ( ) ;
152195const typeExports = Array . from ( collected . type ) . sort ( ) ;
153196
@@ -175,10 +218,13 @@ try {
175218if ( content . includes ( START_MARKER ) && content . includes ( END_MARKER ) ) {
176219 const regex = new RegExp ( `${ START_MARKER } [\\s\\S]*?${ END_MARKER } ` , 'm' ) ;
177220 content = content . replace ( regex , newExportBlock ) ;
221+ logDebug ( 'Updated existing auto-generated export block' ) ;
178222} else {
179223 content = `${ newExportBlock } \n\n${ content } ` ;
224+ logDebug ( 'Inserted new auto-generated export block at file top' ) ;
180225}
181226
182227// Write updated file
183228writeFileSync ( SRC_FILE , content ) ;
184- console . log ( `Updated exports (including types) in ${ SRC_FILE } ` ) ;
229+ logDebug ( `Wrote updated exports to ${ prettyPath ( SRC_FILE ) } ` ) ;
230+ logDebug ( 'Re-export generation script completed' ) ;
0 commit comments