Skip to content

Commit

Permalink
always return Dirents from readdir
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Mar 6, 2023
1 parent f923bb0 commit ba35d77
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 21 deletions.
15 changes: 10 additions & 5 deletions src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
// 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,
statSync,
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)
Expand All @@ -36,9 +39,11 @@ const mkdir = (
fs.mkdir(path, options, (er, made) => (er ? rej(er) : res(made)))
)

const readdir = (path: fs.PathLike): Promise<string[]> =>
new Promise((res, rej) =>
fs.readdir(path, (er, data) => (er ? rej(er) : res(data)))
const readdir = (path: fs.PathLike): Promise<Dirent[]> =>
new Promise<Dirent[]>((res, rej) =>
fs.readdir(path, { withFileTypes: true }, (er, data) =>
er ? rej(er) : res(data)
)
)

const rename = (oldPath: fs.PathLike, newPath: fs.PathLike): Promise<void> =>
Expand Down
5 changes: 3 additions & 2 deletions src/rimraf-move-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/rimraf-posix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/rimraf-windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 10 additions & 7 deletions test/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions test/readdir-or-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit ba35d77

Please sign in to comment.