GeezerVison is showing github.com — fs: introduce `dirent.parentPath` · nodejs/node@32acafe
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/fs.md +14-1Lines changed: 14 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -6614,6 +6614,19 @@ The file name that this {fs.Dirent} object refers to. The type of this | 6614 | 6614 | value is determined by the `options.encoding` passed to [`fs.readdir()`][] or | 6615 | 6615 | [`fs.readdirSync()`][]. | 6616 | 6616 |
| | 6617 | +#### `dirent.parentPath` | | 6618 | +
| | 6619 | +<!-- YAML | | 6620 | +added: | | 6621 | + - REPLACEME | | 6622 | +--> | | 6623 | +
| | 6624 | +> Stability: 1 – Experimental | | 6625 | +
| | 6626 | +* {string} | | 6627 | +
| | 6628 | +The path to the parent directory of the file this {fs.Dirent} object refers to. | | 6629 | +
| 6617 | 6630 | #### `dirent.path` | 6618 | 6631 |
| 6619 | 6632 | <!-- YAML | @@ -6624,7 +6637,7 @@ added: | 6624 | 6637 |
| 6625 | 6638 | * {string} | 6626 | 6639 |
| 6627 | | -The base path that this {fs.Dirent} object refers to. | | 6640 | +Alias for `dirent.parentPath`. | 6628 | 6641 |
| 6629 | 6642 | ### Class: `fs.FSWatcher` | 6630 | 6643 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1426,7 +1426,7 @@ function readdirSyncRecursive(basePath, options) { | 1426 | 1426 | const dirent = getDirent(path, readdirResult[0][i], readdirResult[1][i]); | 1427 | 1427 | ArrayPrototypePush(readdirResults, dirent); | 1428 | 1428 | if (dirent.isDirectory()) { | 1429 | | - ArrayPrototypePush(pathsQueue, pathModule.join(dirent.path, dirent.name)); | | 1429 | + ArrayPrototypePush(pathsQueue, pathModule.join(dirent.parentPath, dirent.name)); | 1430 | 1430 | } | 1431 | 1431 | } | 1432 | 1432 | } else { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/dir.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -161,7 +161,7 @@ class Dir { | 161 | 161 | } | 162 | 162 |
| 163 | 163 | readSyncRecursive(dirent) { | 164 | | - const path = pathModule.join(dirent.path, dirent.name); | | 164 | + const path = pathModule.join(dirent.parentPath, dirent.name); | 165 | 165 | const ctx = { path }; | 166 | 166 | const handle = dirBinding.opendir( | 167 | 167 | pathModule.toNamespacedPath(path), | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/utils.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -165,6 +165,7 @@ function assertEncoding(encoding) { | 165 | 165 | class Dirent { | 166 | 166 | constructor(name, type, path) { | 167 | 167 | this.name = name; | | 168 | + this.parentPath = path; | 168 | 169 | this.path = path; | 169 | 170 | this[kType] = type; | 170 | 171 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-opendir.js +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,9 +48,11 @@ const invalidCallbackObj = { | 48 | 48 | const entries = files.map(() => { | 49 | 49 | const dirent = dir.readSync(); | 50 | 50 | assertDirent(dirent); | 51 | | - return dirent.name; | 52 | | - }); | 53 | | - assert.deepStrictEqual(files, entries.sort()); | | 51 | + return { name: dirent.name, path: dirent.path, parentPath: dirent.parentPath, toString() { return dirent.name; } }; | | 52 | + }).sort(); | | 53 | + assert.deepStrictEqual(entries.map((d) => d.name), files); | | 54 | + assert.deepStrictEqual(entries.map((d) => d.path), Array(entries.length).fill(testDir)); | | 55 | + assert.deepStrictEqual(entries.map((d) => d.parentPath), Array(entries.length).fill(testDir)); | 54 | 56 |
| 55 | 57 | // dir.read should return null when no more entries exist | 56 | 58 | assert.strictEqual(dir.readSync(), null); | |
|