node package manager

file-watcher

file-watcher

Modern file watcher for nodejs

  • Watch a directory and his subdirectories for changes
  • Filter method to exclude files or directories

Installation

npm install file-watcher

Usage

 
var Watcher = require('file-watcher');
 
var watcher = new Watcher({
    root: __dirname,
    filter: function(filename, stats) {
        // only watch those files 
        if(filename.indexOf('.styl') != -1) {
            return true;
        }
        else {
            return false;
        }
    }
});
 
watcher.on('...', function() { /* Watcher is an EventEmitter */ });
 
watcher.watch();
 

constructor new Watcher(opts)

  • opts.root : Directory to watch
  • opts.filter : callback to filter filename or by fs.Stat

callback filter(filename, stats)

  • filename : filename relative to the root of the watched tree
  • stats : a fs.Stat object

Return true to include this file or directory and their children. Return false to exclude them.

method watcher.watch()

Start watching the directory tree

event create (event)

  • event.oldStats : null
  • event.oldPath : null
  • event.newStats : a fs.Stat object
  • event.newPath : created filename relative to the root of the watched tree

event change (event)

  • event.oldStats : last fs.Stat object
  • event.oldPath : current filename relative to the root of the watched tree
  • event.newStats : current fs.Stat object
  • event.newPath : current filename relative to the root of the watched tree

event delete (event)

  • event.oldStats : last fs.Stat object
  • event.oldPath : deleted filename relative to the root of the watched tree
  • event.newStats : null
  • event.newPath : null

event any (type, event)

  • type : event type
  • event : see corresponding event type

event error (err)

  • err : unhandled error

TODOs

  • Add unit tests
  • Handle rename
  • ?

History

  • 2013-06-11 : first working version