CircleCI

sane

I've been driven to insanity by node filesystem watcher wrappers. Sane aims to be fast, small, and reliable file system watcher. It does that by:

Install

$ npm install sane

How to choose a mode

Don't worry too much about choosing the correct mode upfront because sane maintains the same API across all modes and will be easy to switch.

API

sane(dir, options)

Watches a directory and all its descendant directories for changes, deletions, and additions on files and directories.

js var watcher = sane('path/to/dir', {glob: ['**/*.js', '**/*.css']}); watcher.on('ready', function () { console.log('ready') }); watcher.on('change', function (filepath, root, stat) { console.log('file changed', filepath); }); watcher.on('add', function (filepath, root, stat) { console.log('file added', filepath); }); watcher.on('delete', function (filepath, root) { console.log('file deleted', filepath); }); // close watcher.close();

options:

For the glob pattern documentation, see micromatch. If you choose to use watchman you'll have to install watchman yourself). For the ignored options, see anymatch.

sane.NodeWatcher(dir, options)

The default watcher class. Uses fs.watch under the hood, and takes the same options as sane(dir, options).

sane.WatchmanWatcher(dir, options)

The watchman watcher class. Takes the same options as sane(dir, options).

sane.PollWatcher(dir, options)

The polling watcher class. Takes the same options as sane(dir, options) with the addition of:

sane.{Node|Watchman|Poll}Watcher#close

Stops watching.

sane.{Node|Watchman|Poll}Watcher events

Emits the following events:

All events are passed the file/dir path relative to the root directory * ready when the program is ready to detect events in the directory * change when a file changes * add when a file or directory has been added * delete when a file or directory has been deleted

CLI

This module includes a simple command line interface, which you can install with npm install sane -g.

``` Usage: sane [...directory] [--glob=] [--poll] [--watchman] [--watchman-path=] [--dot] [--wait=]

OPTIONS: --glob= A single string glob pattern or an array of them.

--ignored=<filePattern>
  A glob, regex, function, or array of any combination.

--poll, -p
  Use polling mode.

--watchman, -w
  Use watchman (if available).

--watchman-path=<watchmanBinaryPath>
  Sets a custom path for watchman binary (if using this mode).

--dot, -d
  Enables watching files/directories that start with a dot.

--wait=<seconds>
  Duration, in seconds, that watching will be disabled
  after running <command>. Setting this option will
  throttle calls to <command> for the specified duration.

```

It will watch the given directory and run the given every time a file changes.

CLI example usage

License

MIT

Credits

The CLI was originally based on the watch CLI. Watch is licensed under the Apache License Version 2.0.