Skip to content

aminzer/traverse-directory

Repository files navigation

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-directory

Usage 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 within onEachChild to 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 to traverseDirectory.
  • size (number) - size of file in bytes, 0 for directories.
  • isFile (boolean) - true if entry is file.
  • isDirectory (boolean) - true if 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.

About

Utility for directory child entries recursive iteration

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors