diff --git a/lib/test/testing_util.js b/lib/test/testing_util.js index 6a32756..570ed58 100644 --- a/lib/test/testing_util.js +++ b/lib/test/testing_util.js @@ -1,28 +1,31 @@ (function() { - var EventBuffer, check_exec, check_exec_options, exec, listsContainSameElements, sys; - var __slice = Array.prototype.slice; + var EventBuffer, check_exec, check_exec_options, exec, listsContainSameElements, util, + __slice = Array.prototype.slice; + exec = require('child_process').exec; - sys = require('sys'); + + util = (function() { + try { + return require('util'); + } catch (e) { + return require('sys'); + } + })(); + exports.check_exec_options = check_exec_options = function(cmd, options, callback) { return exec(cmd, options, function(e, stdout, stderr) { - if (e) { - throw e; - } - if (callback) { - return callback(); - } + if (e) throw e; + if (callback) return callback(); }); }; + exports.check_exec = check_exec = function(cmd, callback) { return exec(cmd, function(e, stdout, stderr) { - if (e) { - throw e; - } - if (callback) { - return callback(); - } + if (e) throw e; + if (callback) return callback(); }); }; + exports.listsContainSameElements = listsContainSameElements = function(t, arr1, arr2) { var d1, d2, x, _i, _j, _len, _len2; d1 = {}; @@ -37,27 +40,29 @@ } return t.deepEqual(d1, d2); }; + exports.EventBuffer = EventBuffer = (function() { + function EventBuffer() { this.stack = []; this.callback = null; } + EventBuffer.prototype.wait = function(callback) { var event; if (this.stack.length > 0) { event = this.stack.pop(); return callback(event); } else { - if (this.callback) { - throw new Error("Only store one callback"); - } + if (this.callback) throw new Error("Only store one callback"); return this.callback = callback; } }; + EventBuffer.prototype.expect = function() { var args, t; t = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; - sys.debug("Expecting " + args[0] + "..."); + util.debug("Expecting " + args[0] + "..."); return this.wait(function(event) { var i, x, _len, _ref; _ref = args.slice(0, -1); @@ -68,6 +73,7 @@ return args.slice(-1)[0](event); }); }; + EventBuffer.prototype.event = function(event) { var callback; if (this.callback) { @@ -78,6 +84,9 @@ return this.stack.push(event); } }; + return EventBuffer; + })(); + }).call(this); diff --git a/lib/watchers/stat.js b/lib/watchers/stat.js index d6797db..95d3ea6 100644 --- a/lib/watchers/stat.js +++ b/lib/watchers/stat.js @@ -1,48 +1,53 @@ (function() { - var ENOENT, ENOTDIR, Paths, StatWatcher, assert, async, events, fs, pathsIn, _pathsIn; - var __hasProp = Object.prototype.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; - }, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; + var Paths, StatWatcher, assert, async, events, fs, pathsIn, _pathsIn, + __hasProp = Object.prototype.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; }; + fs = require('fs'); + events = require('events'); + assert = require('assert'); + async = require('async'); - ENOENT = 2; - ENOTDIR = 20; + Paths = (function() { + function Paths() { this.items = []; this.itemsDict = {}; this.numItems = 0; this.pos = 0; } + Paths.prototype.add = function(x) { assert.ok(!this.contains(x)); this.items.push(x); this.numItems += 1; return this.itemsDict[x] = true; }; + Paths.prototype.contains = function(x) { return this.itemsDict[x] != null; }; + Paths.prototype.next = function() { - if (this.items.length === 0) { - return null; - } + if (this.items.length === 0) return null; this.pos = (this.pos + 1) % this.numItems; return this.items[this.pos]; }; + return Paths; + })(); - exports.StatWatcher = StatWatcher = (function() { - __extends(StatWatcher, events.EventEmitter); + + exports.StatWatcher = StatWatcher = (function(_super) { + + __extends(StatWatcher, _super); + function StatWatcher(top, opt) { - var options; + var options, + _this = this; events.EventEmitter.call(this); options = opt || {}; this.ignore = opt.ignore != null ? new RegExp(opt.ignore) : null; @@ -55,108 +60,115 @@ this.numStatsPending = 0; this.preexistingPathsToReport = {}; this.numPreexistingPathsToReport = 0; - pathsIn(top, __bind(function(paths) { + pathsIn(top, function(paths) { var path, _i, _len; for (_i = 0, _len = paths.length; _i < _len; _i++) { path = paths[_i]; - if ((!this.paths.contains(path)) && (!this.ignore || !path.match(this.ignore)) && (!this.match || path.match(this.match))) { - this.preexistingPathsToReport[path] = true; - this.numPreexistingPathsToReport++; - this.paths.add(path); - this.statPath(path); + if ((!_this.paths.contains(path)) && (!_this.ignore || !path.match(_this.ignore)) && (!_this.match || path.match(_this.match))) { + _this.preexistingPathsToReport[path] = true; + _this.numPreexistingPathsToReport++; + _this.paths.add(path); + _this.statPath(path); } } - return this.intervalId = setInterval((__bind(function() { - return this.tick(); - }, this)), this.sampleRate); - }, this)); + return _this.intervalId = setInterval((function() { + return _this.tick(); + }), _this.sampleRate); + }); } + StatWatcher.prototype.end = function() { return clearInterval(this.intervalId); }; + StatWatcher.prototype.tick = function() { var path; if (this.numStatsPending <= this.maxStatsPending) { path = this.paths.next(); - if (path) { - return this.statPath(path); - } + if (path) return this.statPath(path); } }; + StatWatcher.prototype.statPath = function(path) { + var _this = this; this.numStatsPending++; - return fs.stat(path, __bind(function(err, stats) { + return fs.stat(path, function(err, stats) { var eventName, last_mtime; - this.numStatsPending--; - last_mtime = this.path_mtime[path] || null; + _this.numStatsPending--; + last_mtime = _this.path_mtime[path] || null; if (err) { - if (err.errno === ENOENT) { + if (err.code === 'ENOENT') { if (last_mtime) { - this.emit('fileDeleted', path); - delete this.path_mtime[path]; + _this.emit('fileDeleted', path); + delete _this.path_mtime[path]; } } else { throw err; } } else { - this.path_mtime[path] = stats.mtime; + _this.path_mtime[path] = stats.mtime; if (stats.isDirectory()) { - if ((!last_mtime) || (stats.mtime > last_mtime)) { - this.scanDir(path); - } + if ((!last_mtime) || (stats.mtime > last_mtime)) _this.scanDir(path); } else { if (!last_mtime) { eventName = 'fileCreated'; - if (this.preexistingPathsToReport[path]) { + if (_this.preexistingPathsToReport[path]) { eventName = 'filePreexisted'; - delete this.preexistingPathsToReport[path]; - this.numPreexistingPathsToReport--; + delete _this.preexistingPathsToReport[path]; + _this.numPreexistingPathsToReport--; } - this.emit(eventName, path, stats); + _this.emit(eventName, path, stats); } else if (stats.mtime > last_mtime) { - this.emit('fileModified', path, stats); + _this.emit('fileModified', path, stats); } } } - if (this.numPreexistingPathsToReport === 0) { - this.emit('allPreexistingFilesReported'); - return this.numPreexistingPathsToReport = -1; + if (_this.numPreexistingPathsToReport === 0) { + _this.emit('allPreexistingFilesReported'); + return _this.numPreexistingPathsToReport = -1; } - }, this)); + }); }; + StatWatcher.prototype.scanDir = function(path) { - return fs.readdir(path, __bind(function(err, files) { + var _this = this; + return fs.readdir(path, function(err, files) { var file, path2, _i, _len, _results; _results = []; for (_i = 0, _len = files.length; _i < _len; _i++) { file = files[_i]; path2 = "" + path + "/" + file; - _results.push((!this.paths.contains(path2)) && (!this.ignore || !path2.match(this.ignore)) && (!this.match || path2.match(this.match)) ? (this.paths.add(path2), this.statPath(path2)) : void 0); + if ((!_this.paths.contains(path2)) && (!_this.ignore || !path2.match(_this.ignore)) && (!_this.match || path2.match(_this.match))) { + _this.paths.add(path2); + _results.push(_this.statPath(path2)); + } else { + _results.push(void 0); + } } return _results; - }, this)); + }); }; + return StatWatcher; - })(); + + })(events.EventEmitter); + _pathsIn = function(path, paths, callback) { return fs.readdir(path, function(err, files) { - if (err && err.errno === ENOTDIR) { + if (err && err.code === 'ENOTDIR') { paths.push(path); return callback(); } - if (err) { - throw err; - } + if (err) throw err; return async.forEach(files, (function(file, cb) { return _pathsIn("" + path + "/" + file, paths, cb); }), (function(err) { - if (err) { - throw err; - } + if (err) throw err; return callback(); })); }); }; + pathsIn = function(dir, callback) { var paths; paths = []; @@ -164,4 +176,5 @@ return callback(paths); }); }; + }).call(this); diff --git a/package.json b/package.json index 7d21e20..8a3f36d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "watch-tree", - "version": "0.1.1", + "version": "0.1.2", "description": "Yet another library for watching FS trees. Includes a JSON-on-stdout command-line tool and {filePreexisted,allPreexistingFilesReported} events.", "url": "https://github.com/tafa/node-watch-tree", @@ -19,7 +19,12 @@ }, "author": "Andrew Schaaf ", - + + "maintainers": [{ + "name": "Matjaž Lipuš", + "web": "https://github.com/matjaz" + }], + "repository": { "type": "git", "url": "git://github.com/tafa/node-watch-tree.git" diff --git a/src/test/testing_util.coffee b/src/test/testing_util.coffee index 098e9d7..3dd9778 100644 --- a/src/test/testing_util.coffee +++ b/src/test/testing_util.coffee @@ -1,7 +1,6 @@ {exec} = require 'child_process' -sys = require 'sys' - +util = try require 'util' catch e then require 'sys' exports.check_exec_options = check_exec_options = (cmd, options, callback) -> exec cmd, options, (e, stdout, stderr) -> @@ -41,7 +40,7 @@ exports.EventBuffer = class EventBuffer @callback = callback expect: (t, args...) -> - sys.debug "Expecting #{args[0]}..." + util.debug "Expecting #{args[0]}..." @wait (event) -> for x, i in args[...-1] t.equal event[i], x diff --git a/src/watchers/stat.coffee b/src/watchers/stat.coffee index e0f664c..7f88d10 100644 --- a/src/watchers/stat.coffee +++ b/src/watchers/stat.coffee @@ -5,10 +5,6 @@ assert = require 'assert' async = require 'async' -ENOENT = 2 -ENOTDIR = 20 - - class Paths constructor: () -> @@ -86,7 +82,7 @@ exports.StatWatcher = class StatWatcher extends events.EventEmitter if err # file deleted - if err.errno == ENOENT + if err.code is 'ENOENT' if last_mtime @emit 'fileDeleted', path delete @path_mtime[path] @@ -139,7 +135,7 @@ _pathsIn = (path, paths, callback) -> fs.readdir path, (err, files) -> # Case: file - if err and err.errno == ENOTDIR + if err and err.code is 'ENOTDIR' paths.push path return callback()