From d5c14552942efcf24344d200c13ce048799b92e6 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Sat, 15 Jul 2017 10:24:35 +0200 Subject: [PATCH] Handle directory added event correctly --- lib/DirectoryWatcher.js | 45 ++++++++++++++++++++++++------------- test/Watchpack.js | 50 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 16 deletions(-) diff --git a/lib/DirectoryWatcher.js b/lib/DirectoryWatcher.js index 58389a1..2aea0df 100644 --- a/lib/DirectoryWatcher.js +++ b/lib/DirectoryWatcher.js @@ -120,24 +120,37 @@ DirectoryWatcher.prototype.setFileTime = function setFileTime(filePath, mtime, i }; DirectoryWatcher.prototype.setDirectory = function setDirectory(directoryPath, exist, initial, type) { - var old = this.directories[directoryPath]; - if(!old) { - if(exist) { - if(this.nestedWatching) { - this.createNestedWatcher(directoryPath); - } else { - this.directories[directoryPath] = true; - } + if(directoryPath === this.path) { + if(!initial && this.watchers[withoutCase(this.path)]) { + this.watchers[withoutCase(this.path)].forEach(function(w) { + w.emit("change", directoryPath, w.data, initial ? "initial" : type); + }); } } else { - if(!exist) { - if(this.nestedWatching) - this.directories[directoryPath].close(); - delete this.directories[directoryPath]; - if(!initial && this.watchers[withoutCase(this.path)]) { - this.watchers[withoutCase(this.path)].forEach(function(w) { - w.emit("change", directoryPath, w.data, initial ? "initial" : type); - }); + var old = this.directories[directoryPath]; + if(!old) { + if(exist) { + if(this.nestedWatching) { + this.createNestedWatcher(directoryPath); + } else { + this.directories[directoryPath] = true; + } + if(!initial && this.watchers[withoutCase(this.path)]) { + this.watchers[withoutCase(this.path)].forEach(function(w) { + w.emit("change", directoryPath, w.data, initial ? "initial" : type); + }); + } + } + } else { + if(!exist) { + if(this.nestedWatching) + this.directories[directoryPath].close(); + delete this.directories[directoryPath]; + if(!initial && this.watchers[withoutCase(this.path)]) { + this.watchers[withoutCase(this.path)].forEach(function(w) { + w.emit("change", directoryPath, w.data, initial ? "initial" : type); + }); + } } } } diff --git a/test/Watchpack.js b/test/Watchpack.js index 232b3d3..0ef4888 100644 --- a/test/Watchpack.js +++ b/test/Watchpack.js @@ -194,6 +194,56 @@ describe("Watchpack", function() { }); }); + it("should watch a missing directory", function(done) { + var w = new Watchpack({ + aggregateTimeout: 1000 + }); + var changeEvents = []; + w.on("change", function(file) { + if(changeEvents[changeEvents.length - 1] === file) + return; + changeEvents.push(file); + }); + w.on("aggregated", function(changes) { + changes.should.be.eql([path.join(fixtures, "dir", "sub")]); + changeEvents.should.be.eql([path.join(fixtures, "dir", "sub")]); + w.close(); + done(); + }); + testHelper.dir("dir"); + testHelper.tick(function() { + w.watch([], [path.join(fixtures, "dir", "sub")]); + testHelper.tick(function() { + testHelper.dir(path.join("dir", "sub")); + }); + }); + }); + + it("should watch a directory (add directory)", function(done) { + var w = new Watchpack({ + aggregateTimeout: 1000 + }); + var changeEvents = []; + w.on("change", function(file) { + if(changeEvents[changeEvents.length - 1] === file) + return; + changeEvents.push(file); + }); + w.on("aggregated", function(changes) { + changes.should.be.eql([path.join(fixtures, "dir")]); + changeEvents.should.be.eql([path.join(fixtures, "dir", "sub")]); + w.close(); + done(); + }); + testHelper.dir("dir"); + testHelper.tick(function() { + w.watch([], [path.join(fixtures, "dir")]); + testHelper.tick(function() { + testHelper.dir(path.join("dir", "sub")); + }); + }); + }); + it("should watch a directory (delete directory)", function(done) { var w = new Watchpack({ aggregateTimeout: 1000