fs-walker

1.0.0 • Public • Published

node-fs-walker

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 = require('fs-walker');

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.cwd(),
    walk = require('fs-walker');

Async

Files

walk(dir, function(stats) {
  console.log(stats.fullname);
});

note: walk is a shortcut for walk.files, so the following works as well. This will make more sense in the other samples.

walk.files(dir, function(stats) {
  console.log(stats.fullname);
});

Directories

walk.directories(dir, function(stats) {
  console.log(stats.fullname);
});

Both

walk.all(dir, function(stats) {
  console.log(stats.fullname);
});

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 = new walk.Walker(dir);
 
walker.on('file', function(stats) {
        console.log('file event: %s', stats.fullname);
      })
      .on('dir', function(stats) {
        console.log('dir event: %s', stats.fullname);
      })
      .walk();

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 = { 
  file: function(stats) {
    return /\.js$/i.test(stats.name);
  }, 
  directory: function(stats) {
    return stats.name !== '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 = { 
  directory: function(stats) {
    return stats.name !== '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

walk(dir, filter, function(stats) {
    console.log(stats.fullname);
});

Sync

walk.sync(dir, filter).forEach(function(stats) {
    console.log(stats.fullname);
});

Events

var walker = new walk.Walker(dir);
 
walker.on('file', function(stats) {
        console.log('file event: %s', stats.fullname);
      })
      .on('dir', function(stats) {
        console.log('dir event: %s', stats.fullname);
      })
      .walk(filter);

Package Sidebar

Install

npm i fs-walker

Weekly Downloads

38

Version

1.0.0

License

none

Last publish

Collaborators

  • steventhuriot