Skip to content

Commit 711c4bd

Browse files
authored
fix: don't include filename in path when calling readdir with withFileTypes: true (#1024)
* fix: readdir withFileType path bug * fix: readdir(recursive) normalize to unix filepath sep. in filenames * fix: fullPath normalization inplace * fix: formatting * fix: remove debugging console statement
1 parent 2612163 commit 711c4bd

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

src/Dirent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class Dirent implements IDirent {
1515

1616
dirent.name = strToEncoding(link.getName(), encoding);
1717
dirent.mode = mode;
18-
dirent.path = link.getPath();
18+
dirent.path = link.getParentPath();
1919

2020
return dirent;
2121
}

src/__tests__/volume/readdirSync.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ describe('readdirSync()', () => {
6767
return { ...dirent };
6868
});
6969
expect(mapped).toEqual([
70-
{ mode: 33206, name: 'af', path: '/x/af' },
71-
{ mode: 16895, name: 'b', path: '/x/b' },
72-
{ mode: 16895, name: 'c', path: '/x/c' },
70+
{ mode: 33206, name: 'af', path: '/x' },
71+
{ mode: 16895, name: 'b', path: '/x' },
72+
{ mode: 16895, name: 'c', path: '/x' },
7373
]);
7474
});
7575

@@ -105,16 +105,16 @@ describe('readdirSync()', () => {
105105
})
106106
.sort((a, b) => a.path.localeCompare(b.path));
107107
expect(mapped).toEqual([
108-
{ mode: 33206, name: 'af1', path: '/z/af1' },
109-
{ mode: 33206, name: 'af2', path: '/z/af2' },
110-
{ mode: 16895, name: 'b', path: '/z/b' },
111-
{ mode: 33206, name: 'bf1', path: '/z/b/bf1' },
112-
{ mode: 33206, name: 'bf2', path: '/z/b/bf2' },
108+
{ mode: 33206, name: 'af1', path: '/z' },
109+
{ mode: 33206, name: 'af2', path: '/z' },
110+
{ mode: 16895, name: 'b', path: '/z' },
111+
{ mode: 16895, name: 'c', path: '/z' },
112+
{ mode: 33206, name: 'bf1', path: '/z/b' },
113+
{ mode: 33206, name: 'bf2', path: '/z/b' },
113114
{ mode: 16895, name: 'c', path: '/z/c' },
114-
{ mode: 16895, name: 'c', path: '/z/c/c' },
115-
{ mode: 33206, name: '.cf0', path: '/z/c/c/.cf0' },
116-
{ mode: 33206, name: 'cf1', path: '/z/c/c/cf1' },
117-
{ mode: 33206, name: 'cf2', path: '/z/c/c/cf2' },
115+
{ mode: 33206, name: '.cf0', path: '/z/c/c' },
116+
{ mode: 33206, name: 'cf1', path: '/z/c/c' },
117+
{ mode: 33206, name: 'cf2', path: '/z/c/c' },
118118
]);
119119
});
120120
});

src/node.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,10 @@ export class Link extends EventEmitter {
409409
return this.steps.join(SEP);
410410
}
411411

412+
getParentPath(): string {
413+
return this.steps.slice(0, -1).join(SEP);
414+
}
415+
412416
getName(): string {
413417
return this.steps[this.steps.length - 1];
414418
}

src/volume.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,11 @@ export class Volume implements FsCallbackApi, FsSynchronousApi {
14961496

14971497
return list.map(dirent => {
14981498
if (options.recursive) {
1499-
return dirent.path.replace(filename2 + pathModule.posix.sep, '');
1499+
let fullPath = pathModule.join(dirent.path, dirent.name.toString());
1500+
if (isWin) {
1501+
fullPath = fullPath.replace(/\\/g, '/');
1502+
}
1503+
return fullPath.replace(filename2 + pathModule.posix.sep, '');
15001504
}
15011505
return dirent.name;
15021506
});

0 commit comments

Comments
 (0)