node-watch
Advanced tools
Comparing version 0.2.8 to 0.2.9
@@ -45,2 +45,18 @@ /** | ||
/** | ||
* Mixing object properties. | ||
*/ | ||
var mixin = function() { | ||
var mix = {}; | ||
[].forEach.call(arguments, function(arg) { | ||
for (var name in arg) { | ||
if (arg.hasOwnProperty(name)) { | ||
mix[name] = arg[name]; | ||
} | ||
} | ||
}); | ||
return mix; | ||
}; | ||
/** | ||
* A container for memorizing names of files or directories. | ||
@@ -112,3 +128,3 @@ */ | ||
*/ | ||
var normalizeCall = function(fname, cb) { | ||
var normalizeCall = function(fname, options, cb) { | ||
// Store each name of the modifying or temporary files generated by an editor. | ||
@@ -126,3 +142,5 @@ fileNameCache.push(fname); | ||
// Watch new created directory. | ||
memo.has(f) || is.dir(f) && watch(f, cb); | ||
if (options.recursive && !memo.has(f) && is.dir(f)) { | ||
watch(f, options, cb); | ||
} | ||
cb.call(null, f); | ||
@@ -141,16 +159,32 @@ }).clear(); | ||
*/ | ||
var catchException = function() {} | ||
var catchException = function() {}; | ||
/** | ||
* Watch a file or a directory recursively. | ||
* Option handler for the `watch` function. | ||
*/ | ||
var handleOptions = function(origin, defaultOptions) { | ||
return function() { | ||
var args = [].slice.call(arguments); | ||
if (Object.prototype.toString.call(args[1]) === '[object Function]') { | ||
args[2] = args[1]; | ||
} | ||
args[1] = mixin(defaultOptions, args[1]); | ||
return origin.apply(null, args); | ||
} | ||
}; | ||
/** | ||
* Watch a file or a directory (recursively by default). | ||
* | ||
* @param {String} fpath | ||
* @options {Object} options | ||
* @param {Function} cb | ||
* | ||
* watch('fpath', function(file) { | ||
* watch('fpath', {recursive: true}, function(file) { | ||
* console.log(file, ' changed'); | ||
* }); | ||
*/ | ||
function watch(fpath, cb) { | ||
function watch(fpath, options, cb) { | ||
@@ -164,3 +198,3 @@ // Due to the unstalbe fs.watch(), if the `fpath` is a file then | ||
if (path.basename(fpath) === fname) { | ||
normalizeCall(fpath, cb); | ||
normalizeCall(fpath, options, cb); | ||
} | ||
@@ -170,8 +204,11 @@ }).on('error', catchException); | ||
fs.watch(fpath, function(evt, fname) { | ||
normalizeCall(path.join(fpath, fname), cb); | ||
normalizeCall(path.join(fpath, fname), options, cb); | ||
}).on('error', catchException); | ||
// Recursively watch its sub-directories. | ||
sub(fpath, function(dir) { | ||
watch(dir, cb); | ||
}); | ||
if (options.recursive) { | ||
// Recursively watch its sub-directories. | ||
sub(fpath, function(dir) { | ||
watch(dir, options, cb); | ||
}); | ||
} | ||
} | ||
@@ -183,3 +220,3 @@ | ||
// Expose. | ||
module.exports = watch; | ||
module.exports = handleOptions(watch, {recursive: true}); | ||
{ | ||
"name": "node-watch" | ||
, "version": "0.2.8" | ||
, "version": "0.2.9" | ||
, "description": "fs.watch() wrapper of Nodejs " | ||
@@ -5,0 +5,0 @@ , "url": "https://github.com/yuanchuan/node-watch" |
@@ -12,13 +12,26 @@ #Node-watch | ||
## Installation | ||
### Installation | ||
npm install node-watch | ||
```bash | ||
npm install node-watch | ||
``` | ||
## Example | ||
### Example | ||
var watch = require('node-watch'); | ||
```js | ||
var watch = require('node-watch'); | ||
watch('somedir_or_somefile', function(filename) { | ||
console.log(filename, ' changed.'); | ||
}); | ||
watch('somedir_or_somefile', function(filename) { | ||
console.log(filename, ' changed.'); | ||
}); | ||
``` | ||
### Options | ||
`recursive`: [ true | fase ] -- If watch recursively or not. True by default. | ||
```js | ||
watch('somedir', { recursive: false }, function(filename) { | ||
console.log(filename, ' changed.'); | ||
}); | ||
``` |
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
7314
186
37