Skip to content

Commit

Permalink
ignore build folder in watch command
Browse files Browse the repository at this point in the history
part of #106
  • Loading branch information
Ivan Vlasic committed Mar 10, 2022
1 parent bca936e commit 33695cd
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions cli/controller/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package controller
import (
"os"
"os/signal"
"path/filepath"
"regexp"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -55,7 +57,9 @@ func Watch(a WatchArgs) error {
}
}

return w.run(fs.ProjectRoot())
// add separator at the end so dirs with prefix BuildDir are not matched
buildDirPath := filepath.Join(fs.ProjectRoot(), BuildDir) + string(filepath.Separator)
return w.run(fs.ProjectRoot(), []string{buildDirPath})
}

type watch struct {
Expand Down Expand Up @@ -105,7 +109,7 @@ func (w *watch) onChange() {
}
}

func (w *watch) run(path string) error {
func (w *watch) run(path string, ignoredDirs []string) error {
wr := watcher.New()
wr.SetMaxEvents(1)
wr.FilterOps(watcher.Write, watcher.Create, watcher.Remove)
Expand All @@ -114,15 +118,24 @@ func (w *watch) run(path string) error {
r := regexp.MustCompile(`\.go$`)
wr.AddFilterHook(watcher.RegexFilterHook(r, false))

isIgnoredPath := func(path string) bool {
for _, dir := range ignoredDirs {
if strings.HasPrefix(path, dir) {
return true
}
}
return false
}

ctrlc := make(chan os.Signal, 1)
signal.Notify(ctrlc, syscall.SIGINT)
go func() {
for {
select {
case e := <-wr.Event:
// due to contraints of the current watcher library finding a way to ignore automatically generated
// main.go files with other filters proved to be a challenge, adding this workaround for now.
if e.Name() == MainFile {
// build folder with other filters proved to be a challenge, adding this workaround for now.
if isIgnoredPath(e.Path) {
continue
}
w.onChange()
Expand Down

0 comments on commit 33695cd

Please sign in to comment.