Skip to content

Commit

Permalink
Stop watching build dir.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanieluz committed May 6, 2020
1 parent e703c38 commit 0d91e7e
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,14 @@ func Watch(buildPath string) {
defer watcher.Close()

// Starting at the root of the project, find subdirectories.
if err := filepath.Walk(".", watchDir(buildPath)); err != nil {
fmt.Println("ERROR", err)
if err := filepath.Walk("content", watchDir(buildPath)); err != nil {
fmt.Println("Watching 'content/' folder for changes error: ", err)
}
if err := filepath.Walk("layout", watchDir(buildPath)); err != nil {
fmt.Println("Watching 'layout/' folder for changes error: ", err)
}
watcher.Add("plenti.json")
watcher.Add("package.json")

done := make(chan bool)

Expand All @@ -133,10 +138,15 @@ func Watch(buildPath string) {
events = append(events, event)
}
case <-ticker.C:
buildDirChanged := false
// Checks on set interval if there are events.
if len(events) > 0 {
// Display messages for each events in batch.
for _, event := range events {
// If Mac picks up on "public" build dir changing, don't rebuild.
if event.Name == buildPath {
buildDirChanged = true
}
if event.Op&fsnotify.Write == fsnotify.Write {
fmt.Printf("\nFile write detected: %#v\n", event)
}
Expand All @@ -147,10 +157,13 @@ func Watch(buildPath string) {
fmt.Printf("\nFile rename detected: %#v\n", event)
}
}
// Rebuild only one time for all batched events.
Build()
// Empty the batch array.
events = make([]fsnotify.Event, 0)
if !buildDirChanged {
// Rebuild only one time for all batched events.
Build()
// Empty the batch array.
events = make([]fsnotify.Event, 0)
}

}

// Watch for errors.
Expand All @@ -170,7 +183,7 @@ func watchDir(buildPath string) filepath.WalkFunc {
// Callback for walk func: searches for directories to add watchers to.
return func(path string, fi os.FileInfo, err error) error {
// Skip the "public" build dir to avoid infinite loops.
if fi.IsDir() && fi.Name() == buildPath {
if fi.IsDir() && fi.Name() == buildPath || fi.Name() == "node_modules" {
return filepath.SkipDir
}
// Add watchers only to nested directory.
Expand Down

0 comments on commit 0d91e7e

Please sign in to comment.