Recursively walk through the filesystem, searching for files and directories, either async or synchronous while optionally filtering.
The walker will always respond with an fs_stats
instances, retrieved using lstat and decorated with extra properties:
- directory
- name
- fullname (which is a concatenation of directory and name)
Installation
Install using npm:
npm install --save fs-walker
Then require:
var walk = ;
Usage
node-fs-walker comes in three flavours:
- Async
- Sync
- Events
The first two flavours both can walk through three options:
- files
- directories
- both
The third flavour has the following events defined, depending on the type found:
- file
- dir
- block
- character
- symbolic
- fifo
- socket
- unknown
Initial
The following samples all use this setup:
var dir = process walk = ;
Async
Files
;
note: walk is a shortcut for walk.files, so the following works as well. This will make more sense in the other samples.
walk;
Directories
walkdirectoriesdir { console;};
Both
walkalldir { console;};
Sync
All the above handlers have a synchronous variant, which maps with the async callers.
- walk.sync or walk.files.sync
- walk.directories.sync
- walk.all.sync
These return an array of fs_stats
instances instead.
Events
note: Under the hood, the async walker will be used.
var walker = dir; walker ;
Filters
It is possible to filter both files and directories. The filter also receives an fs_stats
instance.
The filter looks as follows:
var filter = { return /\.js$/i; } { return statsname !== 'node_modules'; };
This filter will make it so that only files are returned that end in .js
and will not be looking in the node_modules
folder while walking the file-system.
It is possible to omit the file
, directory
or both filters, e.g.:
var filter = { return statsname !== 'node_modules'; };
The filter handler is picked based on fs_stats
's isDirectory()
.
Using the filter is done by passing it as the second argument, as follows:
Async
;
Sync
walk;
Events
var walker = dir; walker ;