@@ -2,29 +2,47 @@ import fs from 'fs';
2
2
import path from 'path' ;
3
3
import ts from 'typescript' ;
4
4
import type { SourceFile , Files } from './read-write' ;
5
- import { extensionsUtil , separator } from './const' ;
5
+ import { ExtensionsUtil , separator } from './const' ;
6
6
import { asString } from './type' ;
7
7
8
- const formProperFilePath = ( {
9
- filePath ,
10
- } : Readonly < {
11
- filePath : string ;
12
- } > ) => {
13
- return filePath . split ( separator ) . filter ( Boolean ) . join ( separator ) ;
8
+ const formProperFilePath = (
9
+ props : Readonly < {
10
+ filePath : string ;
11
+ } >
12
+ ) => {
13
+ return props . filePath . split ( separator ) . filter ( Boolean ) . join ( separator ) ;
14
14
} ;
15
15
16
- const checkJavaScriptFileExistByAppend = ( {
17
- filePath,
18
- } : Readonly < {
19
- filePath : string ;
20
- } > ) => {
21
- const { js, mjs } = extensionsUtil ( ) . extensions ;
22
- if ( fs . existsSync ( `${ filePath } ${ js } ` ) ) {
23
- return js ;
16
+ const checkJavaScriptFileExistByAppend = (
17
+ props : Readonly < {
18
+ filePath : string ;
19
+ } >
20
+ ) => {
21
+ const { js } = ExtensionsUtil . extensions . javaScript ;
22
+
23
+ const jsFilePath = `${ props . filePath } ${ js } ` ;
24
+
25
+ if ( fs . existsSync ( jsFilePath ) ) {
26
+ return { extension : js , filePath : jsFilePath } ;
24
27
}
25
- if ( fs . existsSync ( `${ filePath } ${ mjs } ` ) ) {
26
- return mjs ;
28
+
29
+ return false ;
30
+ } ;
31
+
32
+ const checkTypeDefinitionFileExistByAppend = (
33
+ props : Readonly < {
34
+ filePath : string ;
35
+ } >
36
+ ) => {
37
+ const { js } = ExtensionsUtil . extensions . javaScript ;
38
+ const { dts } = ExtensionsUtil . extensions . typeDefinition ;
39
+
40
+ const dtsFilePath = `${ props . filePath } ${ dts } ` ;
41
+
42
+ if ( fs . existsSync ( dtsFilePath ) ) {
43
+ return { extension : js , filePath : dtsFilePath } ;
27
44
}
45
+
28
46
return false ;
29
47
} ;
30
48
@@ -36,66 +54,84 @@ const isDirectory = (filePath: string) => {
36
54
}
37
55
} ;
38
56
39
- const addJSExtension = ( {
40
- filePath,
41
- importPath,
42
- } : Readonly < {
43
- filePath : string ;
44
- importPath : string ;
45
- } > ) : Readonly <
46
- | {
47
- procedure : 'skip' ;
48
- }
49
- | {
50
- procedure : 'proceed' ;
51
- importPath : string ;
52
- filePathImported : string ;
53
- }
54
- > => {
55
- if ( extensionsUtil ( ) . matchAnyJs ( filePath ) ) {
56
- return {
57
- procedure : 'skip' ,
58
- } ;
59
- }
60
- if ( ! isDirectory ( filePath ) ) {
61
- const extension = checkJavaScriptFileExistByAppend ( {
62
- filePath,
57
+ const addJSExtensionConditionally = (
58
+ props : Readonly < {
59
+ filePath : string ;
60
+ importPath : string ;
61
+ checkType : 'dts' | 'js' ;
62
+ } >
63
+ ) => {
64
+ const check =
65
+ props . checkType === 'js'
66
+ ? checkJavaScriptFileExistByAppend
67
+ : checkTypeDefinitionFileExistByAppend ;
68
+
69
+ const skip = {
70
+ procedure : 'skip' ,
71
+ } as const ;
72
+
73
+ if ( ! isDirectory ( props . filePath ) ) {
74
+ const extensionResult = check ( {
75
+ filePath : props . filePath ,
63
76
} ) ;
64
- if ( ! extension ) {
65
- return {
66
- procedure : 'skip' ,
67
- } ;
77
+
78
+ if ( ! extensionResult ) {
79
+ return skip ;
68
80
}
81
+
69
82
return {
70
83
procedure : 'proceed' ,
84
+ absolutePath : extensionResult . filePath ,
71
85
importPath : formProperFilePath ( {
72
- filePath : `${ importPath } ${ extension } ` ,
86
+ filePath : `${ props . importPath } ${ extensionResult . extension } ` ,
73
87
} ) ,
74
- filePathImported : formProperFilePath ( {
75
- filePath : `${ filePath } ${ extension } ` ,
76
- } ) ,
77
- } ;
88
+ } as const ;
78
89
}
79
- const extension = checkJavaScriptFileExistByAppend ( {
80
- filePath : path . posix . join ( filePath , 'index' ) ,
90
+
91
+ const extensionResult = check ( {
92
+ filePath : path . posix . join ( props . filePath , 'index' ) ,
81
93
} ) ;
82
- if ( ! extension ) {
83
- return {
84
- procedure : 'skip' ,
85
- } ;
94
+
95
+ if ( ! extensionResult ) {
96
+ return skip ;
86
97
}
87
98
88
- const file = `index${ extension } ` ;
99
+ const file = `index${ extensionResult . extension } ` ;
89
100
90
101
return {
91
102
procedure : 'proceed' ,
103
+ absolutePath : extensionResult . filePath ,
92
104
importPath : formProperFilePath ( {
93
- filePath : [ importPath , separator , file ] . join ( '' ) ,
94
- } ) ,
95
- filePathImported : formProperFilePath ( {
96
- filePath : [ filePath , separator , file ] . join ( '' ) ,
105
+ filePath : [ props . importPath , separator , file ] . join ( '' ) ,
97
106
} ) ,
98
- } ;
107
+ } as const ;
108
+ } ;
109
+
110
+ const addJSExtension = (
111
+ props : Readonly < {
112
+ filePath : string ;
113
+ importPath : string ;
114
+ } >
115
+ ) : ReturnType < typeof addJSExtensionConditionally > => {
116
+ if ( ExtensionsUtil . matchJs ( props . filePath ) ) {
117
+ return {
118
+ procedure : 'skip' ,
119
+ } ;
120
+ }
121
+
122
+ const result = addJSExtensionConditionally ( {
123
+ ...props ,
124
+ checkType : 'js' ,
125
+ } ) ;
126
+
127
+ if ( result . procedure === 'proceed' ) {
128
+ return result ;
129
+ }
130
+
131
+ return addJSExtensionConditionally ( {
132
+ ...props ,
133
+ checkType : 'dts' ,
134
+ } ) ;
99
135
} ;
100
136
101
137
const traverseAndUpdateFileWithJSExtension = ( files : Files ) => {
@@ -125,8 +161,6 @@ const traverseAndUpdateFileWithJSExtension = (files: Files) => {
125
161
) ,
126
162
} ) ;
127
163
128
- const separator = '/' ;
129
-
130
164
const fileName = formProperFilePath ( {
131
165
filePath : ! moduleSpecifier . endsWith ( separator )
132
166
? moduleSpecifier
@@ -154,10 +188,12 @@ const traverseAndUpdateFileWithJSExtension = (files: Files) => {
154
188
switch ( result . procedure ) {
155
189
case 'proceed' : {
156
190
// if file name not included in list of js file read
157
- const { filePathImported , importPath } = result ;
191
+
158
192
if (
159
193
files . find ( ( file ) => {
160
- return file . endsWith ( filePathImported ) ;
194
+ return file . endsWith (
195
+ result . absolutePath
196
+ ) ;
161
197
} )
162
198
) {
163
199
const before = characters
@@ -174,7 +210,7 @@ const traverseAndUpdateFileWithJSExtension = (files: Files) => {
174
210
before,
175
211
after : before . replace (
176
212
moduleSpecifier ,
177
- importPath
213
+ result . importPath
178
214
) ,
179
215
} ,
180
216
] ;
@@ -184,6 +220,7 @@ const traverseAndUpdateFileWithJSExtension = (files: Files) => {
184
220
}
185
221
}
186
222
}
223
+
187
224
return [ ] ;
188
225
} ) ;
189
226
@@ -192,8 +229,8 @@ const traverseAndUpdateFileWithJSExtension = (files: Files) => {
192
229
: [
193
230
{
194
231
file : sourceFile . fileName ,
195
- code : replaceNodes . reduce ( ( prev , { before , after } ) => {
196
- return prev . replace ( before , after ) ;
232
+ code : replaceNodes . reduce ( ( code , node ) => {
233
+ return code . replace ( node . before , node . after ) ;
197
234
} , code ) ,
198
235
} ,
199
236
] ;
0 commit comments