Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ import (
"syscall"
"time"

"github.com/fsnotify/fsnotify"
"github.com/unknwon/com"
"github.com/unknwon/log"
"github.com/urfave/cli"
"gopkg.in/fsnotify/fsnotify.v1"

"github.com/gohugoio/hugo/watcher/filenotify"
"github.com/unknwon/bra/internal/setting"
)

Expand All @@ -53,10 +54,7 @@ var Run = cli.Command{

// isTmpFile returns true if the event was for temporary files.
func isTmpFile(name string) bool {
if strings.HasSuffix(strings.ToLower(name), ".tmp") {
return true
}
return false
return strings.HasSuffix(strings.ToLower(name), ".tmp")
}

// hasWatchExt returns true if the file name has watched extension.
Expand Down Expand Up @@ -114,6 +112,21 @@ func parseRunCommands(cmds [][]string) []*runCommand {
return runCmds
}

func getWatcher() (filenotify.FileWatcher, error) {
if setting.Cfg.Run.Poll {
interval := setting.Cfg.Run.PollInterval

if interval < 500 {
interval = 500
}

pollInterval := time.Duration(interval) * time.Millisecond
return filenotify.NewPollingWatcher(pollInterval), nil
}

return filenotify.NewEventWatcher()
}

func envFromFiles() []string {
envs := make([]string, 0)

Expand Down Expand Up @@ -251,18 +264,19 @@ func runRun(ctx *cli.Context) error {
watchPathes = append(watchPathes, subdirs...)
}

watcher, err := fsnotify.NewWatcher()
watcher, err := getWatcher()
if err != nil {
log.Fatal("Fail to create new watcher: %v", err)
}

defer watcher.Close()

go func() {
runCmds := parseRunCommands(setting.Cfg.Run.Cmds)

eventsChannel := watcher.Events()
for {
select {
case e := <-watcher.Events:
case e := <-eventsChannel:
needsNotify := true

if isTmpFile(e.Name) || !hasWatchExt(e.Name) || setting.IgnoreFile(e.Name) {
Expand Down Expand Up @@ -328,5 +342,4 @@ func runRun(ctx *cli.Context) error {
fmt.Printf("-> %s\n", p)
}
select {}
return nil
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ go 1.13

require (
github.com/BurntSushi/toml v0.3.1
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/fsnotify/fsnotify v1.6.0
github.com/gohugoio/hugo v0.111.3
github.com/unknwon/com v1.0.1
github.com/unknwon/log v0.0.0-20150304194804-e617c87089d3
github.com/urfave/cli v1.22.1
golang.org/x/sys v0.0.0-20191020152052-9984515f0562 // indirect
gopkg.in/fsnotify/fsnotify.v1 v1.4.7
)
Loading