Gargoyle
Monitor a directory for changes. You can detect file changes, creations, deletions and renames. You know it works because it actually has tests!
Installation
Via NPM: npm install gargoyle
Usage
There is one export: gargoyle.monitor(path[, options, callback])
. path
should
be a filename (file or directory). IF it's a directory, it'll be traversed
recursively.
When something changes, it'll emit one of the following events:
modify
- when a file is modifiedcreate
- when a file is createddelete
- when a file is deletedrename
- when a file is renamed (only when options.type === "watch")
Your event listener should be a function that takes one argument: the absolute path of the file that got modified/created/deleted/renamed.
Example
Monitor a directory tree:
var gargoyle = ;gargoyle;
Stop monitoring:
monitor; //monitor is now worthless
Options
There are a few different options you can pass as an optional
second parameter to gargoyle.monitor
.
Exclude certain files
var path = ;var options = { //note: filename is absolute var basename = path; //ignore dotfiles if basename === '.' return true; //ignore the static directory if stat && basename === 'static' return true; //javascript/coffeescript files are okay if !/\.$/ return true; return false; };gargoyle;
Watch type
Use fs.watchFile
instead of fs.watch
. If you're on OS X, or trying to
watch a network directory (e.g. a shared folder in a VM), you'll want
to use fs.watchFile
. fs.watch
is far more efficient, and much faster,
but doesn't work all the time.
var options = type: 'watchFile' //default is 'watch';gargoyle;
Poll interval
If you use the watchFile
type, you can define the poll interval for stat
ing
the filesystem (this is part of the watchFile
Node API). The default is 507
,
which is 10x as fast as the Node default.
var options = type: 'watchFile' interval: 50 //poll 20 times a second;gargoyle;
Development
git clone git@github.com:tmont/gargoyle.gitcd gargoylenpm installnpm test
When running the tests, you'll notice the fs.watchFile
tests take much longer.
This is due to the fact that some file systems do not have millisecond resolution,
which means we have to wait at least one second to detect a modification.