1.0.16 • Published 8 months ago

file-system-walker v1.0.16

Weekly downloads
-
License
Anti 996
Repository
github
Last release
8 months ago

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

1.0.16

8 months ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

3 years ago

1.0.12

2 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago