file-system-walker
TypeScript icon, indicating that this package has built-in type declarations

1.0.14 • Public • Published

file-system-walker

Badge LICENSE Node npm version

A modern file system walker lib for Nodejs.

Installation

npm install file-system-walker --save

Usage

// import via esm
import { FileSystemWalker } from "file-system-walker";

// import via cjs
const { FileSystemWalker } = require("file-system-walker");
import { FileSystemWalker } from "file-system-walker";

const walker = new FileSystemWalker("/path/to/folder");

// walk file system asynchronously (Recommend)
for await (const entity of walker) {
  console.log(entity.filepath, entity.stats, entity.deep);

  // breakable for walker
  if (/node_modules/.test(entity.filepath)) {
    break;
  }
}

// walk file system synchronously (Not recommend)
for (const entity of walker) {
  console.log(entity.filepath, entity.stats, entity.deep);

  // breakable for walker
  if (/node_modules/.test(entity.filepath)) {
    break;
  }
}

API

new FileSystemWalker(filepath, [options])

Generate a traversable object which can use with for ... of and for await ... of

Options:

export interface FileSystemWalkerOptions {
  /**
   * Define the exclude when walk in
   * @default undefined
   */
  exclude?: RegExp | ((filepath: string, stat: fs.Stats) => boolean);
  /**
   * only travel to max depth.
   * @example `maxDeep=0 mean emit root dir only`
   * @example `maxDeep=1 mean emit the file/folder of root`
   * @default undefined
   */
  maxDeep?: number;
  /**
   * Whether follow the Symlinks
   * @default false
   */
  followSymlinks?: boolean;
}

Entity:

export interface FileSystemWalkerEntity {
  /**
   * The file path of walk entity
   */
  filepath: string;
  /**
   * The file status of walk entity
   */
  stats: fs.Stats;
  /**
   * The deep of the traverse. The root is zero
   */
  deep: number;
}

License

The Anti 996 License

Readme

Keywords

Package Sidebar

Install

npm i file-system-walker

Weekly Downloads

6

Version

1.0.14

License

Anti 996

Unpacked Size

38.8 kB

Total Files

9

Last publish

Collaborators

  • axetroy