@@ -2,47 +2,59 @@ const fs = require('fs');
22const path = require ( 'path' ) ;
33
44const exts = [ '.js' , '.ts' , '.tsx' ] ;
5- const targetDirs = [ 'api' , 'ui' , 'upload-api' ] ;
5+ const targetDirs = [ 'api' ] ;
66
77function resolveImport ( importPath , fileDir ) {
8- // Absolute or relative path
9- let basePath = path . resolve ( fileDir , importPath ) ;
8+ const absPath = path . resolve ( fileDir , importPath ) ;
109
11- // 1. If import has extension, check directly
12- if ( / \. [ j t ] s x ? $ / . test ( importPath ) ) {
13- if ( fs . existsSync ( basePath ) && fs . statSync ( basePath ) . isFile ( ) ) {
10+ // 1. Check as-is (could be a file with or without extension)
11+ if ( fs . existsSync ( absPath ) && fs . statSync ( absPath ) . isFile ( ) ) {
12+ return true ;
13+ }
14+
15+ // 2. If import has extension, check for aliasing (.js <-> .ts, .jsx <-> .tsx)
16+ const extMatch = importPath . match ( / \. ( j s | j s x ) $ / ) ;
17+ if ( extMatch ) {
18+ const tsLike = extMatch [ 1 ] === 'js'
19+ ? importPath . replace ( / \. j s $ / , '.ts' )
20+ : importPath . replace ( / \. j s x $ / , '.tsx' ) ;
21+ const tsLikePath = path . resolve ( fileDir , tsLike ) ;
22+ if ( fs . existsSync ( tsLikePath ) && fs . statSync ( tsLikePath ) . isFile ( ) ) {
1423 return true ;
1524 }
16- } else {
17- // 2. Try with extensions
25+ }
26+
27+ // 3. Try with extensions
28+ for ( const ext of exts ) {
29+ const withExt = absPath + ext ;
30+ if ( fs . existsSync ( withExt ) && fs . statSync ( withExt ) . isFile ( ) ) {
31+ return true ;
32+ }
33+ }
34+
35+ // 4. Directory with index file
36+ if ( fs . existsSync ( absPath ) && fs . statSync ( absPath ) . isDirectory ( ) ) {
1837 for ( const ext of exts ) {
19- if ( fs . existsSync ( basePath + ext ) && fs . statSync ( basePath + ext ) . isFile ( ) ) {
38+ const idx = path . join ( absPath , 'index' + ext ) ;
39+ if ( fs . existsSync ( idx ) && fs . statSync ( idx ) . isFile ( ) ) {
2040 return true ;
2141 }
2242 }
23- // 3. Directory with index file
24- if ( fs . existsSync ( basePath ) && fs . statSync ( basePath ) . isDirectory ( ) ) {
25- for ( const ext of exts ) {
26- const idx = path . join ( basePath , 'index' + ext ) ;
27- if ( fs . existsSync ( idx ) && fs . statSync ( idx ) . isFile ( ) ) {
28- return true ;
29- }
30- }
31- }
3243 }
44+
3345 return false ;
3446}
3547
48+
49+
3650function cleanImportsInFile ( filePath ) {
3751 let code = fs . readFileSync ( filePath , 'utf-8' ) ;
3852 const fileDir = path . dirname ( filePath ) ;
3953
40- // Handles both regular and type-only imports
4154 const importRegex = / ^ i m p o r t \s + ( t y p e \s + ) ? [ \s \S ] * ?f r o m \s + [ ' " ] ( .* ?) [ ' " ] ; ? .* $ / gm;
4255
4356 let changed = false ;
4457 code = code . replace ( importRegex , ( match , typeKeyword , importPath ) => {
45- // Ignore node_modules and built-in modules
4658 if ( ! importPath . startsWith ( '.' ) && ! importPath . startsWith ( '/' ) ) return match ;
4759
4860 if ( ! resolveImport ( importPath , fileDir ) ) {
@@ -77,7 +89,6 @@ function walkDir(dir, callback) {
7789 } ) ;
7890}
7991
80- // Only process the specified folders
8192targetDirs . forEach ( folder => {
8293 const absPath = path . join ( process . cwd ( ) , folder ) ;
8394 walkDir ( absPath , cleanImportsInFile ) ;
0 commit comments