GeezerVison is showing github.com — fs: introduce `dirent.parentPath` · nodejs/node@26e8f77
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 +13Lines changed: 13 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -6437,6 +6437,19 @@ The file name that this {fs.Dirent} object refers to. The type of this | 6437 | 6437 | value is determined by the `options.encoding` passed to [`fs.readdir()`][] or | 6438 | 6438 | [`fs.readdirSync()`][]. | 6439 | 6439 |
| | 6440 | +#### `dirent.parentPath` | | 6441 | +
| | 6442 | +<!-- YAML | | 6443 | +added: | | 6444 | + - REPLACEME | | 6445 | +--> | | 6446 | +
| | 6447 | +> Stability: 1 – Experimental | | 6448 | +
| | 6449 | +* {string} | | 6450 | +
| | 6451 | +The path to the parent directory of the file this {fs.Dirent} object refers to. | | 6452 | +
| 6440 | 6453 | #### `dirent.path` | 6441 | 6454 |
| 6442 | 6455 | <!-- YAML | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/dir.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -152,9 +152,10 @@ class Dir { | 152 | 152 | ArrayPrototypePush( | 153 | 153 | this[kDirBufferedEntries], | 154 | 154 | getDirent( | 155 | | - pathModule.join(path, result[i]), | | 155 | + path, | 156 | 156 | result[i], | 157 | 157 | result[i + 1], | | 158 | + true, // Quirk to not introduce a breaking change. | 158 | 159 | ), | 159 | 160 | ); | 160 | 161 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/utils.js +14-9Lines changed: 14 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -161,9 +161,10 @@ function assertEncoding(encoding) { | 161 | 161 | } | 162 | 162 |
| 163 | 163 | class Dirent { | 164 | | - constructor(name, type, path) { | | 164 | + constructor(name, type, path, filepath = path && join(path, name)) { | 165 | 165 | this.name = name; | 166 | | - this.path = path; | | 166 | + this.parentPath = path; | | 167 | + this.path = filepath; | 167 | 168 | this[kType] = type; | 168 | 169 | } | 169 | 170 |
| @@ -197,8 +198,8 @@ class Dirent { | 197 | 198 | } | 198 | 199 |
| 199 | 200 | class DirentFromStats extends Dirent { | 200 | | - constructor(name, stats, path) { | 201 | | - super(name, null, path); | | 201 | + constructor(name, stats, path, filepath) { | | 202 | + super(name, null, path, filepath); | 202 | 203 | this[kStats] = stats; | 203 | 204 | } | 204 | 205 | } | @@ -233,7 +234,7 @@ function join(path, name) { | 233 | 234 | } | 234 | 235 |
| 235 | 236 | if (typeof path === 'string' && typeof name === 'string') { | 236 | | - return pathModule.basename(path) === name ? path : pathModule.join(path, name); | | 237 | + return pathModule.join(path, name); | 237 | 238 | } | 238 | 239 |
| 239 | 240 | if (isUint8Array(path) && isUint8Array(name)) { | @@ -268,7 +269,7 @@ function getDirents(path, { 0: names, 1: types }, callback) { | 268 | 269 | callback(err); | 269 | 270 | return; | 270 | 271 | } | 271 | | - names[idx] = new DirentFromStats(name, stats, path); | | 272 | + names[idx] = new DirentFromStats(name, stats, path, filepath); | 272 | 273 | if (--toFinish === 0) { | 273 | 274 | callback(null, names); | 274 | 275 | } | @@ -304,17 +305,21 @@ function getDirent(path, name, type, callback) { | 304 | 305 | callback(err); | 305 | 306 | return; | 306 | 307 | } | 307 | | - callback(null, new DirentFromStats(name, stats, filepath)); | | 308 | + callback(null, new DirentFromStats(name, stats, path, filepath)); | 308 | 309 | }); | 309 | 310 | } else { | 310 | 311 | callback(null, new Dirent(name, type, path)); | 311 | 312 | } | 312 | 313 | } else if (type === UV_DIRENT_UNKNOWN) { | 313 | 314 | const filepath = join(path, name); | 314 | 315 | const stats = lazyLoadFs().lstatSync(filepath); | 315 | | - return new DirentFromStats(name, stats, path); | 316 | | - } else { | | 316 | + // callback === true: Quirk to not introduce a breaking change. | | 317 | + return new DirentFromStats(name, stats, path, callback === true ? filepath : path); | | 318 | + } else if (callback === true) { | | 319 | + // callback === true: Quirk to not introduce a breaking change. | 317 | 320 | return new Dirent(name, type, path); | | 321 | + } else { | | 322 | + return new Dirent(name, type, path, path); | 318 | 323 | } | 319 | 324 | } | 320 | 325 |
| |
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), files.map((name) => path.join(testDir, name))); | | 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); | |
|