Skip to content
Modern file watcher for nodejs
JavaScript
Find file
Pull request Compare This branch is 1 commit behind xpensia:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
.gitignore
README.md
index.js
package.json

README.md

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(__dirname, function(err){
    if (err){
        console.log(err);
    }
});

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(dir, cb)

  • dir : Directory to watch
  • cb : (optional) callback fired then the watch has started. As error object may be passed as the first argument if there was a problem.

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
Something went wrong with that request. Please try again.