Skip to content

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

License

Notifications You must be signed in to change notification settings

NodeSecure/fs-walk

Folders and files

NameName
Last commit message
Last commit date
May 1, 2025
Mar 16, 2024
Mar 16, 2024
Jun 24, 2023
Aug 7, 2021
May 4, 2023
Jan 24, 2023
Jun 13, 2022
Jan 5, 2025
Dec 23, 2024
Jan 18, 2023
Aug 13, 2024
Feb 24, 2025
Aug 13, 2024

Repository files navigation

fs-walk

npm version license ossf scorecard github ci workflow

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.

Note

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 "node: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 WalkEntry = [dirent: fs.Dirent, absoluteFileLocation: string];

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

Asynchronous walk.

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

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);

Contributors ✨

All Contributors

Thanks goes to these wonderful people (emoji key):

Gentilhomme
Gentilhomme

πŸ’» πŸ“– πŸ‘€ πŸ›‘οΈ πŸ›
Nicolas Hallaert
Nicolas Hallaert

πŸ“–
Antoine Coulon
Antoine Coulon

🚧
Kouadio Fabrice Nguessan
Kouadio Fabrice Nguessan

🚧
Christian Lisangola
Christian Lisangola

⚠️
PierreDemailly
PierreDemailly

🚧

License

MIT