fix: fs.readdir should support withFileTypes (#24062)

This commit is contained in:
Shelley Vohr 2020-06-12 08:32:59 -07:00 committed by GitHub
parent a7599a0838
commit 66744ecb4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 2 deletions

View file

@ -572,7 +572,7 @@
};
const { readdir } = fs;
fs.readdir = function (pathArgument, options, callback) {
fs.readdir = function (pathArgument, options = {}, callback) {
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (typeof options === 'function') {
callback = options;
@ -594,13 +594,29 @@
return;
}
if (options.withFileTypes) {
const dirents = [];
for (const file of files) {
const stats = archive.stat(file);
if (stats.isFile) {
dirents.push(new fs.Dirent(file, fs.constants.UV_DIRENT_FILE));
} else if (stats.isDirectory) {
dirents.push(new fs.Dirent(file, fs.constants.UV_DIRENT_DIR));
} else if (stats.isLink) {
dirents.push(new fs.Dirent(file, fs.constants.UV_DIRENT_LINK));
}
}
nextTick(callback, [null, dirents]);
return;
}
nextTick(callback, [null, files]);
};
fs.promises.readdir = util.promisify(fs.readdir);
const { readdirSync } = fs;
fs.readdirSync = function (pathArgument, options) {
fs.readdirSync = function (pathArgument, options = {}) {
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return readdirSync.apply(this, arguments);
@ -614,6 +630,21 @@
throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
}
if (options.withFileTypes) {
const dirents = [];
for (const file of files) {
const stats = archive.stat(file);
if (stats.isFile) {
dirents.push(new fs.Dirent(file, fs.constants.UV_DIRENT_FILE));
} else if (stats.isDirectory) {
dirents.push(new fs.Dirent(file, fs.constants.UV_DIRENT_DIR));
} else if (stats.isLink) {
dirents.push(new fs.Dirent(file, fs.constants.UV_DIRENT_LINK));
}
}
return dirents;
}
return files;
};