JavaScript
Latest commit 20be778 Mar 18, 2017 @yuanchuan release 0.5.2
Permalink
Failed to load latest commit information.
lib add deprecate warning Mar 18, 2017
test fix bugs in 0.10.x on mac Mar 17, 2017
.gitignore completely rewrite Mar 12, 2017
.travis.yml update version list Mar 15, 2017
LICENSE completely rewrite Mar 12, 2017
README.md update readme Mar 18, 2017
index.js rename repo May 20, 2012
package.json release 0.5.2 Mar 18, 2017

README.md

node-watch Status

A neat fs.watch wrapper.

NPM

Installation

npm install node-watch

Example

var watch = require('node-watch');

watch('somedir_or_somefile', { recursive: true }, function(evt, name) {
  console.log(name, ' changed.');
});

This is a completely rewritten version, much faster and in a more memory-efficient way. So with recent nodejs versions under OS X or Windows you can do something like this:

// watch the whole disk
watch('/', { recursive: true }, console.log);

Why?

  • Some editors will generate temporary files which will cause the callback function to be triggered multiple times.
  • When watching a single file the callback function will only be triggered once.
  • Missing an option to watch a directory recursively.
  • Recursive watch is not supported on Linux or in older versions of nodejs.

Changelog

  • The recursive option is default to be false since v0.5.0.
  • The callback function will always provide a event name since v0.5.0.
  • Returns a fs.FSWatcher like object since v0.4.0.

Events

The events provided by the callback function would be either update or remove.

watch('./', function(evt, name) {

  if (evt == 'remove') {
    // on delete
  }

  if (evt == 'update') {
    // on create or modify
  }

});

Watcher object

watch function returns a fs.FSWatcher like object as the same as fs.watch (>= v0.4.0).

var watcher = watch('./', { recursive: true });

watcher.on('change', function(evt, name) {
  // callback
});

watcher.on('error', function(err) {
  // handle error
});

// close
watcher.close();

Extra options

  • filter Filter files or directories or skip to watch them.
var options = {
  recursive: true,
  filter : function(name) {
    return !/node_modules/.test(name);
  }
};

// ignore node_modules
watch('./', options, console.log);

Known bugs

Windows, node < v4.2.5

  • Failed to detect remove event
  • Failed to get deleted filename or directory name

Misc

1. Watch multiple files or directories in one place
watch(['file1', 'file2'], console.log);
2. Other ways to filter

a) filtering directly inside the callback function:

watch('./', { recursive: true }, function(evt, name) {
  // ignore node_modules
  if (!/node_modules/.test(name)) {
    // do something
  }
});

b) filtering with higher order function:

function filter(pattern, fn) {
  return function(evt, name) {
    if (pattern.test(name)) {
      fn.apply(null, arguments);
    }
  }
}

// watch only for js files
watch('./', filter(/\.js$/, console.log));

License

Licensed under MIT

Copyright (c) 2012-2017 yuanchuan