File tree Expand file tree Collapse file tree 1 file changed +27
-1
lines changed Expand file tree Collapse file tree 1 file changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package watcher
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "io/fs"
6
7
"os"
7
8
"path/filepath"
8
9
"time"
@@ -161,9 +162,34 @@ func (w *FileWatcher) handleEvent(event fsnotify.Event) {
161
162
switch event .Op {
162
163
case fsnotify .Create , fsnotify .Write :
163
164
// Check if this is actually a file (not a directory)
164
- if info , err := os .Stat (filePath ); err == nil && ! info .IsDir () {
165
+ info , err := os .Stat (filePath )
166
+ if err == nil && ! info .IsDir () {
165
167
w .handler .OnFileChanged (filePath )
166
168
}
169
+ if err == nil && info .IsDir () {
170
+ err := w .watcher .Add (filePath )
171
+ if err != nil {
172
+ w .log .Error ().Err (err ).Str ("path" , filePath ).Msg ("failed to add directory to watcher" )
173
+ return
174
+ }
175
+ err = filepath .WalkDir (filePath , func (path string , d fs.DirEntry , err error ) error {
176
+ if err != nil {
177
+ return err
178
+ }
179
+ // Skip directories
180
+ if d .IsDir () {
181
+ return nil
182
+ }
183
+ w .handler .OnFileChanged (path )
184
+
185
+ return nil
186
+ })
187
+ if err != nil {
188
+ w .log .Error ().Err (err ).Str ("path" , filePath ).Msg ("failed to walk directory" )
189
+ return
190
+ }
191
+ }
192
+
167
193
case fsnotify .Rename , fsnotify .Remove :
168
194
w .handler .OnFileDeleted (filePath )
169
195
default :
You can’t perform that action at this time.
0 commit comments