tschaub / mock-fs

Configurable mock for the fs module
https://npmjs.org/package/mock-fs
Other
906 stars 85 forks source link

Implementing "withFileTypes" option for fs.readdir method #287

Closed mrmlnc closed 4 years ago

mrmlnc commented 4 years ago

Hi, I'm the maintainer of the fast-glob package.

In my package, I try to use all possible ways to optimize the work with the file system. One of these ways is a withFileTypes option in the fs.readdir method. Unfortunately, this package does not support this option. So, this pull request implements support for this option.

This is a possible solution for the following issues:

My solution uses an internal implementation of the getDirents method from FSReqWrap, because I don't know how to replace callback. I will accept any suggestions if it really needs to be done.

I checked that my changes are working by the following script:

const mock = require('.');
const fg = require('fast-glob');

mock({
    first: 'content',
    second: {},
    third: {
        nested: {
            file: 'content'
        }
    }
});

const entries = fg.sync('**/*', { onlyFiles: false, objectMode: true });

console.dir(entries, { colors: true });

[ { dirent: Dirent { name: 'first', [Symbol(type)]: 1 },
    name: 'first',
    path: 'first' },
  { dirent: Dirent { name: 'second', [Symbol(type)]: 2 },
    name: 'second',
    path: 'second' },
  { dirent: Dirent { name: 'third', [Symbol(type)]: 2 },
    name: 'third',
    path: 'third' },
  { dirent: Dirent { name: 'nested', [Symbol(type)]: 2 },
    name: 'nested',
    path: 'third/nested' },
  { dirent: Dirent { name: 'file', [Symbol(type)]: 1 },
    name: 'file',
    path: 'third/nested/file' } ]
3cp commented 4 years ago

LGTM. cc @tschaub

tschaub commented 4 years ago

Looks great. Thanks for the contribution @mrmlnc!