watchForFileChanges method
Future<void>
watchForFileChanges(
)
Implementation
Future<void> watchForFileChanges() async {
logger.detail('Watching ${root.path} for changes');
if (_watcherSubscription != null) {
return;
}
// Watch the root directory
_watcherSubscription = DirectoryWatcher(root.path).events
.asyncMap((event) async {
final WatchEvent(:type, :path) = event;
if (type == ChangeType.REMOVE) {
await onFileRemove(path);
} else {
await onFilesChange([path]);
}
return event;
})
.debounce(Duration.zero)
.listen((event) {
final WatchEvent(:type, :path) = event;
_reload(path);
});
_watcherSubscription
?.asFuture<void>()
.then((_) async {
logger.detail('Root directory watcher closed normally');
await _cancelWatcherSubscription();
await stop();
})
.catchError((Object e, StackTrace st) async {
logger
..err('File watcher error (root directory): $e')
..detail('Stack trace: $st');
await _cancelWatcherSubscription();
await stop(1);
})
.ignore();
// Watch dependency directories if available
if (getDependencyDirectories case final getDependencyDirectories?
when _dependencyWatchers.isEmpty) {
final dependencyDirs = await getDependencyDirectories();
for (final dir in dependencyDirs) {
logger.detail('Watching dependency directory: $dir');
final watcher = DirectoryWatcher(dir).events
.asyncMap((event) async {
final WatchEvent(:type, :path) = event;
// Only watch for Dart files in dependency directories
if (p.extension(path) == '.dart') {
if (type == ChangeType.REMOVE) {
await onFileRemove(path);
} else {
await onFilesChange([path]);
}
}
return event;
})
.debounce(Duration.zero)
.listen((event) {
final WatchEvent(:type, :path) = event;
if (p.extension(path) == '.dart') {
_reload(path);
}
});
_dependencyWatchers.add(watcher);
watcher
.asFuture<void>()
.then((_) async {
logger.detail('Dependency watcher closed: $dir');
await _cancelWatcherSubscription();
await stop();
})
.catchError((Object e, StackTrace st) async {
logger
..err('File watcher error (dependency $dir): $e')
..detail('Stack trace: $st');
await _cancelWatcherSubscription();
await stop(1);
})
.ignore();
}
}
}