From a84f0785a4da26c31859522280d2fee742b4de13 Mon Sep 17 00:00:00 2001 From: Ash Wilson Date: Tue, 21 Nov 2017 08:28:37 -0500 Subject: [PATCH] Watch parent directories on Windows --- lib/path-watcher-manager.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/lib/path-watcher-manager.js b/lib/path-watcher-manager.js index ed7d8a28..9e042d72 100644 --- a/lib/path-watcher-manager.js +++ b/lib/path-watcher-manager.js @@ -1,3 +1,4 @@ +const path = require('path') const {PathWatcher} = require('./path-watcher') const {NativeWatcher} = require('./native-watcher') const {NativeWatcherRegistry} = require('./native-watcher-registry') @@ -41,6 +42,41 @@ class PathWatcherManager { createWatcher (rootPath, options, eventCallback) { const watcher = new PathWatcher(this.nativeRegistry, rootPath, options) watcher.onDidChange(eventCallback) + + if (process.platform === 'win32' && !options.poll) { + watcher.getNormalizedPathPromise().then(normalizedPath => { + let previousPath = normalizedPath + let currentPath = path.resolve(normalizedPath, '..') + while (true) { + const parentWatcher = new PathWatcher(this.nativeRegistry, currentPath, {recursive: false}) + parentWatcher.onDidChange((function (childPath) { + return events => { + for (const event of events) { + if (event.path !== childPath && event.oldPath !== childPath) continue + if (event.action !== 'deleted' && event.action !== 'renamed') continue + + parentWatcher.dispose() + eventCallback([{ + action: 'deleted', + kind: 'directory', + path: normalizedPath + }]) + watcher.dispose() + } + } + })(previousPath)) + + let nextPath = path.resolve(currentPath, '..') + if (nextPath !== currentPath) { + previousPath = currentPath + currentPath = nextPath + } else { + break + } + } + }, () => {}) + } + return watcher }