@@ -95,6 +95,12 @@ function innerReadDirNames(dirents, options) {
95
95
return sort ? names . sort ( naturalCompare ) : names
96
96
}
97
97
98
+ /*@__NO_SIDE_EFFECTS__ */
99
+ function isDirSync ( filepath ) {
100
+ const fs = getFs ( )
101
+ return fs . existsSync ( filepath ) && ! ! safeStatsSync ( filepath ) ?. isDirectory ( )
102
+ }
103
+
98
104
/*@__NO_SIDE_EFFECTS__ */
99
105
function isDirEmptySync ( dirname , options ) {
100
106
const { ignore = defaultIgnore } = { __proto__ : null , ...options }
@@ -148,7 +154,10 @@ async function readDirNames(dirname, options) {
148
154
const fs = getFs ( )
149
155
try {
150
156
return innerReadDirNames (
151
- await fs . promises . readdir ( dirname , { withFileTypes : true } ) ,
157
+ await fs . promises . readdir ( dirname , {
158
+ __proto__ : null ,
159
+ withFileTypes : true
160
+ } ) ,
152
161
options
153
162
)
154
163
} catch { }
@@ -160,29 +169,48 @@ function readDirNamesSync(dirname, options) {
160
169
const fs = getFs ( )
161
170
try {
162
171
return innerReadDirNames (
163
- fs . readdirSync ( dirname , { withFileTypes : true } ) ,
172
+ fs . readdirSync ( dirname , { __proto__ : null , withFileTypes : true } ) ,
164
173
options
165
174
)
166
175
} catch { }
167
176
return [ ]
168
177
}
169
178
179
+ /*@__NO_SIDE_EFFECTS__ */
180
+ async function readFileBinary ( filepath , options ) {
181
+ const fs = getFs ( )
182
+ return await fs . promises . readFile ( filepath , {
183
+ signal : /*@__PURE__ */ require ( './constants/abort-signal' ) ,
184
+ ...options ,
185
+ encoding : 'binary'
186
+ } )
187
+ }
188
+
189
+ /*@__NO_SIDE_EFFECTS__ */
190
+ async function readFileUtf8 ( filepath , options ) {
191
+ const fs = getFs ( )
192
+ return await fs . promises . readFile ( filepath , {
193
+ signal : /*@__PURE__ */ require ( './constants/abort-signal' ) ,
194
+ ...options ,
195
+ encoding : 'utf8'
196
+ } )
197
+ }
198
+
170
199
/*@__NO_SIDE_EFFECTS__ */
171
200
async function readJson ( filepath , options ) {
172
201
if ( typeof options === 'string' ) {
173
202
options = { encoding : options }
174
203
}
175
- const { reviver, throws, ...fsOptionsRaw } = { __proto__ : null , ...options }
176
- const fsOptions = {
177
- __proto__ : null ,
178
- encoding : 'utf8' ,
179
- ...fsOptionsRaw
180
- }
204
+ const { reviver, throws, ...fsOptions } = { __proto__ : null , ...options }
181
205
const fs = getFs ( )
182
206
const shouldThrow = throws === undefined || ! ! throws
183
207
return parse (
184
208
filepath ,
185
- await fs . promises . readFile ( filepath , fsOptions ) ,
209
+ await fs . promises . readFile ( filepath , {
210
+ __proto__ : null ,
211
+ encoding : 'utf8' ,
212
+ ...fsOptions
213
+ } ) ,
186
214
reviver ,
187
215
shouldThrow
188
216
)
@@ -193,17 +221,16 @@ function readJsonSync(filepath, options) {
193
221
if ( typeof options === 'string' ) {
194
222
options = { encoding : options }
195
223
}
196
- const { reviver, throws, ...fsOptionsRaw } = { __proto__ : null , ...options }
197
- const fsOptions = {
198
- __proto__ : null ,
199
- encoding : 'utf8' ,
200
- ...fsOptionsRaw
201
- }
224
+ const { reviver, throws, ...fsOptions } = { __proto__ : null , ...options }
202
225
const fs = getFs ( )
203
226
const shouldThrow = throws === undefined || ! ! throws
204
227
return parse (
205
228
filepath ,
206
- fs . readFileSync ( filepath , fsOptions ) ,
229
+ fs . readFileSync ( filepath , {
230
+ __proto__ : null ,
231
+ encoding : 'utf8' ,
232
+ ...fsOptions
233
+ } ) ,
207
234
reviver ,
208
235
shouldThrow
209
236
)
@@ -231,6 +258,45 @@ function removeSync(filepath, options) {
231
258
} )
232
259
}
233
260
261
+ /*@__NO_SIDE_EFFECTS__ */
262
+ async function safeReadFile ( filepath , options ) {
263
+ const fs = getFs ( )
264
+ try {
265
+ return await fs . promises . readFile ( filepath , {
266
+ encoding : 'utf8' ,
267
+ signal : /*@__PURE__ */ require ( './constants/abort-signal' ) ,
268
+ ...( typeof options === 'string' ? { encoding : options } : options )
269
+ } )
270
+ } catch { }
271
+ return undefined
272
+ }
273
+
274
+ /*@__NO_SIDE_EFFECTS__ */
275
+ function safeStatsSync ( filepath , options ) {
276
+ const fs = getFs ( )
277
+ try {
278
+ return fs . statSync ( filepath , {
279
+ __proto__ : null ,
280
+ throwIfNoEntry : false ,
281
+ ...options
282
+ } )
283
+ } catch { }
284
+ return undefined
285
+ }
286
+
287
+ /*@__NO_SIDE_EFFECTS__ */
288
+ function safeReadFileSync ( filepath , options ) {
289
+ const fs = getFs ( )
290
+ try {
291
+ return fs . readFileSync ( filepath , {
292
+ __proto__ : null ,
293
+ encoding : 'utf8' ,
294
+ ...( typeof options === 'string' ? { encoding : options } : options )
295
+ } )
296
+ } catch { }
297
+ return undefined
298
+ }
299
+
234
300
/*@__NO_SIDE_EFFECTS__ */
235
301
function stringify (
236
302
json ,
@@ -261,48 +327,52 @@ async function writeJson(filepath, json, options) {
261
327
if ( typeof options === 'string' ) {
262
328
options = { encoding : options }
263
329
}
264
- const { EOL , finalEOL, replacer, spaces, ...fsOptionsRaw } = {
330
+ const { EOL , finalEOL, replacer, spaces, ...fsOptions } = {
265
331
__proto__ : null ,
266
332
...options
267
333
}
268
- const fsOptions = {
269
- __proto__ : null ,
270
- encoding : 'utf8' ,
271
- ...fsOptionsRaw
272
- }
273
334
const fs = getFs ( )
274
335
const str = stringify ( json , EOL , finalEOL , replacer , spaces )
275
- await fs . promises . writeFile ( filepath , str , fsOptions )
336
+ await fs . promises . writeFile ( filepath , str , {
337
+ __proto__ : null ,
338
+ encoding : 'utf8' ,
339
+ ...fsOptions
340
+ } )
276
341
}
277
342
278
343
/*@__NO_SIDE_EFFECTS__ */
279
344
function writeJsonSync ( filepath , json , options ) {
280
345
if ( typeof options === 'string' ) {
281
346
options = { encoding : options }
282
347
}
283
- const { EOL , finalEOL, replacer, spaces, ...fsOptionsRaw } = {
348
+ const { EOL , finalEOL, replacer, spaces, ...fsOptions } = {
284
349
__proto__ : null ,
285
350
...options
286
351
}
287
- const fsOptions = {
288
- __proto__ : null ,
289
- encoding : 'utf8' ,
290
- ...fsOptionsRaw
291
- }
292
352
const fs = getFs ( )
293
353
const str = stringify ( json , EOL , finalEOL , replacer , spaces )
294
- fs . writeFileSync ( filepath , str , fsOptions )
354
+ fs . writeFileSync ( filepath , str , {
355
+ __proto__ : null ,
356
+ encoding : 'utf8' ,
357
+ ...fsOptions
358
+ } )
295
359
}
296
360
297
361
module . exports = {
362
+ isDirSync,
298
363
isDirEmptySync,
299
364
isSymLinkSync,
365
+ readFileBinary,
366
+ readFileUtf8,
300
367
readJson,
301
368
readJsonSync,
302
369
readDirNames,
303
370
readDirNamesSync,
304
371
remove,
305
372
removeSync,
373
+ safeReadFile,
374
+ safeReadFileSync,
375
+ safeStatsSync,
306
376
uniqueSync,
307
377
writeJson,
308
378
writeJsonSync
0 commit comments