node-watch
Advanced tools
Comparing version 0.5.5 to 0.5.6
# Changelog | ||
## 0.5.6 | ||
* Fix recursive watch with filter option. | ||
## 0.5.5 | ||
@@ -4,0 +7,0 @@ * Remove duplicate events from a composed watcher. |
@@ -231,6 +231,3 @@ var fs = require('fs'); | ||
else if (is.directory(name) && !self.watchers[fullPath]) { | ||
var filterGuard = guard(info.options.filter); | ||
filterGuard(name, function() { | ||
self.watchDirectory(name, info.options); | ||
}); | ||
self.watchDirectory(name, info.options); | ||
} | ||
@@ -328,6 +325,3 @@ } | ||
getSubDirectories(dir, function(d) { | ||
var filterGuard = guard(options.filter); | ||
filterGuard(d, function() { | ||
self.watchDirectory(d, options); | ||
}); | ||
self.watchDirectory(d, options); | ||
}); | ||
@@ -334,0 +328,0 @@ } |
@@ -14,3 +14,3 @@ { | ||
], | ||
"version": "0.5.5", | ||
"version": "0.5.6", | ||
"bugs": { | ||
@@ -17,0 +17,0 @@ "url": "https://github.com/yuanchuan/node-watch/issues" |
# node-watch [![Status](https://travis-ci.org/yuanchuan/node-watch.svg?branch=master)](https://travis-ci.org/yuanchuan/node-watch "See test builds") | ||
A wrapper and enhancements for [fs.watch](http://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener) (with 0 dependencies). | ||
A wrapper and enhancements for [fs.watch](http://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener). | ||
@@ -24,4 +24,3 @@ [![NPM](https://nodei.co/npm/node-watch.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/node-watch.png/) | ||
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: | ||
Now it's fast to watch **deep** directories on macOS and Windows, since the `recursive` option is natively supported except on Linux. | ||
@@ -37,10 +36,32 @@ ```js | ||
* 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. | ||
* The callback function will only be triggered once on watching a single file. | ||
* <del>Missing an option to watch a directory recursively.</del> | ||
* Recursive watch is not supported on Linux or in older versions of nodejs. | ||
* Keep it simple, stupid. | ||
## Options | ||
The usage and options of `node-watch` are compatible with [fs.watch](https://nodejs.org/dist/latest-v7.x/docs/api/fs.html#fs_fs_watch_filename_options_listener). | ||
* `persistent: Boolean` (default **true**) | ||
* `recursive: Boolean` (default **false**) | ||
* `encoding: String` (default **'utf8'**) | ||
**Extra options** | ||
* `filter: RegExp | Function` | ||
Return that matches the filter expression. | ||
```js | ||
// filter with regular expression | ||
watch('./', { filter: /\.json$/ }); | ||
// filter with custom function | ||
watch('./', { filter: f => !/node_modules/.test(f) }); | ||
``` | ||
## Events | ||
The events provided by the callback function is either `update` or `remove`, which is less confusing to `fs.watch`'s `rename` and `change`. | ||
The events provided by the callback function is either `update` or `remove`, which is less confusing to `fs.watch`'s `rename` or `change`. | ||
@@ -61,26 +82,3 @@ ```js | ||
## Options | ||
The usage and options of `node-watch` is fully compatible with [fs.watch](https://nodejs.org/dist/latest-v7.x/docs/api/fs.html#fs_fs_watch_filename_options_listener). | ||
* `persistent: <Boolean>` default = **true** | ||
* `recursive: <Boolean>` default = **false** | ||
* `encoding: <String>` default = **'utf8'** | ||
##### Extra options | ||
* `filter: <RegExp | Function>` filter using regular expression or custom function. | ||
```js | ||
// 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); | ||
``` | ||
## Watcher object | ||
@@ -107,5 +105,5 @@ | ||
``` | ||
The watcher object is also an instance of [EventEmitter](https://nodejs.org/dist/latest-v7.x/docs/api/events.html#events_class_eventemitter). | ||
This's a list of methods for watcher specifically: | ||
#### List of methods | ||
* `.on` | ||
@@ -155,2 +153,8 @@ * `.once` | ||
## Alternatives | ||
* [chokidar](https://github.com/paulmillr/chokidar) | ||
* [gaze](https://github.com/shama/gaze) | ||
* [mikeal/watch](https://github.com/mikeal/watch) | ||
## License | ||
@@ -160,2 +164,1 @@ MIT | ||
Copyright (c) 2012-2017 [yuanchuan](https://github.com/yuanchuan) | ||
@@ -133,3 +133,3 @@ var assert = require('assert'); | ||
var dir = tree.getPath('home'); | ||
var file = tree.getPath('home/bb/file1'); | ||
var file = tree.getPath('home/d/file1'); | ||
watcher = watch(dir, { recursive: true }, function(evt, name) { | ||
@@ -139,3 +139,3 @@ assert.equal(file, name); | ||
}); | ||
tree.modify('home/bb/file1', 200); | ||
tree.modify('home/d/file1', 1000); | ||
}); | ||
@@ -214,3 +214,3 @@ }); | ||
filter: function(name) { | ||
return !/node_modules/.test(name); | ||
return !/deep_node_modules/.test(name); | ||
} | ||
@@ -220,3 +220,3 @@ }; | ||
watcher = watch(tree.getPath('home'), option, function(evt, name) { | ||
if (/node_modules/.test(name)) { | ||
if (/deep_node_modules/.test(name)) { | ||
shouldNotModify = true; | ||
@@ -229,7 +229,7 @@ } else { | ||
tree.modify('home/b/file1', 200); | ||
tree.modify('home/node_modules/ma/file1', 500); | ||
tree.modify('home/deep_node_modules/ma/file1', 500); | ||
setTimeout(function() { | ||
assert(!shouldModify, 'watch failed'); | ||
assert(!shouldNotModify, 'fail to ingore path `node_modules`'); | ||
assert(!shouldNotModify, 'fail to ingore path `deep_node_modules`'); | ||
done(); | ||
@@ -240,3 +240,3 @@ }, 900); | ||
it('should only report filtered files', function(done) { | ||
var dir = tree.getPath('home/a'); | ||
var dir = tree.getPath('home'); | ||
var file1 = 'home/a/file1'; | ||
@@ -246,2 +246,3 @@ var file2 = 'home/a/file2'; | ||
var options = { | ||
recursive: true, | ||
filter: function(name) { | ||
@@ -266,3 +267,3 @@ return !/file1/.test(name); | ||
it('should be able to filter with regexp', function(done) { | ||
var dir = tree.getPath('home/a'); | ||
var dir = tree.getPath('home'); | ||
var file1 = 'home/a/file1'; | ||
@@ -272,2 +273,3 @@ var file2 = 'home/a/file2'; | ||
var options = { | ||
recursive: true, | ||
filter: /file2/ | ||
@@ -274,0 +276,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
159
31200
12
920