Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed readDir unexpectedly giving undefined instead of empty array when there are no matching directories #516

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/node/services/node-file-system.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,24 @@ export async function readDir(
const dirEntries = entryFilter
? unfilteredDirEntries.filter((dirent) => entryFilter(dirent.name))
: unfilteredDirEntries;
return Object.fromEntries(
groupBy(
dirEntries,
(dirEntry): EntryType => {
if (dirEntry.isFile()) return EntryType.File;
if (dirEntry.isDirectory()) return EntryType.Directory;
return EntryType.Unknown;
},
(dirent) => joinUriPaths(uri, dirent.name),
),
) as DirectoryEntries;

// Group each entry by EntryType
const entryMap = groupBy(
dirEntries,
(dirEntry): EntryType => {
if (dirEntry.isFile()) return EntryType.File;
if (dirEntry.isDirectory()) return EntryType.Directory;
return EntryType.Unknown;
},
(dirent) => joinUriPaths(uri, dirent.name),
);

// Fill in each missing EntryType with an empty array
Object.values(EntryType).forEach((entryType) => {
if (!entryMap.has(entryType)) entryMap.set(entryType, []);
});

return Object.freeze(Object.fromEntries(entryMap)) as DirectoryEntries;
}

/**
Expand Down