-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
Description
In node v10.10.0 with merge request #22020 was added a new flag withFileTypes to fs.readdir:
If options.withFileTypes is set to true, the files array will contain fs.Dirent objects.
It is really great addition and I have a couple ideas to share.
Would be great if fs.Dirent contain all information fs.Stat has. I can see no reason to create new entity for such simple data structure which is half duplicated.
Lets compare them:
fs.Dirent
dirent.isBlockDevice()dirent.isCharacterDevice()dirent.isDirectory()dirent.isFIFO()dirent.isFile()dirent.isSocket()dirent.isSymbolicLink()dirent.name
fs.Stats
stats.isBlockDevice()stats.isCharacterDevice()stats.isDirectory()stats.isFIFO()stats.isFile()stats.isSocket()stats.isSymbolicLink()stats.devstats.inostats.modestats.nlinkstats.uidstats.gidstats.rdevstats.sizestats.blksizestats.blocksstats.atimeMsstats.mtimeMsstats.ctimeMsstats.birthtimeMsstats.atimestats.mtimestats.ctimestats.birthtime
The only thing that missing in fs.Stats it's name.
Is your feature request related to a problem? Please describe.
I'm working on file manager for the web Cloud Commander, so I do such things all the time: read directory content and then get stat of every file. Now this all done in readify. And I would like to use the new approach of reading files with stats, it can simplify my code a lot, and make node API cleaner.
Right now if I use a flag withFileTypes I will need to call fs.stat anyways because I need not only divide files and directories but also get their size, mode, uid and mtime.
Describe the solution you'd like
I suggest to change withFileTypes behavior to get real stat information: like size, mtime, uid in method call:
fs.readdirSync('/', {withFileTypes: true});
And remove this fs.Dirent.
Describe alternatives you've considered
Also we can use something like withFileStats to get full stats (maybe with names).
fs.readdirSync('/', {withFileStats: true})