@nodesecure/fs-walk

Modern FileSystem (fs) utilities to lazy walk directories Asynchronously (but also Synchronously)

Usage no npm install needed!

<script type="module">
  import nodesecureFsWalk from 'https://cdn.skypack.dev/@nodesecure/fs-walk';
</script>

README

fs-walk

version Maintenance Security Responsible Disclosure mit dep

Modern FileSystem (fs) utilities to lazy walk directories Asynchronously (but also Synchronously). Under the hood the code has been created using ES6 Generators.

Features

  • Lazy walk by using fs.opendir.
  • Zero dependencies.
  • Enforce usage of Symbols for CONSTANTS.
  • Synchronous API.

Performance over some of the features is a non-goal.

Requirements

Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @nodesecure/fs-walk
# or
$ yarn add @nodesecure/fs-walk

Usage example

import path from "path";
import { walk } from "@nodesecure/fs-walk";

for await (const [dirent, absoluteFileLocation] of walk(".")) {
  if (dirent.isFile()) {
    console.log(absoluteFileLocation);
    console.log(path.extname(absoluteFileLocation));
  }
}

API

export interface WalkOptions {
  extensions?: Set<string>;
}

export type WalkResult = [dirent: fs.Dirent, absoluteFileLocation: string];

walk(directory: string, options?: WalkOptions): AsyncIterableIterator< WalkResult >

Asynchronous walk.

walkSync(directory: string, options?: WalkOptions): IterableIterator< WalkResult >

Synchronous walk (using readdirSync under the hood instead of opendir).

For example fetching JavaScript files for a given location:

import { walkSync } from "@nodesecure/fs-walk";

const javascriptFiles = [...walkSync("./someDirectory", { extensions: new Set([".js"]) }))]
    .filter(([dirent]) => dirent.isFile())
    .map(([, absoluteFileLocation]) => absoluteFileLocation);

console.log(javascriptFiles);

License

MIT