@@ -2,27 +2,46 @@ const fs = require('fs');
22const path = require ( 'path' ) ;
33
44const exts = [ '.js' , '.ts' , '.tsx' ] ;
5+ const targetDirs = [ 'api' , 'ui' , 'upload-api' ] ;
56
6- function fileExists ( importPath , fileDir ) {
7+ function resolveImport ( importPath , fileDir ) {
8+ let basePath = path . resolve ( fileDir , importPath ) ;
9+
10+ // 1. Direct file with extension
11+ if ( fs . existsSync ( basePath ) && fs . statSync ( basePath ) . isFile ( ) ) {
12+ return true ;
13+ }
14+
15+ // 2. Try with extensions
716 for ( const ext of exts ) {
8- let resolved = path . resolve ( fileDir , importPath ) ;
9- if ( ! resolved . endsWith ( ext ) ) resolved += ext ;
10- if ( fs . existsSync ( resolved ) ) return true ;
17+ if ( fs . existsSync ( basePath + ext ) ) {
18+ return true ;
19+ }
20+ }
21+
22+ // 3. Directory with index file
23+ if ( fs . existsSync ( basePath ) && fs . statSync ( basePath ) . isDirectory ( ) ) {
24+ for ( const ext of exts ) {
25+ if ( fs . existsSync ( path . join ( basePath , 'index' + ext ) ) ) {
26+ return true ;
27+ }
28+ }
1129 }
30+
1231 return false ;
1332}
1433
1534function cleanImportsInFile ( filePath ) {
1635 let code = fs . readFileSync ( filePath , 'utf-8' ) ;
1736 const fileDir = path . dirname ( filePath ) ;
18- const importRegex = / i m p o r t \s + .* ?f r o m \s + [ ' " ] ( .* ?) [ ' " ] ; ? / g;
37+
38+ const importRegex = / ^ i m p o r t \s + ( t y p e \s + ) ? [ \s \S ] * ?f r o m \s + [ ' " ] ( .* ?) [ ' " ] ; ? .* $ / gm;
1939
2040 let changed = false ;
21- code = code . replace ( importRegex , ( match , importPath ) => {
22- // Ignore node_modules and built-in modules
41+ code = code . replace ( importRegex , ( match , typeKeyword , importPath ) => {
2342 if ( ! importPath . startsWith ( '.' ) && ! importPath . startsWith ( '/' ) ) return match ;
2443
25- if ( ! fileExists ( importPath , fileDir ) ) {
44+ if ( ! resolveImport ( importPath , fileDir ) ) {
2645 console . log ( `Removing broken import in ${ filePath } : ${ match . trim ( ) } ` ) ;
2746 changed = true ;
2847 return '' ;
@@ -36,9 +55,14 @@ function cleanImportsInFile(filePath) {
3655}
3756
3857function walkDir ( dir , callback ) {
58+ if ( ! fs . existsSync ( dir ) ) return ;
3959 fs . readdirSync ( dir , { withFileTypes : true } ) . forEach ( dirent => {
4060 const entry = path . join ( dir , dirent . name ) ;
41- if ( dirent . isDirectory ( ) && dir !== 'node_modules' && ! dirent . name . startsWith ( '.' ) ) {
61+ if (
62+ dirent . isDirectory ( ) &&
63+ dirent . name !== 'node_modules' &&
64+ ! dirent . name . startsWith ( '.' )
65+ ) {
4266 walkDir ( entry , callback ) ;
4367 } else if (
4468 dirent . isFile ( ) &&
@@ -49,5 +73,8 @@ function walkDir(dir, callback) {
4973 } ) ;
5074}
5175
52- // Start from the repo root
53- walkDir ( process . cwd ( ) , cleanImportsInFile ) ;
76+ // Only process the specified folders
77+ targetDirs . forEach ( folder => {
78+ const absPath = path . join ( process . cwd ( ) , folder ) ;
79+ walkDir ( absPath , cleanImportsInFile ) ;
80+ } ) ;
0 commit comments