From 17b15d61c53ea7a2faa833ae03e41ef4e7a655c3 Mon Sep 17 00:00:00 2001 From: Termina1 Date: Sat, 8 Dec 2012 16:39:19 +0400 Subject: [PATCH] fix error codes to names and added gitignore node_modules --- .gitignore | 1 + lib/command.js | 5 ++ lib/test/testing_util.js | 26 +++++++-- lib/test/tests.js | 12 ++++ lib/watch-tree.js | 5 ++ lib/watchers/stat.js | 120 ++++++++++++++++++++++++--------------- src/watchers/stat.coffee | 8 +-- 7 files changed, 121 insertions(+), 56 deletions(-) diff --git a/.gitignore b/.gitignore index 8699ebe..e6e4b38 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store lib/test/temp +node_modules diff --git a/lib/command.js b/lib/command.js index 2c9a110..e47ce6e 100644 --- a/lib/command.js +++ b/lib/command.js @@ -1,7 +1,11 @@ +// Generated by CoffeeScript 1.3.3 (function() { var watch_tree, _; + _ = require('underscore'); + watch_tree = require('./watch-tree'); + exports.main = function() { var arg, argv, path, w; argv = require('optimist').argv; @@ -16,4 +20,5 @@ }); }); }; + }).call(this); diff --git a/lib/test/testing_util.js b/lib/test/testing_util.js index 6a32756..00db146 100644 --- a/lib/test/testing_util.js +++ b/lib/test/testing_util.js @@ -1,8 +1,12 @@ +// Generated by CoffeeScript 1.3.3 (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, sys, + __slice = [].slice; + exec = require('child_process').exec; + sys = require('sys'); + exports.check_exec_options = check_exec_options = function(cmd, options, callback) { return exec(cmd, options, function(e, stdout, stderr) { if (e) { @@ -13,6 +17,7 @@ } }); }; + exports.check_exec = check_exec = function(cmd, callback) { return exec(cmd, function(e, stdout, stderr) { if (e) { @@ -23,25 +28,29 @@ } }); }; + exports.listsContainSameElements = listsContainSameElements = function(t, arr1, arr2) { - var d1, d2, x, _i, _j, _len, _len2; + var d1, d2, x, _i, _j, _len, _len1; d1 = {}; d2 = {}; for (_i = 0, _len = arr1.length; _i < _len; _i++) { x = arr1[_i]; d1[x] = true; } - for (_j = 0, _len2 = arr2.length; _j < _len2; _j++) { + for (_j = 0, _len1 = arr2.length; _j < _len1; _j++) { x = arr2[_j]; d2[x] = true; } 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) { @@ -54,20 +63,22 @@ 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] + "..."); return this.wait(function(event) { - var i, x, _len, _ref; + var i, x, _i, _len, _ref; _ref = args.slice(0, -1); - for (i = 0, _len = _ref.length; i < _len; i++) { + for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { x = _ref[i]; t.equal(event[i], x); } return args.slice(-1)[0](event); }); }; + EventBuffer.prototype.event = function(event) { var callback; if (this.callback) { @@ -78,6 +89,9 @@ return this.stack.push(event); } }; + return EventBuffer; + })(); + }).call(this); diff --git a/lib/test/tests.js b/lib/test/tests.js index 1c732c8..c63e679 100644 --- a/lib/test/tests.js +++ b/lib/test/tests.js @@ -1,15 +1,25 @@ +// Generated by CoffeeScript 1.3.3 (function() { var EventBuffer, check_exec, check_exec_options, cwd, delay, fs, listsContainSameElements, testWatch, tmp, watch_tree, _, _ref; + fs = require('fs'); + _ = require('underscore'); + watch_tree = require('./../watch-tree'); + _ref = require('./testing_util'), check_exec_options = _ref.check_exec_options, check_exec = _ref.check_exec, EventBuffer = _ref.EventBuffer, listsContainSameElements = _ref.listsContainSameElements; + cwd = process.cwd(); + tmp = "" + cwd + "/lib/test/temp"; + console.log(tmp); + delay = function(callback) { return setTimeout(callback, 1200); }; + testWatch = function(t, options) { return check_exec("mkdir -p " + tmp, function() { return check_exec("touch " + tmp + "/temp", function() { @@ -61,6 +71,7 @@ }); }); }; + module.exports = { undefined: function(t) { return testWatch(t); @@ -84,4 +95,5 @@ }); } }; + }).call(this); diff --git a/lib/watch-tree.js b/lib/watch-tree.js index 88576d3..cecea73 100644 --- a/lib/watch-tree.js +++ b/lib/watch-tree.js @@ -1,9 +1,14 @@ +// Generated by CoffeeScript 1.3.3 (function() { var StatWatcher; + StatWatcher = require('./watchers/stat').StatWatcher; + exports.EVENTS = ['allPreexistingFilesReported', 'filePreexisted', 'fileCreated', 'fileModified', 'fileDeleted']; + exports.watchTree = function(path, options) { options = options || {}; return new StatWatcher(path, options); }; + }).call(this); diff --git a/lib/watchers/stat.js b/lib/watchers/stat.js index d6797db..82ae006 100644 --- a/lib/watchers/stat.js +++ b/lib/watchers/stat.js @@ -1,35 +1,41 @@ +// Generated by CoffeeScript 1.3.3 (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 ENOENT, ENOTDIR, Paths, StatWatcher, assert, async, events, fs, pathsIn, _pathsIn, + __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; }; + fs = require('fs'); + events = require('events'); + assert = require('assert'); + async = require('async'); - ENOENT = 2; - ENOTDIR = 20; + + ENOENT = 'ENOENT'; + + ENOTDIR = 'ENOTDIR'; + 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; @@ -37,12 +43,18 @@ 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,25 +67,27 @@ 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) { @@ -83,64 +97,76 @@ } } }; + 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); + _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(); } @@ -157,6 +183,7 @@ })); }); }; + pathsIn = function(dir, callback) { var paths; paths = []; @@ -164,4 +191,5 @@ return callback(paths); }); }; + }).call(this); diff --git a/src/watchers/stat.coffee b/src/watchers/stat.coffee index e0f664c..c198c21 100644 --- a/src/watchers/stat.coffee +++ b/src/watchers/stat.coffee @@ -5,8 +5,8 @@ assert = require 'assert' async = require 'async' -ENOENT = 2 -ENOTDIR = 20 +ENOENT = 'ENOENT' +ENOTDIR = 'ENOTDIR' class Paths @@ -86,7 +86,7 @@ exports.StatWatcher = class StatWatcher extends events.EventEmitter if err # file deleted - if err.errno == ENOENT + if err.code == ENOENT if last_mtime @emit 'fileDeleted', path delete @path_mtime[path] @@ -139,7 +139,7 @@ _pathsIn = (path, paths, callback) -> fs.readdir path, (err, files) -> # Case: file - if err and err.errno == ENOTDIR + if err and err.code == ENOTDIR paths.push path return callback()