6
6
"use strict" ;
7
7
8
8
/** @typedef {import("./Resolver").FileSystem } FileSystem */
9
- /** @typedef {import("./Resolver").Dirent } Dirent */
9
+ /** @typedef {import("./Resolver").ReaddirStringCallback } ReaddirStringCallback */
10
+ /** @typedef {import("./Resolver").StringCallback } StringCallback */
10
11
/** @typedef {import("./Resolver").SyncFileSystem } SyncFileSystem */
11
12
12
13
/**
@@ -26,12 +27,16 @@ function SyncAsyncFileSystemDecorator(fs) {
26
27
( arg , options , callback ) => {
27
28
let result ;
28
29
try {
29
- result = lstatSync . call ( fs , arg ) ;
30
+ result = /** @type {Function | undefined } */ ( callback )
31
+ ? lstatSync . call ( fs , arg , options )
32
+ : lstatSync . call ( fs , arg ) ;
30
33
} catch ( e ) {
31
- return ( callback || options ) ( e ) ;
34
+ return ( callback || options ) (
35
+ /** @type {NodeJS.ErrnoException | null } */ ( e )
36
+ ) ;
32
37
}
33
38
34
- ( callback || options ) ( null , result ) ;
39
+ ( callback || options ) ( null , /** @type { any } */ ( result ) ) ;
35
40
}
36
41
) ;
37
42
this . lstatSync =
@@ -45,11 +50,16 @@ function SyncAsyncFileSystemDecorator(fs) {
45
50
( arg , options , callback ) => {
46
51
let result ;
47
52
try {
48
- result = callback ? fs . statSync ( arg , options ) : fs . statSync ( arg ) ;
53
+ result = /** @type {Function | undefined } */ ( callback )
54
+ ? fs . statSync ( arg , options )
55
+ : fs . statSync ( arg ) ;
49
56
} catch ( e ) {
50
- return ( callback || options ) ( e ) ;
57
+ return ( callback || options ) (
58
+ /** @type {NodeJS.ErrnoException | null } */ ( e )
59
+ ) ;
51
60
}
52
- ( callback || options ) ( null , result ) ;
61
+
62
+ ( callback || options ) ( null , /** @type {any } */ ( result ) ) ;
53
63
}
54
64
) ;
55
65
this . statSync =
@@ -62,31 +72,48 @@ function SyncAsyncFileSystemDecorator(fs) {
62
72
( arg , options , callback ) => {
63
73
let result ;
64
74
try {
65
- result = callback
66
- ? fs . readdirSync ( arg , options )
75
+ result = /** @type {Function | undefined } */ ( callback )
76
+ ? fs . readdirSync (
77
+ arg ,
78
+ /** @type {Exclude<Parameters<FileSystem["readdir"]>[1], ReaddirStringCallback> } */
79
+ ( options )
80
+ )
67
81
: fs . readdirSync ( arg ) ;
68
82
} catch ( e ) {
69
- return ( callback || options ) ( e ) ;
83
+ return ( callback || options ) (
84
+ /** @type {NodeJS.ErrnoException | null } */ ( e )
85
+ ) ;
70
86
}
71
87
72
- ( callback || options ) ( null , result ) ;
88
+ ( callback || options ) ( null , /** @type { any } */ ( result ) ) ;
73
89
}
74
90
) ;
75
91
this . readdirSync =
76
92
/** @type {SyncFileSystem["readdirSync"] } */
77
- ( ( arg , options ) => fs . readdirSync ( arg , options ) ) ;
93
+ (
94
+ ( arg , options ) =>
95
+ fs . readdirSync (
96
+ arg ,
97
+ /** @type {Parameters<SyncFileSystem["readdirSync"]>[1] } */ ( options )
98
+ )
99
+ ) ;
78
100
79
101
this . readFile =
80
102
/** @type {FileSystem["readFile"] } */
81
103
(
82
104
( arg , options , callback ) => {
83
105
let result ;
84
106
try {
85
- result = fs . readFileSync ( arg ) ;
107
+ result = /** @type {Function | undefined } */ ( callback )
108
+ ? fs . readFileSync ( arg , options )
109
+ : fs . readFileSync ( arg ) ;
86
110
} catch ( e ) {
87
- return ( callback || options ) ( e ) ;
111
+ return ( callback || options ) (
112
+ /** @type {NodeJS.ErrnoException | null } */ ( e )
113
+ ) ;
88
114
}
89
- ( callback || options ) ( null , result ) ;
115
+
116
+ ( callback || options ) ( null , /** @type {any } */ ( result ) ) ;
90
117
}
91
118
) ;
92
119
this . readFileSync =
@@ -99,37 +126,55 @@ function SyncAsyncFileSystemDecorator(fs) {
99
126
( arg , options , callback ) => {
100
127
let result ;
101
128
try {
102
- result = fs . readlinkSync ( arg ) ;
129
+ result = /** @type {Function | undefined } */ ( callback )
130
+ ? fs . readlinkSync (
131
+ arg ,
132
+ /** @type {Exclude<Parameters<FileSystem["readlink"]>[1], StringCallback> } */
133
+ ( options )
134
+ )
135
+ : fs . readlinkSync ( arg ) ;
103
136
} catch ( e ) {
104
- return ( callback || options ) ( e ) ;
137
+ return ( callback || options ) (
138
+ /** @type {NodeJS.ErrnoException | null } */ ( e )
139
+ ) ;
105
140
}
106
- ( callback || options ) ( null , result ) ;
141
+
142
+ ( callback || options ) ( null , /** @type {any } */ ( result ) ) ;
107
143
}
108
144
) ;
109
145
this . readlinkSync =
110
146
/** @type {SyncFileSystem["readlinkSync"] } */
111
- ( ( arg , options ) => fs . readlinkSync ( arg , options ) ) ;
147
+ (
148
+ ( arg , options ) =>
149
+ fs . readlinkSync (
150
+ arg ,
151
+ /** @type {Parameters<SyncFileSystem["readlinkSync"]>[1] } */ ( options )
152
+ )
153
+ ) ;
112
154
113
155
this . readJson = undefined ;
114
156
this . readJsonSync = undefined ;
115
157
const readJsonSync = fs . readJsonSync ;
116
158
if ( readJsonSync ) {
117
159
this . readJson =
118
- /** @type {NonNullable< FileSystem["readJson"]> } */
160
+ /** @type {FileSystem["readJson"] } */
119
161
(
120
- ( arg , options , callback ) => {
162
+ ( arg , callback ) => {
121
163
let result ;
122
164
try {
123
165
result = readJsonSync . call ( fs , arg ) ;
124
166
} catch ( e ) {
125
- return ( callback || options ) ( e ) ;
167
+ return callback (
168
+ /** @type {NodeJS.ErrnoException | Error | null } */ ( e )
169
+ ) ;
126
170
}
127
- ( callback || options ) ( null , result ) ;
171
+
172
+ callback ( null , result ) ;
128
173
}
129
174
) ;
130
175
this . readJsonSync =
131
176
/** @type {SyncFileSystem["readJsonSync"] } */
132
- ( ( arg , options ) => readJsonSync . call ( fs , arg , options ) ) ;
177
+ ( arg => readJsonSync . call ( fs , arg ) ) ;
133
178
}
134
179
135
180
this . realpath = undefined ;
@@ -142,16 +187,34 @@ function SyncAsyncFileSystemDecorator(fs) {
142
187
( arg , options , callback ) => {
143
188
let result ;
144
189
try {
145
- result = realpathSync . call ( fs , arg ) ;
190
+ result = /** @type {Function | undefined } */ ( callback )
191
+ ? realpathSync . call (
192
+ fs ,
193
+ arg ,
194
+ /** @type {Exclude<Parameters<NonNullable<FileSystem["realpath"]>>[1], StringCallback> } */
195
+ ( options )
196
+ )
197
+ : realpathSync . call ( fs , arg ) ;
146
198
} catch ( e ) {
147
- return ( callback || options ) ( e ) ;
199
+ return ( callback || options ) (
200
+ /** @type {NodeJS.ErrnoException | null } */ ( e )
201
+ ) ;
148
202
}
149
- ( callback || options ) ( null , result ) ;
203
+
204
+ ( callback || options ) ( null , /** @type {any } */ ( result ) ) ;
150
205
}
151
206
) ;
152
207
this . realpathSync =
153
208
/** @type {SyncFileSystem["realpathSync"] } */
154
- ( ( arg , options ) => realpathSync . call ( fs , arg , options ) ) ;
209
+ (
210
+ ( arg , options ) =>
211
+ realpathSync . call (
212
+ fs ,
213
+ arg ,
214
+ /** @type {Parameters<NonNullable<SyncFileSystem["realpathSync"]>>[1] } */
215
+ ( options )
216
+ )
217
+ ) ;
155
218
}
156
219
}
157
220
module . exports = SyncAsyncFileSystemDecorator ;
0 commit comments