file-system-walker
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