Socket
Socket
Sign inDemoInstall

watchr

Package Overview
Dependencies
Maintainers
1
Versions
122
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

watchr - npm Package Compare versions

Comparing version 2.1.3 to 2.1.4

4

History.md
## History
- v2.1.4 September 27, 2012
- Fixed new listeners not being added for directories that have already been watched
- Thanks to [pull request #14](https://github.com/bevry/watchr/pull/14) by [Casey Foster](https://github.com/caseywebdev)
- v2.1.3 August 10, 2012

@@ -4,0 +8,0 @@ - Re-added markdown files to npm distribution as they are required for the npm website

43

out/bin/watchr.js
// Generated by CoffeeScript 1.3.3
(function() {
var changes, cwd, path, watchPath, watchr,
__slice = [].slice;
var changes, cwd, path, watchPath, watchr,
__slice = [].slice;
path = require('path');
path = require('path');
watchr = require(__dirname + '/../lib/watchr');
watchr = require(__dirname + '/../lib/watchr');
cwd = process.cwd();
cwd = process.cwd();
watchPath = process.argv.length === 3 ? path.resolve(cwd, process.argv[2]) : cwd;
watchPath = process.argv.length === 3 ? path.resolve(cwd, process.argv[2]) : cwd;
changes = 0;
changes = 0;
watchr.watch({
path: watchPath,
listener: function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return console.log('a watch event occured:', ++changes, ':', args);
},
next: function(err, watcher) {
if (err) {
throw err;
}
return console.log('now watching:', watchPath);
watchr.watch({
path: watchPath,
listener: function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return console.log('a watch event occured:', ++changes, ':', args);
},
next: function(err, watcher) {
if (err) {
throw err;
}
});
}).call(this);
return console.log('now watching:', watchPath);
}
});
// Generated by CoffeeScript 1.3.3
/*

@@ -12,404 +11,409 @@ Watchr is used to be nofitied when a change happens to, or within a directory.

var EventEmitter, Watcher, balUtil, createWatcher, debug, fsUtil, pathUtil, watch, watchers,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
__slice = [].slice,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
(function() {
var EventEmitter, Watcher, balUtil, createWatcher, debug, fsUtil, pathUtil, watch, watchers,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
__slice = [].slice,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
pathUtil = require('path');
pathUtil = require('path');
fsUtil = require('fs');
fsUtil = require('fs');
balUtil = require('bal-util');
balUtil = require('bal-util');
EventEmitter = require('events').EventEmitter;
EventEmitter = require('events').EventEmitter;
debug = false;
debug = false;
watchers = {};
watchers = {};
Watcher = (function(_super) {
Watcher = (function(_super) {
__extends(_Class, _super);
__extends(_Class, _super);
_Class.prototype.path = null;
_Class.prototype.path = null;
_Class.prototype.isDirectory = null;
_Class.prototype.isDirectory = null;
_Class.prototype.stat = null;
_Class.prototype.stat = null;
_Class.prototype.fswatcher = null;
_Class.prototype.fswatcher = null;
_Class.prototype.children = null;
_Class.prototype.children = null;
_Class.prototype.state = 'pending';
_Class.prototype.state = 'pending';
_Class.prototype.method = null;
_Class.prototype.method = null;
_Class.prototype.config = null;
_Class.prototype.config = null;
function _Class(config, next) {
var applyStat, listener, watcher, _base, _base1, _base2, _base3, _i, _len, _ref, _ref1, _ref2, _ref3, _ref4, _ref5,
_this = this;
_ref = balUtil.extractOptsAndCallback(config, next), config = _ref[0], next = _ref[1];
watcher = this;
this.children = {};
applyStat = function(stat) {
_this.stat = stat;
_this.isDirectory = stat.isDirectory();
return _this.watch(function(err) {
return typeof config.next === "function" ? config.next(err, watcher) : void 0;
});
};
this.path = config.path;
this.config = config;
if ((_ref1 = (_base = this.config).ignoreHiddenFiles) == null) {
_base.ignoreHiddenFiles = false;
function _Class(config, next) {
var applyStat, listener, watcher, _base, _base1, _base2, _base3, _i, _len, _ref, _ref1, _ref2, _ref3, _ref4, _ref5,
_this = this;
_ref = balUtil.extractOptsAndCallback(config, next), config = _ref[0], next = _ref[1];
watcher = this;
this.children = {};
applyStat = function(stat) {
_this.stat = stat;
_this.isDirectory = stat.isDirectory();
return _this.watch(function(err) {
return typeof next === "function" ? next(err, watcher) : void 0;
});
};
this.path = config.path;
this.config = config;
if ((_ref1 = (_base = this.config).ignoreHiddenFiles) == null) {
_base.ignoreHiddenFiles = false;
}
if ((_ref2 = (_base1 = this.config).ignorePatterns) == null) {
_base1.ignorePatterns = false;
}
if ((_ref3 = (_base2 = this.config).interval) == null) {
_base2.interval = 100;
}
if ((_ref4 = (_base3 = this.config).persistent) == null) {
_base3.persistent = true;
}
if (config.listener) {
this.listen(config.listener);
}
if (config.listeners) {
_ref5 = config.listeners;
for (_i = 0, _len = _ref5.length; _i < _len; _i++) {
listener = _ref5[_i];
this.listen(listener);
}
if ((_ref2 = (_base1 = this.config).ignorePatterns) == null) {
_base1.ignorePatterns = false;
}
if ((_ref3 = (_base2 = this.config).interval) == null) {
_base2.interval = 100;
}
if ((_ref4 = (_base3 = this.config).persistent) == null) {
_base3.persistent = true;
}
if (config.listener) {
this.listen(config.listener);
}
if (config.listeners) {
_ref5 = config.listeners;
for (_i = 0, _len = _ref5.length; _i < _len; _i++) {
listener = _ref5[_i];
this.listen(listener);
}
if (config.stat) {
applyStat(config.stat);
} else {
balUtil.stat(config.path, function(err, stat) {
if (watcher.state !== 'pending') {
return;
}
}
if (config.stat) {
applyStat(config.stat);
} else {
balUtil.stat(config.path, function(err, stat) {
if (watcher.state !== 'pending') {
return;
}
if (err) {
throw err;
}
return applyStat(stat);
});
}
if (err) {
throw err;
}
return applyStat(stat);
});
}
}
_Class.prototype.listen = function(listener) {
this.removeListener('changed', listener);
this.on('changed', listener);
if (debug) {
console.log("added a listener: on " + this.path);
}
return this;
};
_Class.prototype.listen = function(listener) {
this.removeListener('changed', listener);
this.on('changed', listener);
if (debug) {
console.log("added a listener: on " + this.path);
}
return this;
};
_Class.prototype.bubble = function() {
var args, currentStat, eventName, filename, previousStat;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
eventName = args[0], filename = args[1], currentStat = args[2], previousStat = args[3];
if (debug) {
console.log("bubble: " + eventName + ": " + filename + " on " + this.path);
_Class.prototype.bubble = function() {
var args, currentStat, eventName, filename, previousStat;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
eventName = args[0], filename = args[1], currentStat = args[2], previousStat = args[3];
if (debug) {
console.log("bubble: " + eventName + ": " + filename + " on " + this.path);
}
this.emit('changed', eventName, filename, currentStat, previousStat);
return this;
};
_Class.prototype.changed = function() {
var args, currentStat, determineTheChange, fileExists, fileFullPath, isTheSame, me, previousStat,
_this = this;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
me = this;
fileFullPath = this.path;
currentStat = null;
previousStat = this.stat;
fileExists = null;
if (debug) {
console.log("watch event triggered on " + this.path + "\n", args);
}
isTheSame = function() {
if ((currentStat != null) && (previousStat != null)) {
if (currentStat.size === previousStat.size && currentStat.mtime.toString() === previousStat.mtime.toString()) {
return true;
}
}
this.emit('changed', eventName, filename, currentStat, previousStat);
return this;
return false;
};
_Class.prototype.changed = function() {
var args, currentStat, determineTheChange, fileExists, fileFullPath, isTheSame, me, previousStat,
_this = this;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
me = this;
fileFullPath = this.path;
currentStat = null;
previousStat = this.stat;
fileExists = null;
if (debug) {
console.log("watch event triggered on " + this.path + "\n", args);
}
isTheSame = function() {
if ((currentStat != null) && (previousStat != null)) {
if (currentStat.size === previousStat.size && currentStat.mtime.toString() === previousStat.mtime.toString()) {
return true;
}
determineTheChange = function() {
if (!fileExists) {
if (debug) {
console.log('determined unlink:', fileFullPath);
}
return false;
};
determineTheChange = function() {
if (!fileExists) {
return _this.close('unlink');
} else {
if (isTheSame()) {
if (debug) {
console.log('determined unlink:', fileFullPath);
return console.log("determined same:", fileFullPath);
}
return _this.close('unlink');
} else {
if (isTheSame()) {
if (debug) {
return console.log("determined same:", fileFullPath);
}
} else {
if (_this.isDirectory) {
if (isTheSame() === false) {
return balUtil.readdir(fileFullPath, function(err, newFileRelativePaths) {
if (err) {
throw err;
}
balUtil.each(newFileRelativePaths, function(newFileRelativePath) {
var newFileFullPath;
if (_this.children[newFileRelativePath] != null) {
if (_this.isDirectory) {
if (isTheSame() === false) {
return balUtil.readdir(fileFullPath, function(err, newFileRelativePaths) {
if (err) {
throw err;
}
balUtil.each(newFileRelativePaths, function(newFileRelativePath) {
var newFileFullPath;
if (_this.children[newFileRelativePath] != null) {
} else {
newFileFullPath = pathUtil.join(fileFullPath, newFileRelativePath);
return balUtil.stat(newFileFullPath, function(err, newFileStat) {
if (err) {
throw err;
}
if (debug) {
console.log('determined new:', newFileFullPath);
}
_this.emit('changed', 'new', newFileFullPath, newFileStat, null);
return _this.watchChild(newFileFullPath, newFileRelativePath, newFileStat);
});
}
});
return balUtil.each(_this.children, function(childFileWatcher, childFileRelativePath) {
var childFileFullPath;
if (__indexOf.call(newFileRelativePaths, childFileRelativePath) >= 0) {
} else {
childFileFullPath = childFileWatcher.path;
} else {
newFileFullPath = pathUtil.join(fileFullPath, newFileRelativePath);
return balUtil.stat(newFileFullPath, function(err, newFileStat) {
if (err) {
throw err;
}
if (debug) {
console.log('determined unlink:', childFileRelativePath);
console.log('determined new:', newFileFullPath);
}
return _this.closeChild(childFileRelativePath, 'unlink');
_this.emit('changed', 'new', newFileFullPath, newFileStat, null);
return _this.watchChild(newFileFullPath, newFileRelativePath, newFileStat);
});
}
});
return balUtil.each(_this.children, function(childFileWatcher, childFileRelativePath) {
var childFileFullPath;
if (__indexOf.call(newFileRelativePaths, childFileRelativePath) >= 0) {
} else {
childFileFullPath = childFileWatcher.path;
if (debug) {
console.log('determined unlink:', childFileRelativePath);
}
});
return _this.closeChild(childFileRelativePath, 'unlink');
}
});
}
} else {
if (debug) {
console.log('determined change:', fileFullPath);
}
return _this.emit('changed', 'change', fileFullPath, currentStat, previousStat);
});
}
} else {
if (debug) {
console.log('determined change:', fileFullPath);
}
return _this.emit('changed', 'change', fileFullPath, currentStat, previousStat);
}
}
};
balUtil.exists(fileFullPath, function(exists) {
fileExists = exists;
if (fileExists) {
return balUtil.stat(fileFullPath, function(err, stat) {
if (err) {
throw err;
}
currentStat = stat;
me.stat = currentStat;
return determineTheChange();
});
} else {
}
};
balUtil.exists(fileFullPath, function(exists) {
fileExists = exists;
if (fileExists) {
return balUtil.stat(fileFullPath, function(err, stat) {
if (err) {
throw err;
}
currentStat = stat;
me.stat = currentStat;
return determineTheChange();
}
});
return this;
};
_Class.prototype.close = function(type) {
var childRelativePath, watchr, _ref;
if (this.state !== 'active') {
return this;
}
if (debug) {
console.log("close: " + this.path + " ", new Error('trace').stack);
}
_ref = this.children;
for (childRelativePath in _ref) {
if (!__hasProp.call(_ref, childRelativePath)) continue;
watchr = _ref[childRelativePath];
this.closeChild(childRelativePath, type);
}
if (this.method === 'watchFile') {
fsUtil.unwatchFile(this.path);
} else if (this.method === 'watch' && this.fswatcher) {
this.fswatcher.close();
this.fswatcher = null;
}
if (type === 'unlink') {
this.emit('changed', 'unlink', this.path, null, this.stat);
this.state = 'unlink';
});
} else {
this.state = 'closed';
return determineTheChange();
}
if (watchers[this.path] != null) {
delete watchers[this.path];
}
});
return this;
};
_Class.prototype.close = function(type) {
var childRelativePath, watchr, _ref;
if (this.state !== 'active') {
return this;
};
}
if (debug) {
console.log("close: " + this.path + " ", new Error('trace').stack);
}
_ref = this.children;
for (childRelativePath in _ref) {
if (!__hasProp.call(_ref, childRelativePath)) continue;
watchr = _ref[childRelativePath];
this.closeChild(childRelativePath, type);
}
if (this.method === 'watchFile') {
fsUtil.unwatchFile(this.path);
} else if (this.method === 'watch' && this.fswatcher) {
this.fswatcher.close();
this.fswatcher = null;
}
if (type === 'unlink') {
this.emit('changed', 'unlink', this.path, null, this.stat);
this.state = 'unlink';
} else {
this.state = 'closed';
}
if (watchers[this.path] != null) {
delete watchers[this.path];
}
return this;
};
_Class.prototype.closeChild = function(fileRelativePath, type) {
var watcher;
watcher = this.children[fileRelativePath];
if (watcher) {
watcher.close(type);
delete this.children[fileRelativePath];
_Class.prototype.closeChild = function(fileRelativePath, type) {
var watcher;
watcher = this.children[fileRelativePath];
if (watcher) {
watcher.close(type);
delete this.children[fileRelativePath];
}
return this;
};
_Class.prototype.watchChild = function(fileFullPath, fileRelativePath, fileStat, next) {
var config, me;
me = this;
config = this.config;
return watch({
path: fileFullPath,
listener: function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
if (args.length > 3 && args[0] === 'changed' && args[1] === 'unlink' && args[2] === fileFullPath) {
this.closeChild(fileRelativePath, 'unlink');
}
return me.bubble.apply(me, args);
},
stat: fileStat,
ignoreHiddenFiles: config.ignoreHiddenFiles,
ignorePatterns: config.ignorePatterns,
next: function(err, watcher) {
if (err) {
return typeof next === "function" ? next(err) : void 0;
}
me.children[fileRelativePath] = watcher;
return typeof next === "function" ? next() : void 0;
}
return this;
};
});
};
_Class.prototype.watchChild = function(fileFullPath, fileRelativePath, fileStat, next) {
var config, me;
me = this;
config = this.config;
return watch({
path: fileFullPath,
listener: function() {
_Class.prototype.watch = function(next) {
var config, me, startWatching,
_this = this;
me = this;
config = this.config;
this.close();
if (debug) {
console.log("watch: " + this.path, new Error('trace').stack);
}
startWatching = function() {
var tasks, watchFileOpts;
tasks = new balUtil.Group(function(err) {
return typeof next === "function" ? next(err) : void 0;
});
tasks.total = 2;
if (_this.isDirectory) {
balUtil.scandir({
path: _this.path,
ignoreHiddenFiles: config.ignoreHiddenFiles,
ignorePatterns: config.ignorePatterns,
recurse: false,
next: function(err) {
return tasks.complete(err);
},
action: function(fileFullPath, fileRelativePath, nextFile, fileStat) {
return me.watchChild(fileFullPath, fileRelativePath, fileStat, function(err) {
return nextFile(err);
});
}
});
} else {
tasks.complete();
}
try {
watchFileOpts = {
persistent: config.persistent,
interval: config.interval
};
fsUtil.watchFile(_this.path, watchFileOpts, function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
if (args.length > 3 && args[0] === 'changed' && args[1] === 'unlink' && args[2] === fileFullPath) {
this.closeChild(fileRelativePath, 'unlink');
}
return me.bubble.apply(me, args);
},
stat: fileStat,
ignoreHiddenFiles: config.ignoreHiddenFiles,
ignorePatterns: config.ignorePatterns,
next: function(err, watcher) {
if (err) {
return typeof next === "function" ? next(err) : void 0;
}
me.children[fileRelativePath] = watcher;
return typeof next === "function" ? next() : void 0;
}
});
return me.changed.apply(me, args);
});
_this.method = 'watchFile';
} catch (err) {
_this.fswatcher = fsUtil.watch(_this.path, function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return me.changed.apply(me, args);
});
_this.method = 'watch';
}
_this.state = 'active';
return tasks.complete();
};
_Class.prototype.watch = function(next) {
var config, me, startWatching,
_this = this;
me = this;
config = this.config;
this.close();
if (debug) {
console.log("watch: " + this.path, new Error('trace').stack);
balUtil.exists(this.path, function(exists) {
if (!exists) {
next();
return this;
}
startWatching = function() {
var tasks, watchFileOpts;
tasks = new balUtil.Group(function(err) {
return typeof next === "function" ? next(err) : void 0;
});
tasks.total = 2;
if (_this.isDirectory) {
balUtil.scandir({
path: _this.path,
ignoreHiddenFiles: config.ignoreHiddenFiles,
ignorePatterns: config.ignorePatterns,
recurse: false,
next: function(err) {
return tasks.complete(err);
},
action: function(fileFullPath, fileRelativePath, nextFile, fileStat) {
return me.watchChild(fileFullPath, fileRelativePath, fileStat, function(err) {
return nextFile(err);
});
}
});
} else {
tasks.complete();
}
try {
watchFileOpts = {
persistent: config.persistent,
interval: config.interval
};
fsUtil.watchFile(_this.path, watchFileOpts, function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return me.changed.apply(me, args);
});
_this.method = 'watchFile';
} catch (err) {
_this.fswatcher = fsUtil.watch(_this.path, function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return me.changed.apply(me, args);
});
_this.method = 'watch';
}
_this.state = 'active';
return tasks.complete();
};
balUtil.exists(this.path, function(exists) {
if (!exists) {
next();
return this;
}
return startWatching();
});
return this;
};
return startWatching();
});
return this;
};
return _Class;
return _Class;
})(EventEmitter);
})(EventEmitter);
createWatcher = function(opts, next) {
var path, watcher, watchr, _ref;
_ref = balUtil.extractOptsAndCallback(opts, next), opts = _ref[0], next = _ref[1];
path = opts.path;
watchr = null;
if (!balUtil.existsSync(path)) {
return;
createWatcher = function(opts, next) {
var listener, listeners, path, watcher, watchr, _i, _len, _listener, _ref;
_ref = balUtil.extractOptsAndCallback(opts, next), opts = _ref[0], next = _ref[1];
path = opts.path, listener = opts.listener, listeners = opts.listeners;
watchr = null;
if (!balUtil.existsSync(path)) {
return;
}
if (watchers[path] != null) {
watcher = watchers[path];
if (listener) {
watcher.listen(listener);
}
if (watchers[path] != null) {
watcher = watchers[path];
if (typeof next === "function") {
next(null, watcher);
if (listeners) {
for (_i = 0, _len = listeners.length; _i < _len; _i++) {
_listener = listeners[_i];
watcher.listen(_listener);
}
} else {
watcher = new Watcher(opts);
watchers[path] = watcher;
}
return watcher;
};
if (typeof next === "function") {
next(null, watcher);
}
} else {
watcher = new Watcher(opts);
watchers[path] = watcher;
}
return watcher;
};
watch = function(opts, next) {
var paths, result, tasks, _ref;
_ref = balUtil.extractOptsAndCallback(opts, next), opts = _ref[0], next = _ref[1];
paths = opts.paths;
result = null;
delete opts.paths;
if (paths instanceof Array) {
result = [];
tasks = new balUtil.Group(function(err) {
return next(err, result);
watch = function(opts, next) {
var paths, result, tasks, _ref;
_ref = balUtil.extractOptsAndCallback(opts, next), opts = _ref[0], next = _ref[1];
paths = opts.paths;
result = null;
delete opts.paths;
if (paths instanceof Array) {
result = [];
tasks = new balUtil.Group(function(err) {
return typeof next === "function" ? next(err, result) : void 0;
});
balUtil.each(paths, function(path) {
return tasks.push(function(complete) {
var localOpts, watchr;
localOpts = balUtil.extend({}, opts);
localOpts.path = path;
localOpts.next = complete;
watchr = createWatcher(localOpts);
if (watchr) {
return result.push(watchr);
}
});
balUtil.each(paths, function(path) {
return tasks.push(function(complete) {
var localOpts, watchr;
localOpts = balUtil.extend({}, opts);
localOpts.path = path;
localOpts.next = complete();
watchr = createWatcher(localOpts);
if (watchr) {
return result.push(watchr);
}
});
});
tasks.async();
} else {
result = createWatcher(opts, next);
}
return result;
};
});
tasks.async();
} else {
result = createWatcher(opts, next);
}
return result;
};
module.exports = {
watch: watch,
Watcher: Watcher
};
}).call(this);
module.exports = {
watch: watch,
Watcher: Watcher
};
{
"name": "watchr",
"version": "2.1.3",
"version": "2.1.4",
"description": "Better file system watching for Node.js",

@@ -18,3 +18,4 @@ "homepage": "https://github.com/bevry/watchr",

"contributors": [
"Benjamin Lupton <b@lupton.cc> (http://balupton.com)"
"Benjamin Lupton <b@lupton.cc> (http://balupton.com)",
"Casey Foster (https://github.com/caseywebdev)"
],

@@ -35,3 +36,3 @@ "bugs": {

"dependencies": {
"bal-util": "1.11.x"
"bal-util": "1.13.x"
},

@@ -38,0 +39,0 @@ "devDependencies": {

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