From 08f037ce53cf009c3357d93e579f19fe66961aec Mon Sep 17 00:00:00 2001 From: "David Humphrey (:humph) david.humphrey@senecacollege.ca" Date: Mon, 14 Jul 2014 16:46:52 -0400 Subject: [PATCH] Follow-up fix for issue #235, deal with '/' when adding trailing slash --- src/fs-watcher.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/fs-watcher.js b/src/fs-watcher.js index 1ecd61a..e236bcb 100644 --- a/src/fs-watcher.js +++ b/src/fs-watcher.js @@ -10,11 +10,12 @@ function FSWatcher() { EventEmitter.call(this); var self = this; var recursive = false; + var recursivePathPrefix; var filename; function onchange(path) { - // Watch for exact filename, or parent path when recursive is true - if(filename === path || (recursive && path.indexOf(filename + '/') === 0)) { + // Watch for exact filename, or parent path when recursive is true. + if(filename === path || (recursive && path.indexOf(recursivePathPrefix) === 0)) { self.trigger('change', 'change', path); } } @@ -38,6 +39,12 @@ function FSWatcher() { // Whether to watch beneath this path or not recursive = recursive_ === true; + // If recursive, construct a path prefix portion for comparisons later + // (i.e., '/path' becomes '/path/' so we can search within a filename for the + // prefix). We also take care to allow for '/' on its own. + if(recursive) { + recursivePathPrefix = filename === '/' ? '/' : filename + '/'; + } var intercom = Intercom.getInstance(); intercom.on('change', onchange);