@aminzer/traverse-directory
v1.2.3
Published
Utility for directory child entries recursive iteration
Maintainers
Readme
Overview
NodeJS utility for recursively iterating over directory contents.
It allows to traverse through all subdirectories and files within a specified directory.
Installation
npm i @aminzer/traverse-directoryUsage examples
import { traverseDirectory } from '@aminzer/traverse-directory';
await traverseDirectory('/path/to/directory', ({ isFile, relativePath }) => {
console.log(`${isFile ? 'File' : 'Directory'} ${relativePath} was found`);
});API
traverseDirectory
Overview
traverseDirectory recursively iterates over the contents of a directory.
import { directoryExists } from '@aminzer/traverse-directory';
await traverseDirectory(dirPath, onEachChild);Parameters
dirPath(string, required) - path to the directory whose contents should be iterated.onEachChild(function, required) - callback invoked for each child file and directory.
onEachChild callback accepts following arguments:
fsEntry(FsEntry) - currently iterated child file or directory.additionalArgs(object, optional) - additional callback arguments:skipEntryChildrenIteration(function) - call this function withinonEachChildto skip iterating the children of the current entry.
Return value
Promise that resolves when the directory traversal is complete.
FsEntry
Overview
FsEntry - class representing File System Entry (file or directory).
import { FsEntry } from '@aminzer/traverse-directory';Instance properties:
name(string) - name of entry.absolutePath(string) - absolute path to entry.relativePath(string) - relative path to entry. It's relative to the directory passed totraverseDirectory.size(number) - size of file in bytes,0for directories.isFile(boolean) -trueif entry is file.isDirectory(boolean) -trueif entry is directory.
directoryExists
Overview
directoryExists is an async function that checks if a given path exists and corresponds to a directory.
import { directoryExists } from '@aminzer/traverse-directory';
const exists = await directoryExists('/path/to/directory');Parameters
dirPath(string, required) - path to the directory to check.
Return value
Promise<boolean> that resolves to true if the directory exists, or false otherwise.
