Skip to content

Commit ba35d77

Browse files
committed
always return Dirents from readdir
1 parent f923bb0 commit ba35d77

File tree

7 files changed

+34
-21
lines changed

7 files changed

+34
-21
lines changed

src/fs.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
// promisify ourselves, because older nodes don't have fs.promises
22

3-
import fs from 'fs'
3+
import fs, { Dirent } from 'fs'
44

55
// sync ones just take the sync version from node
66
export {
77
chmodSync,
88
mkdirSync,
9-
readdirSync,
109
renameSync,
1110
rmdirSync,
1211
rmSync,
1312
statSync,
1413
unlinkSync,
1514
} from 'fs'
1615

16+
import { readdirSync as rdSync } from 'fs'
17+
export const readdirSync = (path: fs.PathLike): Dirent[] =>
18+
rdSync(path, { withFileTypes: true })
19+
1720
// unrolled for better inlining, this seems to get better performance
1821
// than something like:
1922
// const makeCb = (res, rej) => (er, ...d) => er ? rej(er) : res(...d)
@@ -36,9 +39,11 @@ const mkdir = (
3639
fs.mkdir(path, options, (er, made) => (er ? rej(er) : res(made)))
3740
)
3841

39-
const readdir = (path: fs.PathLike): Promise<string[]> =>
40-
new Promise((res, rej) =>
41-
fs.readdir(path, (er, data) => (er ? rej(er) : res(data)))
42+
const readdir = (path: fs.PathLike): Promise<Dirent[]> =>
43+
new Promise<Dirent[]>((res, rej) =>
44+
fs.readdir(path, { withFileTypes: true }, (er, data) =>
45+
er ? rej(er) : res(data)
46+
)
4247
)
4348

4449
const rename = (oldPath: fs.PathLike, newPath: fs.PathLike): Promise<void> =>

src/rimraf-move-remove.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export const rimrafMoveRemove = async (
100100

101101
const removedAll = (
102102
await Promise.all(
103-
entries.map(entry => rimrafMoveRemove(resolve(path, entry), opt))
103+
entries.map(entry => rimrafMoveRemove(resolve(path, entry.name), opt))
104104
)
105105
).reduce((a, b) => a && b, true)
106106
if (!removedAll) {
@@ -163,7 +163,8 @@ export const rimrafMoveRemoveSync = (
163163

164164
let removedAll = true
165165
for (const entry of entries) {
166-
removedAll = rimrafMoveRemoveSync(resolve(path, entry), opt) && removedAll
166+
removedAll =
167+
rimrafMoveRemoveSync(resolve(path, entry.name), opt) && removedAll
167168
}
168169
if (!removedAll) {
169170
return false

src/rimraf-posix.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const rimrafPosix = async (
3939

4040
const removedAll = (
4141
await Promise.all(
42-
entries.map(entry => rimrafPosix(resolve(path, entry), opt))
42+
entries.map(entry => rimrafPosix(resolve(path, entry.name), opt))
4343
)
4444
).reduce((a, b) => a && b, true)
4545

@@ -85,7 +85,7 @@ export const rimrafPosixSync = (
8585
}
8686
let removedAll: boolean = true
8787
for (const entry of entries) {
88-
removedAll = rimrafPosixSync(resolve(path, entry), opt) && removedAll
88+
removedAll = rimrafPosixSync(resolve(path, entry.name), opt) && removedAll
8989
}
9090
if (opt.preserveRoot === false && path === parse(path).root) {
9191
return false

src/rimraf-windows.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export const rimrafWindows = async (
100100
const s = state === START ? CHILD : state
101101
const removedAll = (
102102
await Promise.all(
103-
entries.map(entry => rimrafWindows(resolve(path, entry), opt, s))
103+
entries.map(entry => rimrafWindows(resolve(path, entry.name), opt, s))
104104
)
105105
).reduce((a, b) => a && b, true)
106106

@@ -149,7 +149,7 @@ export const rimrafWindowsSync = (
149149
let removedAll = true
150150
for (const entry of entries) {
151151
const s = state === START ? CHILD : state
152-
removedAll = rimrafWindowsSync(resolve(path, entry), opt, s) && removedAll
152+
removedAll = rimrafWindowsSync(resolve(path, entry.name), opt, s) && removedAll
153153
}
154154

155155
if (state === START) {

test/bin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ t.test('interactive deletes', t => {
341341
child.stderr.setEncoding('utf8')
342342
let last = ''
343343
child.stdout.on('data', async c => {
344-
await new Promise(r => setTimeout(r, 50))
344+
// await new Promise(r => setTimeout(r, 50))
345345
out.push(c.trim())
346346
const s = script.shift()
347347
if (s !== undefined) {

test/fs.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@ for (const method of Object.keys(
2828
fs.promises as { [k: string]: (...a: any[]) => any }
2929
)) {
3030
// of course fs.rm is missing when we shouldn't use native :)
31+
// also, readdirSync is clubbed to always return file types
3132
if (method !== 'rm' || useNative()) {
3233
t.type(
3334
(realFS as { [k: string]: any })[method],
3435
Function,
3536
`real fs.${method} is a function`
3637
)
37-
t.equal(
38-
(fs as { [k: string]: any })[`${method}Sync`],
39-
(realFS as unknown as { [k: string]: (...a: any[]) => any })[
40-
`${method}Sync`
41-
],
42-
`has ${method}Sync`
43-
)
38+
if (method !== 'readdir') {
39+
t.equal(
40+
(fs as { [k: string]: any })[`${method}Sync`],
41+
(realFS as unknown as { [k: string]: (...a: any[]) => any })[
42+
`${method}Sync`
43+
],
44+
`has ${method}Sync`
45+
)
46+
}
4447
}
4548

4649
// set up our pass/fails for the next tests

test/readdir-or-error.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ for (const [c, expect] of cases) {
2727
const resAsync = await readdirOrError(p)
2828
const resSync = readdirOrErrorSync(p)
2929
if (Array.isArray(expect)) {
30-
t.same(resAsync.sort(), expect.sort(), 'got async result')
31-
t.same(resSync.sort(), expect.sort(), 'got sync result')
30+
t.same(
31+
resAsync.map(e => e.name).sort(),
32+
expect.sort(),
33+
'got async result'
34+
)
35+
t.same(resSync.map(e => e.name).sort(), expect.sort(), 'got sync result')
3236
} else {
3337
t.match(resAsync, expect, 'got async result')
3438
t.match(resSync, expect, 'got sync result')

0 commit comments

Comments
 (0)