@@ -114,60 +114,68 @@ export class TypeScriptStructureAnalyzer {
114114 }
115115
116116 const allFiles = new Set < string > ( ) ;
117+ const isNodeModules = ( filePath : string ) => {
118+ const relativePath = path . relative ( module . path , filePath ) ;
119+ return relativePath . split ( path . sep ) . includes ( 'node_modules' ) ;
120+ } ;
121+
122+ const addFileIfAllowed = ( filePath : string ) => {
123+ if ( ! isNodeModules ( filePath ) ) {
124+ allFiles . add ( filePath ) ;
125+ }
126+ } ;
117127
118128 // 1. Handle srcPatterns if provided
119129 if ( options . srcPatterns && options . srcPatterns . length > 0 ) {
120130 // For now, if patterns are provided, we search the entire module for matching files
121131 // In a more complex implementation, we might want to support actual glob matching
122132 // Here we reuse findTypeScriptFiles which already finds .ts/.js files
123133 const files = this . findTypeScriptFiles ( module . path , options ) ;
124- files . forEach ( f => allFiles . add ( f ) ) ;
125- return Array . from ( allFiles ) ;
126- }
127-
128- // 2. Original behavior fallback
129- // Get tsconfig.json configuration
130- const config = this . tsConfigCache . getTsConfig ( module . path ) ;
131-
132- // Default: all files in tsconfig.json
133- if ( config . fileNames && config . fileNames . length > 0 ) {
134- config . fileNames . forEach ( file => {
135- if ( fs . existsSync ( file ) ) {
136- allFiles . add ( file ) ;
137- }
138- } ) ;
139- if ( allFiles . size > 0 ) {
140- return Array . from ( allFiles ) ;
134+ files . forEach ( f => addFileIfAllowed ( f ) ) ;
135+ } else {
136+ // 2. Original behavior fallback
137+ // Get tsconfig.json configuration
138+ const config = this . tsConfigCache . getTsConfig ( module . path ) ;
139+
140+ // Default: all files in tsconfig.json
141+ if ( config . fileNames && config . fileNames . length > 0 ) {
142+ config . fileNames . forEach ( file => {
143+ if ( fs . existsSync ( file ) ) {
144+ addFileIfAllowed ( file ) ;
145+ }
146+ } ) ;
141147 }
142- }
143-
144- // Fallback to rootDir and outDir
145- const searchDirs : string [ ] = [ ] ;
146- if ( config . rootDir ) {
147- searchDirs . push ( path . join ( module . path , config . rootDir ) ) ;
148- }
149- if ( config . outDir && ! ( options . noDist && config . outDir === 'dist' ) ) {
150- searchDirs . push ( path . join ( module . path , config . outDir ) ) ;
151- }
148+
149+ if ( allFiles . size === 0 ) {
150+ // Fallback to rootDir and outDir
151+ const searchDirs : string [ ] = [ ] ;
152+ if ( config . rootDir ) {
153+ searchDirs . push ( path . join ( module . path , config . rootDir ) ) ;
154+ }
155+ if ( config . outDir && ! ( options . noDist && config . outDir === 'dist' ) ) {
156+ searchDirs . push ( path . join ( module . path , config . outDir ) ) ;
157+ }
152158
153- // Default source directories
154- const defaultDirs = [ 'src' , 'lib' ] ;
155- if ( ! options . noDist ) {
156- defaultDirs . push ( 'dist' ) ;
157- }
158-
159- for ( const dir of defaultDirs ) {
160- const dirPath = path . join ( module . path , dir ) ;
161- if ( fs . existsSync ( dirPath ) && fs . statSync ( dirPath ) . isDirectory ( ) ) {
162- searchDirs . push ( dirPath ) ;
163- }
164- }
159+ // Default source directories
160+ const defaultDirs = [ 'src' , 'lib' ] ;
161+ if ( ! options . noDist ) {
162+ defaultDirs . push ( 'dist' ) ;
163+ }
164+
165+ for ( const dir of defaultDirs ) {
166+ const dirPath = path . join ( module . path , dir ) ;
167+ if ( fs . existsSync ( dirPath ) && fs . statSync ( dirPath ) . isDirectory ( ) ) {
168+ searchDirs . push ( dirPath ) ;
169+ }
170+ }
165171
166- // Find all files in the collected directories
167- for ( const dir of [ ...new Set ( searchDirs ) ] ) {
168- if ( fs . existsSync ( dir ) ) {
169- const files = this . findTypeScriptFiles ( dir , options ) ;
170- files . forEach ( f => allFiles . add ( f ) ) ;
172+ // Find all files in the collected directories
173+ for ( const dir of [ ...new Set ( searchDirs ) ] ) {
174+ if ( fs . existsSync ( dir ) ) {
175+ const files = this . findTypeScriptFiles ( dir , options ) ;
176+ files . forEach ( f => addFileIfAllowed ( f ) ) ;
177+ }
178+ }
171179 }
172180 }
173181
@@ -202,7 +210,12 @@ export class TypeScriptStructureAnalyzer {
202210 private findTypeScriptFiles ( dir : string , options : { noDist ?: boolean , srcPatterns ?: string [ ] } = { } ) : string [ ] {
203211 const files : string [ ] = [ ] ;
204212
205- function traverse ( currentDir : string ) {
213+ // Safety check: if the starting directory itself is node_modules, skip it
214+ if ( path . basename ( dir ) === 'node_modules' ) {
215+ return [ ] ;
216+ }
217+
218+ const traverse = ( currentDir : string ) => {
206219 if ( ! fs . existsSync ( currentDir ) ) return ;
207220
208221 const entries = fs . readdirSync ( currentDir , { withFileTypes : true } ) ;
@@ -228,7 +241,7 @@ export class TypeScriptStructureAnalyzer {
228241 }
229242 }
230243 }
231- }
244+ } ;
232245
233246 traverse ( dir ) ;
234247 return files ;
0 commit comments