diff --git a/src/fs.ts b/src/fs.ts index 692298a1..3cafb66e 100644 --- a/src/fs.ts +++ b/src/fs.ts @@ -1,12 +1,11 @@ // promisify ourselves, because older nodes don't have fs.promises -import fs from 'fs' +import fs, { Dirent } from 'fs' // sync ones just take the sync version from node export { chmodSync, mkdirSync, - readdirSync, renameSync, rmdirSync, rmSync, @@ -14,6 +13,10 @@ export { unlinkSync, } from 'fs' +import { readdirSync as rdSync } from 'fs' +export const readdirSync = (path: fs.PathLike): Dirent[] => + rdSync(path, { withFileTypes: true }) + // unrolled for better inlining, this seems to get better performance // than something like: // const makeCb = (res, rej) => (er, ...d) => er ? rej(er) : res(...d) @@ -36,9 +39,11 @@ const mkdir = ( fs.mkdir(path, options, (er, made) => (er ? rej(er) : res(made))) ) -const readdir = (path: fs.PathLike): Promise => - new Promise((res, rej) => - fs.readdir(path, (er, data) => (er ? rej(er) : res(data))) +const readdir = (path: fs.PathLike): Promise => + new Promise((res, rej) => + fs.readdir(path, { withFileTypes: true }, (er, data) => + er ? rej(er) : res(data) + ) ) const rename = (oldPath: fs.PathLike, newPath: fs.PathLike): Promise => diff --git a/src/rimraf-move-remove.ts b/src/rimraf-move-remove.ts index 2cac797b..73f08de7 100644 --- a/src/rimraf-move-remove.ts +++ b/src/rimraf-move-remove.ts @@ -100,7 +100,7 @@ export const rimrafMoveRemove = async ( const removedAll = ( await Promise.all( - entries.map(entry => rimrafMoveRemove(resolve(path, entry), opt)) + entries.map(entry => rimrafMoveRemove(resolve(path, entry.name), opt)) ) ).reduce((a, b) => a && b, true) if (!removedAll) { @@ -163,7 +163,8 @@ export const rimrafMoveRemoveSync = ( let removedAll = true for (const entry of entries) { - removedAll = rimrafMoveRemoveSync(resolve(path, entry), opt) && removedAll + removedAll = + rimrafMoveRemoveSync(resolve(path, entry.name), opt) && removedAll } if (!removedAll) { return false diff --git a/src/rimraf-posix.ts b/src/rimraf-posix.ts index a4a4c69a..cb109037 100644 --- a/src/rimraf-posix.ts +++ b/src/rimraf-posix.ts @@ -39,7 +39,7 @@ export const rimrafPosix = async ( const removedAll = ( await Promise.all( - entries.map(entry => rimrafPosix(resolve(path, entry), opt)) + entries.map(entry => rimrafPosix(resolve(path, entry.name), opt)) ) ).reduce((a, b) => a && b, true) @@ -85,7 +85,7 @@ export const rimrafPosixSync = ( } let removedAll: boolean = true for (const entry of entries) { - removedAll = rimrafPosixSync(resolve(path, entry), opt) && removedAll + removedAll = rimrafPosixSync(resolve(path, entry.name), opt) && removedAll } if (opt.preserveRoot === false && path === parse(path).root) { return false diff --git a/src/rimraf-windows.ts b/src/rimraf-windows.ts index 8582a1b5..2b9b1296 100644 --- a/src/rimraf-windows.ts +++ b/src/rimraf-windows.ts @@ -100,7 +100,7 @@ export const rimrafWindows = async ( const s = state === START ? CHILD : state const removedAll = ( await Promise.all( - entries.map(entry => rimrafWindows(resolve(path, entry), opt, s)) + entries.map(entry => rimrafWindows(resolve(path, entry.name), opt, s)) ) ).reduce((a, b) => a && b, true) @@ -149,7 +149,7 @@ export const rimrafWindowsSync = ( let removedAll = true for (const entry of entries) { const s = state === START ? CHILD : state - removedAll = rimrafWindowsSync(resolve(path, entry), opt, s) && removedAll + removedAll = rimrafWindowsSync(resolve(path, entry.name), opt, s) && removedAll } if (state === START) { diff --git a/test/bin.js b/test/bin.js index aa0f5513..8199d2ec 100644 --- a/test/bin.js +++ b/test/bin.js @@ -341,7 +341,7 @@ t.test('interactive deletes', t => { child.stderr.setEncoding('utf8') let last = '' child.stdout.on('data', async c => { - await new Promise(r => setTimeout(r, 50)) + // await new Promise(r => setTimeout(r, 50)) out.push(c.trim()) const s = script.shift() if (s !== undefined) { diff --git a/test/fs.ts b/test/fs.ts index c74a1a46..8d9cd0fb 100644 --- a/test/fs.ts +++ b/test/fs.ts @@ -28,19 +28,22 @@ for (const method of Object.keys( fs.promises as { [k: string]: (...a: any[]) => any } )) { // of course fs.rm is missing when we shouldn't use native :) + // also, readdirSync is clubbed to always return file types if (method !== 'rm' || useNative()) { t.type( (realFS as { [k: string]: any })[method], Function, `real fs.${method} is a function` ) - t.equal( - (fs as { [k: string]: any })[`${method}Sync`], - (realFS as unknown as { [k: string]: (...a: any[]) => any })[ - `${method}Sync` - ], - `has ${method}Sync` - ) + if (method !== 'readdir') { + t.equal( + (fs as { [k: string]: any })[`${method}Sync`], + (realFS as unknown as { [k: string]: (...a: any[]) => any })[ + `${method}Sync` + ], + `has ${method}Sync` + ) + } } // set up our pass/fails for the next tests diff --git a/test/readdir-or-error.js b/test/readdir-or-error.js index 251bb867..3eb1ca29 100644 --- a/test/readdir-or-error.js +++ b/test/readdir-or-error.js @@ -27,8 +27,12 @@ for (const [c, expect] of cases) { const resAsync = await readdirOrError(p) const resSync = readdirOrErrorSync(p) if (Array.isArray(expect)) { - t.same(resAsync.sort(), expect.sort(), 'got async result') - t.same(resSync.sort(), expect.sort(), 'got sync result') + t.same( + resAsync.map(e => e.name).sort(), + expect.sort(), + 'got async result' + ) + t.same(resSync.map(e => e.name).sort(), expect.sort(), 'got sync result') } else { t.match(resAsync, expect, 'got async result') t.match(resSync, expect, 'got sync result')