new DirectoryWatcher(dir, type, …exclude)
`fs.watch` wrapper class (extends EventEmitter) with the reliable recursive watching capabilities
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
dir |
string | a full system path to a directory | |
type |
RegExp | a regular expression to match files | |
exclude |
string |
<repeatable> |
files/directories to exclude (not full paths, just file/directory names) |
Fires:
- DirectoryWatcher#event:change
Example
import {DirectoryWatcher} from 'simple-recursive-watch';
import {join} from 'path';
let libDir = join(__dirname, 'lib'),
watcher = new DirectoryWatcher(libDir, /\.js$/, 'ignoreMe.js');
Methods
(static) watch(dir, extension, callback, …exclude) → {DirectoryWatcher}
A convenience shortcut method for starting a watcher
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
dir |
string | a full system path to a directory | |
extension |
string | a file extension to watch for | |
callback |
function | a callback function to execute whenever a file system change occurs | |
exclude |
string |
<repeatable> |
files/directories to exclude (not full paths, just file/directory names) |
Returns:
a watcher instance
- Type
- DirectoryWatcher
Example
let watcher = DirectoryWatcher.watch(libDir, 'js', function () {
// some JavaScript file, not named "ignoreMe.js", was changed in the "lib" directory
}, 'ignoreMe.js');
start()
Starts the watcher
Example
watcher.on('change', function () {
// some JavaScript file, not named "ignoreMe.js", was changed in the "lib" directory
});
watcher.start();
stop()
Stops the watcher
Example
watcher.stop();