JSPM

  • Created
  • Published
  • Downloads 845888
  • Score
    100M100P100Q182072F
  • License MIT

A wrapper and enhancements for fs.watch

Package Exports

  • node-watch

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-watch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

node-watch Status

A wrapper and enhancements for fs.watch (with 0 dependencies).

NPM

Installation

npm install node-watch

Example

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

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

This is a completely rewritten version, much faster and in a more memory-efficient way. So with recent nodejs 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 filter option can be of either Function or RegExp type since v0.5.3.
  • The recursive option is default to be false since v0.5.0.
  • The callback function will always provide an 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 <RegExp> as a regular expression for filtering with ease
  • filter <Function> as a function to filter files
// watch only for json files
watch('./', { filter: /\.json$/ }, console.log);

// ignore node_modules
watch('./', {
  recursive: true,
  filter: function(name) {
    return !/node_modules/.test(name);
  }
}, console.log);

Known issues

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));

3. customize watch command line tool

#!/usr/bin/env node

// https://github.com/nodejs/node-v0.x-archive/issues/3211
require('epipebomb')();

var watcher = require('node-watch')(
  process.argv[2] || './', { recursive: true }, console.log
);

process.on('SIGINT', watcher.close);

Monitoring chrome from disk:

$ watch / | grep -i chrome

License

MIT

Copyright (c) 2012-2017 yuanchuan