diff options
author | Bryan English <bryan@bryanenglish.com> | 2018-07-27 19:29:32 -0700 |
---|---|---|
committer | Bryan English <bryan@bryanenglish.com> | 2018-08-13 19:18:45 -0700 |
commit | c7944a7a7bd92a446ea81d774ccad31e0e7c8a3f (patch) | |
tree | c7b86a281f2af282e080b34965debcea28caf2b7 /lib/fs.js | |
parent | 78584b64d88c0567c46e3cf7bae42aa73927df40 (diff) | |
download | android-node-v8-c7944a7a7bd92a446ea81d774ccad31e0e7c8a3f.tar.gz android-node-v8-c7944a7a7bd92a446ea81d774ccad31e0e7c8a3f.tar.bz2 android-node-v8-c7944a7a7bd92a446ea81d774ccad31e0e7c8a3f.zip |
fs: readdir optionally returning type information
readdir and readdirSync now have a "withFileTypes" option, which, when
enabled, provides an array of DirectoryEntry objects, similar to Stats
objects, which have the filename and the type information.
Refs: https://github.com/nodejs/node/issues/15699
PR-URL: https://github.com/nodejs/node/pull/22020
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Diffstat (limited to 'lib/fs.js')
-rw-r--r-- | lib/fs.js | 23 |
1 files changed, 19 insertions, 4 deletions
@@ -58,6 +58,8 @@ const { getPathFromURL } = require('internal/url'); const internalUtil = require('internal/util'); const { copyObject, + Dirent, + getDirents, getOptions, nullCheck, preprocessSymlinkDestination, @@ -773,8 +775,19 @@ function readdir(path, options, callback) { validatePath(path); const req = new FSReqCallback(); - req.oncomplete = callback; - binding.readdir(pathModule.toNamespacedPath(path), options.encoding, req); + if (!options.withFileTypes) { + req.oncomplete = callback; + } else { + req.oncomplete = (err, result) => { + if (err) { + callback(err); + return; + } + getDirents(path, result, callback); + }; + } + binding.readdir(pathModule.toNamespacedPath(path), options.encoding, + !!options.withFileTypes, req); } function readdirSync(path, options) { @@ -783,9 +796,10 @@ function readdirSync(path, options) { validatePath(path); const ctx = { path }; const result = binding.readdir(pathModule.toNamespacedPath(path), - options.encoding, undefined, ctx); + options.encoding, !!options.withFileTypes, + undefined, ctx); handleErrorFromBinding(ctx); - return result; + return options.withFileTypes ? getDirents(path, result) : result; } function fstat(fd, options, callback) { @@ -1819,6 +1833,7 @@ module.exports = fs = { writeFileSync, write, writeSync, + Dirent, Stats, get ReadStream() { |