|
| 1 | +package serve |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io/fs" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/fsnotify/fsnotify" |
| 13 | + "github.com/go-errors/errors" |
| 14 | + "github.com/spf13/afero" |
| 15 | + "github.com/supabase/cli/internal/utils" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + // Debounce duration for file changes |
| 20 | + debounceDuration = 500 * time.Millisecond |
| 21 | + restartEvents = fsnotify.Write | fsnotify.Create | fsnotify.Remove | fsnotify.Rename |
| 22 | +) |
| 23 | + |
| 24 | +var ( |
| 25 | + // Directories to ignore. |
| 26 | + ignoredDirNames = []string{ |
| 27 | + ".git", |
| 28 | + "node_modules", |
| 29 | + ".vscode", |
| 30 | + ".idea", |
| 31 | + ".DS_Store", |
| 32 | + "vendor", |
| 33 | + } |
| 34 | + |
| 35 | + // Patterns for ignoring file events. |
| 36 | + ignoredFilePatterns = []struct { |
| 37 | + Prefix string // File basename prefix |
| 38 | + Suffix string // File basename suffix |
| 39 | + ExactMatch string // File basename exact match |
| 40 | + Op fsnotify.Op // Specific operation to ignore for this pattern (0 for any op) |
| 41 | + }{ |
| 42 | + {Suffix: "~"}, // Common backup files (e.g., emacs, gedit) |
| 43 | + {Prefix: ".", Suffix: ".swp"}, // Vim swap files |
| 44 | + {Prefix: ".", Suffix: ".swx"}, // Vim swap files (extended) |
| 45 | + {Prefix: "___", Suffix: "___"}, // Deno deploy/bundle temporary files often look like ___<slug>___<hash>___ |
| 46 | + {Prefix: "___"}, // Some other editor temp files might start with this |
| 47 | + {Suffix: ".tmp"}, // Generic temp files |
| 48 | + {Prefix: ".#"}, // Emacs lock files |
| 49 | + {Suffix: "___", Op: fsnotify.Chmod}, // Deno specific temp file pattern during write (often involves a chmod) |
| 50 | + } |
| 51 | +) |
| 52 | + |
| 53 | +// isIgnoredFileEvent checks if a file event should be ignored based on predefined patterns. |
| 54 | +func isIgnoredFileEvent(eventName string, eventOp fsnotify.Op) bool { |
| 55 | + baseName := filepath.Base(eventName) |
| 56 | + for _, p := range ignoredFilePatterns { |
| 57 | + match := false |
| 58 | + if p.ExactMatch != "" && baseName == p.ExactMatch { |
| 59 | + match = true |
| 60 | + } else { |
| 61 | + // Check prefix if specified |
| 62 | + prefixMatch := p.Prefix == "" || strings.HasPrefix(baseName, p.Prefix) |
| 63 | + // Check suffix if specified |
| 64 | + suffixMatch := p.Suffix == "" || strings.HasSuffix(baseName, p.Suffix) |
| 65 | + |
| 66 | + // Both prefix and suffix must match |
| 67 | + if p.Prefix != "" && p.Suffix != "" { |
| 68 | + match = prefixMatch && suffixMatch |
| 69 | + // Only prefix specified |
| 70 | + } else if p.Prefix != "" { |
| 71 | + match = prefixMatch |
| 72 | + // Only suffix specified |
| 73 | + } else if p.Suffix != "" { |
| 74 | + match = suffixMatch |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if match { |
| 79 | + // If Op is 0, it means the pattern applies to any operation. |
| 80 | + // Otherwise, check if the event's operation is relevant to the pattern's Op. |
| 81 | + if p.Op == 0 || (eventOp&p.Op) != 0 { |
| 82 | + return true |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + return false |
| 87 | +} |
| 88 | + |
| 89 | +type debounceFileWatcher struct { |
| 90 | + watcher *fsnotify.Watcher |
| 91 | + restartTimer *time.Timer |
| 92 | + RestartCh <-chan time.Time |
| 93 | + ErrCh <-chan error |
| 94 | +} |
| 95 | + |
| 96 | +func NewDebounceFileWatcher() (*debounceFileWatcher, error) { |
| 97 | + restartTimer := time.NewTimer(debounceDuration) |
| 98 | + if !restartTimer.Stop() { |
| 99 | + return nil, errors.New("failed to initialise timer") |
| 100 | + } |
| 101 | + watcher, err := fsnotify.NewWatcher() |
| 102 | + if err != nil { |
| 103 | + return nil, errors.Errorf("failed to create file watcher: %w", err) |
| 104 | + } |
| 105 | + return &debounceFileWatcher{ |
| 106 | + watcher: watcher, |
| 107 | + ErrCh: watcher.Errors, |
| 108 | + restartTimer: restartTimer, |
| 109 | + RestartCh: restartTimer.C, |
| 110 | + }, nil |
| 111 | +} |
| 112 | + |
| 113 | +func (w *debounceFileWatcher) Start(ctx context.Context) { |
| 114 | + for { |
| 115 | + event, ok := <-w.watcher.Events |
| 116 | + if !ok { |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + if !event.Has(restartEvents) || isIgnoredFileEvent(event.Name, event.Op) { |
| 121 | + fmt.Fprintf(utils.GetDebugLogger(), "Ignoring file event: %s (%s)\n", event.Name, event.Op.String()) |
| 122 | + continue |
| 123 | + } |
| 124 | + |
| 125 | + fileName := filepath.Base(event.Name) |
| 126 | + if fileName == "config.toml" { |
| 127 | + fmt.Fprintf(os.Stderr, "Config file change detected: %s (%s) - will reload configuration\n", event.Name, event.Op.String()) |
| 128 | + } else { |
| 129 | + fmt.Fprintf(os.Stderr, "File change detected: %s (%s)\n", event.Name, event.Op.String()) |
| 130 | + } |
| 131 | + |
| 132 | + if !w.restartTimer.Reset(debounceDuration) { |
| 133 | + fmt.Fprintln(utils.GetDebugLogger(), "Failed to restart debounce timer.") |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func (w *debounceFileWatcher) SetWatchPaths(watchPaths []string, fsys afero.Fs) error { |
| 139 | + shouldWatchDirs := make(map[string]struct{}) |
| 140 | + for _, hostPath := range watchPaths { |
| 141 | + // Ignore non-existent paths |
| 142 | + if err := afero.Walk(fsys, hostPath, func(path string, info fs.FileInfo, err error) error { |
| 143 | + if err != nil { |
| 144 | + return errors.New(err) |
| 145 | + } |
| 146 | + if path == hostPath || info.IsDir() { |
| 147 | + shouldWatchDirs[path] = struct{}{} |
| 148 | + } |
| 149 | + return nil |
| 150 | + }); err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + } |
| 154 | + // Add directories to watch, ignoring duplicates |
| 155 | + for hostPath := range shouldWatchDirs { |
| 156 | + if err := w.watcher.Add(hostPath); err != nil { |
| 157 | + return errors.Errorf("failed to watch directory: %w", err) |
| 158 | + } |
| 159 | + fmt.Fprintln(utils.GetDebugLogger(), "Added directory from watcher:", hostPath) |
| 160 | + } |
| 161 | + // Remove directories that are no longer needed |
| 162 | + for _, hostPath := range w.watcher.WatchList() { |
| 163 | + if _, ok := shouldWatchDirs[hostPath]; !ok { |
| 164 | + if err := w.watcher.Remove(hostPath); err != nil { |
| 165 | + return errors.Errorf("failed to remove watch directory: %w", err) |
| 166 | + } |
| 167 | + fmt.Fprintln(utils.GetDebugLogger(), "Removed directory from watcher:", hostPath) |
| 168 | + } |
| 169 | + } |
| 170 | + return nil |
| 171 | +} |
| 172 | + |
| 173 | +func (r *debounceFileWatcher) Close() error { |
| 174 | + if r.watcher != nil { |
| 175 | + return r.watcher.Close() |
| 176 | + } |
| 177 | + if r.restartTimer != nil { |
| 178 | + r.restartTimer.Stop() |
| 179 | + } |
| 180 | + return nil |
| 181 | +} |
0 commit comments