@@ -22,8 +22,8 @@ import * as path from 'path'
2222import * as util from 'util'
2323
2424// windows cant find the right types
25- type Dir = any
26- type Dirent = any
25+ type Dir = FsType . Dir
26+ type Dirent = FsType . Dirent
2727
2828// using require here on purpose so we can override methods with any
2929// also even though imports are mutable in typescript the cognitive dissonance is too high because
@@ -111,7 +111,7 @@ export function patcher(roots: string[]): () => void {
111111 return origLstat ( ...args )
112112 }
113113
114- const cb = once ( args [ args . length - 1 ] as any )
114+ const cb = once ( args [ args . length - 1 ] as Function )
115115
116116 // override the callback
117117 args [ args . length - 1 ] = function lstatCb ( err : Error , stats : Stats ) {
@@ -207,7 +207,7 @@ export function patcher(roots: string[]): () => void {
207207 return origRealpath ( ...args )
208208 }
209209
210- const cb = once ( args [ args . length - 1 ] as any )
210+ const cb = once ( args [ args . length - 1 ] as Function )
211211
212212 args [ args . length - 1 ] = function realpathCb ( err : Error , str : string ) {
213213 if ( err ) return cb ( err )
@@ -230,7 +230,7 @@ export function patcher(roots: string[]): () => void {
230230 return origRealpathNative ( ...args )
231231 }
232232
233- const cb = once ( args [ args . length - 1 ] as any )
233+ const cb = once ( args [ args . length - 1 ] as Function )
234234
235235 args [ args . length - 1 ] = function nativeCb ( err : Error , str : string ) {
236236 if ( err ) return cb ( err )
@@ -277,7 +277,7 @@ export function patcher(roots: string[]): () => void {
277277 return origReadlink ( ...args )
278278 }
279279
280- const cb = once ( args [ args . length - 1 ] as any )
280+ const cb = once ( args [ args . length - 1 ] as Function )
281281
282282 args [ args . length - 1 ] = function readlinkCb ( err : Error , str : string ) {
283283 if ( err ) return cb ( err )
@@ -363,7 +363,7 @@ export function patcher(roots: string[]): () => void {
363363 return origReaddir ( ...args )
364364 }
365365
366- const cb = once ( args [ args . length - 1 ] as any )
366+ const cb = once ( args [ args . length - 1 ] as Function )
367367 const p = resolvePathLike ( args [ 0 ] )
368368
369369 args [ args . length - 1 ] = function readdirCb (
@@ -410,13 +410,13 @@ export function patcher(roots: string[]): () => void {
410410 // if this is not a function opendir should throw an error.
411411 // we call it so we don't have to throw a mock
412412 if ( typeof args [ args . length - 1 ] === 'function' ) {
413- const cb = once ( args [ args . length - 1 ] as any )
413+ const cb = once ( args [ args . length - 1 ] as Function )
414414 args [ args . length - 1 ] = async function opendirCb (
415415 err : Error ,
416416 dir : Dir
417417 ) {
418418 try {
419- cb ( null , handleDir ( dir ) )
419+ cb ( err , err ? undefined : handleDir ( dir ) )
420420 } catch ( err ) {
421421 cb ( err )
422422 }
@@ -460,7 +460,7 @@ export function patcher(roots: string[]): () => void {
460460 let unpatchPromises : Function
461461
462462 if ( promisePropertyDescriptor ) {
463- const promises : any = { }
463+ const promises : typeof fs . promises = { }
464464 promises . lstat = util . promisify ( fs . lstat )
465465 // NOTE: node core uses the newer realpath function fs.promises.native instead of fs.realPath
466466 promises . realpath = util . promisify ( fs . realpath . native )
@@ -523,19 +523,20 @@ export function patcher(roots: string[]): () => void {
523523
524524 const origRead = dir . read . bind ( dir )
525525 dir . read = async function handleDirRead (
526- ... args : Parameters < typeof dir . read >
526+ cb ? : Parameters < typeof dir . read > [ 0 ]
527527 ) {
528- if ( typeof args [ args . length - 1 ] === 'function' ) {
529- const cb = args [ args . length - 1 ] as Function
530- args [ args . length - 1 ] = async function handleDirReadCb (
531- err : Error ,
532- entry : Dirent
533- ) {
534- cb ( err , entry ? await handleDirent ( p , entry ) : null )
535- }
536- origRead ( ...args )
528+ if ( typeof cb === 'function' ) {
529+ origRead ( function handleDirReadCb ( err : Error , entry : Dirent ) {
530+ if ( err ) return cb ( err , null )
531+ handleDirent ( p , entry ) . then (
532+ ( ) => {
533+ cb ( null , entry )
534+ } ,
535+ ( err ) => cb ( err , null )
536+ )
537+ } )
537538 } else {
538- const entry = await origRead ( ... args )
539+ const entry = await origRead ( )
539540 if ( entry ) {
540541 await handleDirent ( p , entry )
541542 }
@@ -975,10 +976,10 @@ export function escapeFunction(_roots: string[]) {
975976 }
976977}
977978
978- function once < T > ( fn : ( ... args : unknown [ ] ) => T ) {
979+ function once ( fn : Function ) {
979980 let called = false
980981
981- return function callOnce ( ...args : unknown [ ] ) {
982+ return function callOnce ( ...args : any [ ] ) {
982983 if ( called ) return
983984 called = true
984985
0 commit comments