From 715499c46418b789c263b74a91eec057f26666d3 Mon Sep 17 00:00:00 2001 From: Trevor Burnham Date: Tue, 8 Nov 2011 08:56:40 +0100 Subject: [PATCH 1/3] gitignore-ing node_modules --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 8699ebe..dff1a5e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store lib/test/temp +node_modules \ No newline at end of file From 0bfc7a8458a1c336cd94226053957aa87bc8a6ae Mon Sep 17 00:00:00 2001 From: Trevor Burnham Date: Tue, 8 Nov 2011 08:57:23 +0100 Subject: [PATCH 2/3] Using error codes instead of error numbers (fixes error under Node 0.6) --- lib/watchers/stat.js | 8 ++-- src/watchers/stat.coffee | 79 +++++++++++++++++++--------------------- 2 files changed, 40 insertions(+), 47 deletions(-) diff --git a/lib/watchers/stat.js b/lib/watchers/stat.js index d6797db..9db9315 100644 --- a/lib/watchers/stat.js +++ b/lib/watchers/stat.js @@ -1,5 +1,5 @@ (function() { - var ENOENT, ENOTDIR, Paths, StatWatcher, assert, async, events, fs, pathsIn, _pathsIn; + var 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; } @@ -12,8 +12,6 @@ events = require('events'); assert = require('assert'); async = require('async'); - ENOENT = 2; - ENOTDIR = 20; Paths = (function() { function Paths() { this.items = []; @@ -90,7 +88,7 @@ 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]; @@ -140,7 +138,7 @@ })(); _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(); } diff --git a/src/watchers/stat.coffee b/src/watchers/stat.coffee index e0f664c..cdfd532 100644 --- a/src/watchers/stat.coffee +++ b/src/watchers/stat.coffee @@ -4,30 +4,25 @@ events = require 'events' assert = require 'assert' async = require 'async' - -ENOENT = 2 -ENOTDIR = 20 - - class Paths - + constructor: () -> - + @items = [] @itemsDict = {} - + @numItems = 0 @pos = 0 - + add: (x) -> assert.ok not @contains x @items.push x @numItems += 1 @itemsDict[x] = true - + contains: (x) -> @itemsDict[x]? - + next: () -> return null if @items.length == 0 @pos = (@pos + 1) % @numItems @@ -36,100 +31,100 @@ class Paths exports.StatWatcher = class StatWatcher extends events.EventEmitter - + constructor: (top, opt) -> - + events.EventEmitter.call this - + options = opt or {} @ignore = if opt.ignore? then new RegExp opt.ignore else null @match = if opt.match? then new RegExp opt.match else null @sampleRate = if opt['sample-rate']? then (1 * opt['sample-rate']) else 5 @maxStatsPending = 10 # Does not apply to the initial scan - + @paths = new Paths() @paths.add top @path_mtime = {} @numStatsPending = 0 @preexistingPathsToReport = {} @numPreexistingPathsToReport = 0 - + pathsIn top, (paths) => for path in paths if (not @paths.contains path) and - (not @ignore or not path.match @ignore) and + (not @ignore or not path.match @ignore) and (not @match or path.match @match) - + @preexistingPathsToReport[path] = true @numPreexistingPathsToReport++ - + @paths.add path @statPath path @intervalId = setInterval (() => @tick()), @sampleRate - + end: () -> clearInterval @intervalId - + tick: () -> if @numStatsPending <= @maxStatsPending path = @paths.next() if path @statPath path - + statPath: (path) -> - + @numStatsPending++ fs.stat path, (err, stats) => @numStatsPending-- last_mtime = @path_mtime[path] or null - + if err - + # file deleted - if err.errno == ENOENT + if err.code == 'ENOENT' if last_mtime @emit 'fileDeleted', path delete @path_mtime[path] - + # error else throw err - + else - + @path_mtime[path] = stats.mtime - + # (new or modified) dir if stats.isDirectory() if (not last_mtime) or (stats.mtime > last_mtime) @scanDir path - + else - + # new file if not last_mtime - + eventName = 'fileCreated' if @preexistingPathsToReport[path] eventName = 'filePreexisted' delete @preexistingPathsToReport[path] - @numPreexistingPathsToReport-- + @numPreexistingPathsToReport-- @emit eventName, path, stats - + # modified file else if stats.mtime > last_mtime @emit 'fileModified', path, stats - + if @numPreexistingPathsToReport == 0 @emit 'allPreexistingFilesReported' @numPreexistingPathsToReport = -1 - + scanDir: (path) -> fs.readdir path, (err, files) => for file in files path2 = "#{path}/#{file}" if (not @paths.contains path2) and - (not @ignore or not path2.match @ignore) and + (not @ignore or not path2.match @ignore) and (not @match or path2.match @match) @paths.add path2 @statPath path2 @@ -137,15 +132,15 @@ exports.StatWatcher = class StatWatcher extends events.EventEmitter _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() - + # Case: error throw err if err - + # Case: dir async.forEach( files, From 847d9270e5665c7f9bb06f6d3de9cd0e84b1231f Mon Sep 17 00:00:00 2001 From: Trevor Burnham Date: Tue, 8 Nov 2011 09:01:58 +0100 Subject: [PATCH 3/3] Using require 'util' instead of require 'sys' if available Eliminates deprecation warning under Node 0.6 --- src/test/testing_util.coffee | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/testing_util.coffee b/src/test/testing_util.coffee index 098e9d7..7cbf1dd 100644 --- a/src/test/testing_util.coffee +++ b/src/test/testing_util.coffee @@ -1,6 +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) -> @@ -26,11 +26,11 @@ exports.listsContainSameElements = listsContainSameElements = (t, arr1, arr2) -> exports.EventBuffer = class EventBuffer - + constructor: () -> @stack = [] @callback = null - + wait: (callback) -> if @stack.length > 0 event = @stack.pop() @@ -39,14 +39,14 @@ exports.EventBuffer = class EventBuffer if @callback throw new Error "Only store one callback" @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 args[-1...][0](event) - + event: (event) -> if @callback callback = @callback