fs-extra#Dirent TypeScript Examples
The following examples show how to use
fs-extra#Dirent.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: files.ts From dendron with GNU Affero General Public License v3.0 | 5 votes |
/** Gets all files in `root`, with include and exclude lists (glob matched)
*
* This function returns the full `Dirent` which gives you access to file
* metadata. If you don't need the metadata, see {@link getAllFiles}.
*
* @throws a `DendronError` with `ERROR_SEVERITY.MINOR`. This is to avoid
* crashing the Dendron initialization, please catch the error and modify the
* severity if needed.
*/
export async function getAllFilesWithTypes(
opts: GetAllFilesOpts
): Promise<RespV2<Dirent[]>> {
const { root } = _.defaults(opts, {
exclude: [".git", "Icon\r", ".*"],
});
try {
const allFiles = await fs.readdir(root, { withFileTypes: true });
return {
data: allFiles
.map((dirent) => {
const { name: fname } = dirent;
// match exclusions
if (
_.some([dirent.isDirectory(), globMatch(opts.exclude || [], fname)])
) {
return null;
}
// match inclusion
if (opts.include && !globMatch(opts.include, fname)) {
return null;
}
return dirent;
})
.filter(isNotNull),
error: null,
};
} catch (err) {
return {
error: new DendronError({
message: "Error when reading the vault",
payload: err,
// Marked as minor to avoid stopping initialization. Even if we can't read one vault, we might be able to read other vaults.
severity: ERROR_SEVERITY.MINOR,
}),
};
}
}