diff --git a/tools/safe-pathwatcher.js b/tools/safe-pathwatcher.js index f57d68f71c..fbda8c4049 100644 --- a/tools/safe-pathwatcher.js +++ b/tools/safe-pathwatcher.js @@ -64,7 +64,10 @@ exports.testDirectory = function (dir) { try { // Watch the candidate directory using pathwatcher.watch. - var watcher = files.pathwatcherWatch(dir, cleanUp); + var watcher = files.pathwatcherWatch(dir, function () { + cleanUp(); + switchFunctions = null; + }); } catch (err) { // If the directory did not exist, do not treat this failure as @@ -110,17 +113,19 @@ exports.watch = function (absPath, callback) { var closed = false; var switched = false; - switchFunctions.push(function switchToPolling() { - if (! switched && ! closed) { - switched = true; - watcher.close(); + if (switchFunctions) { + switchFunctions.push(function switchToPolling() { + if (! switched && ! closed) { + switched = true; + watcher.close(); - // Re-watch the file using files.watchFile instead. - files.watchFile(absPath, { - interval: pollingInterval - }, callback); - } - }); + // Re-watch the file using files.watchFile instead. + files.watchFile(absPath, { + interval: pollingInterval + }, callback); + } + }); + } return { close: function close() {