From 1baaa865f37286f0588b95715397437ca881f54d Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Wed, 28 Feb 2018 15:12:16 +0100 Subject: [PATCH] fix watching for not-existent directories --- lib/DirectoryWatcher.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/DirectoryWatcher.js b/lib/DirectoryWatcher.js index e025479..94b539d 100644 --- a/lib/DirectoryWatcher.js +++ b/lib/DirectoryWatcher.js @@ -23,6 +23,7 @@ function Watcher(directoryWatcher, filePath, startTime) { this.directoryWatcher = directoryWatcher; this.path = filePath; this.startTime = startTime && +startTime; + // TODO this.data seem to be only read, weird this.data = 0; } @@ -72,6 +73,7 @@ function DirectoryWatcher(directoryPath, options) { this.initialScanRemoved = []; this.doInitialScan(); this.watchers = Object.create(null); + this.parentWatcher = null; this.refs = 0; } module.exports = DirectoryWatcher; @@ -142,6 +144,11 @@ DirectoryWatcher.prototype.setDirectory = function setDirectory(directoryPath, e w.emit("change", directoryPath, w.data, initial ? "initial" : type); }); } + if(this.watchers[withoutCase(directoryPath) + "#directory"]) { + this.watchers[withoutCase(directoryPath) + "#directory"].forEach(function(w) { + w.emit("change", w.data, initial ? "initial" : type); + }); + } } } else { if(!exist) { @@ -153,6 +160,11 @@ DirectoryWatcher.prototype.setDirectory = function setDirectory(directoryPath, e w.emit("change", directoryPath, w.data, initial ? "initial" : type); }); } + if(this.watchers[withoutCase(directoryPath) + "#directory"]) { + this.watchers[withoutCase(directoryPath) + "#directory"].forEach(function(w) { + w.emit("change", directoryPath, w.data, initial ? "initial" : type); + }); + } } } } @@ -274,6 +286,14 @@ DirectoryWatcher.prototype.onWatcherError = function onWatcherError(/* err */) { DirectoryWatcher.prototype.doInitialScan = function doInitialScan() { fs.readdir(this.path, function(err, items) { if(err) { + this.parentWatcher = watcherManager.watchFile(this.path + "#directory", this.options, 1); + this.parentWatcher.on("change", function(mtime, type) { + if(this.watchers[withoutCase(this.path)]) { + this.watchers[withoutCase(this.path)].forEach(function(w) { + w.emit("change", this.path, mtime, type); + }); + } + }.bind(this)); this.initialScan = false; return; } @@ -340,6 +360,7 @@ DirectoryWatcher.prototype.close = function() { this.directories[dir].close(); }, this); } + if(this.parentWatcher) this.parentWatcher.close(); this.emit("closed"); };