Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Watch parent directories on Windows #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/path-watcher-manager.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require('path')
const {PathWatcher} = require('./path-watcher')
const {NativeWatcher} = require('./native-watcher')
const {NativeWatcherRegistry} = require('./native-watcher-registry')
Expand Down Expand Up @@ -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
}

Expand Down