NodeJS utility for recursively iterating over directory contents.
It allows to traverse through all subdirectories and files within a specified directory.
npm i @aminzer/traverse-directoryimport { traverseDirectory } from '@aminzer/traverse-directory';
await traverseDirectory('/path/to/directory', ({ isFile, relativePath }) => {
console.log(`${isFile ? 'File' : 'Directory'} ${relativePath} was found`);
});traverseDirectory
traverseDirectory recursively iterates over the contents of a directory.
import { directoryExists } from '@aminzer/traverse-directory';
await traverseDirectory(dirPath, onEachChild);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.
Promise that resolves when the directory traversal is complete.
FsEntry
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
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');dirPath(string, required) - path to the directory to check.
Promise<boolean> that resolves to true if the directory exists, or false otherwise.