Socket
Socket
Sign inDemoInstall

node-watch

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-watch - npm Package Compare versions

Comparing version 0.5.3 to 0.5.4

Changelog.md

3

lib/is.js

@@ -11,2 +11,5 @@ var fs = require('fs');

},
buffer: function(item) {
return Buffer.isBuffer(item);
},
regExp: function(item) {

@@ -13,0 +16,0 @@ return Object.prototype.toString.call(item) == '[object RegExp]';

@@ -88,2 +88,5 @@ var fs = require('fs');

getMessages(cache).forEach(function(msg) {
if (info.options.encoding == 'buffer') {
msg[1] = new Buffer(msg[1]);
}
fn.apply(null, msg);

@@ -95,2 +98,5 @@ });

return function(evt, name) {
if (is.buffer(name)) {
name = name.toString()
}
if (is.nil(name)) {

@@ -314,2 +320,6 @@ name = '';

if (is.buffer(fpath)) {
fpath = fpath.toString();
}
if (is.array(fpath)) {

@@ -327,2 +337,8 @@ return composeWatcher(unique(fpath).map(function(f) {

if (is.string(options)) {
options = {
encoding: options
}
}
if (is.func(options)) {

@@ -329,0 +345,0 @@ fn = options;

2

package.json

@@ -14,3 +14,3 @@ {

],
"version": "0.5.3",
"version": "0.5.4",
"bugs": {

@@ -17,0 +17,0 @@ "url": "https://github.com/yuanchuan/node-watch/issues"

@@ -41,13 +41,5 @@ # node-watch [![Status](https://travis-ci.org/yuanchuan/node-watch.svg?branch=master)](https://travis-ci.org/yuanchuan/node-watch "See test builds")

## 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](https://nodejs.org/api/fs.html#fs_class_fs_fswatcher) like object since **v0.4.0**.
## Events
The events provided by the callback function would be either `update` or `remove`.
The events provided by the callback function is either `update` or `remove`, which is less confusing to `fs.watch`'s `rename` and `change`.

@@ -57,2 +49,6 @@ ```js

if (evt == 'update') {
// on create or modify
}
if (evt == 'remove') {

@@ -62,12 +58,32 @@ // on delete

if (evt == 'update') {
// on create or modify
}
});
```
## 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
`watch` function returns a [fs.FSWatcher](https://nodejs.org/api/fs.html#fs_class_fs_fswatcher) like object as the same as `fs.watch` (>= v0.4.0).
The watch function returns a [fs.FSWatcher](https://nodejs.org/api/fs.html#fs_class_fs_fswatcher) like object as the same as `fs.watch` (>= v0.4.0).

@@ -87,21 +103,20 @@ ```js

watcher.close();
// is closed?
watcher.isClosed()
```
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:
## Extra options
* `filter <RegExp>` as a regular expression for filtering with ease
* `filter <Function>` as a function to filter files
* `.on`
* `.once`
* `.emit`
* `.close`
* `.listeners`
* `.setMaxListeners`
* `.getMaxListeners`
##### Extra methods
* `.isClosed` detect if the watcher is closed
```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);
```
## Known issues

@@ -121,32 +136,4 @@

#### 2. Other ways to filter
*a)* filtering directly inside the callback function:
#### 2. Customize watch command line tool
```js
watch('./', { recursive: true }, function(evt, name) {
// ignore node_modules
if (!/node_modules/.test(name)) {
// do something
}
});
```
*b)* filtering with higher order function:
```js
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
```js
#!/usr/bin/env node

@@ -153,0 +140,0 @@

@@ -44,3 +44,3 @@ var assert = require('assert');

];
watcher = watch(fpath, function(evt, name) {
watcher = watch(new Buffer(fpath), function(evt, name) {
stack.splice(stack.indexOf(name), 1);

@@ -144,2 +144,27 @@ if (!stack.length) done();

describe('options', function() {
describe('encoding', function() {
it('should accept options as an encoding string', function(done) {
var dir = tree.getPath('home/a');
var file = 'home/a/file1';
var fpath = tree.getPath(file);
watcher = watch(dir, 'utf8', function(evt, name) {
assert(name.toString() === fpath);
done();
});
tree.modify(file, 200);
});
it('should support buffer encoding', function(done) {
var dir = tree.getPath('home/a');
var file = 'home/a/file1';
var fpath = tree.getPath(file);
watcher = watch(dir, 'buffer', function(evt, name) {
assert(Buffer.isBuffer(name), 'not a Buffer')
assert(name.toString() === fpath);
done();
});
tree.modify(file, 200);
});
});
describe('filter', function() {

@@ -287,3 +312,3 @@ it('should only watch filtered directories', function(done) {

setTimeout(function() {
assert(watcher.isClosed() === true);
assert(watcher.isClosed(), 'watcher should be closed');
assert(times === 0, 'failed to close the watcher');

@@ -290,0 +315,0 @@ done();

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc