diff --git a/pkg/watcher/.gitignore b/pkg/watcher/.gitignore new file mode 100644 index 00000000..95811e00 --- /dev/null +++ b/pkg/watcher/.gitignore @@ -0,0 +1 @@ +/main diff --git a/pkg/server/watcher/main.go b/pkg/watcher/main.go similarity index 66% rename from pkg/server/watcher/main.go rename to pkg/watcher/main.go index 1f45df6c..c7fc5e9a 100644 --- a/pkg/server/watcher/main.go +++ b/pkg/watcher/main.go @@ -19,12 +19,15 @@ package main import ( + "flag" "log" "os" "os/exec" + "strings" "syscall" "time" + "github.com/pkg/errors" "github.com/radovskyb/watcher" ) @@ -47,8 +50,25 @@ func command(binary string, args []string, entryPoint string) *exec.Cmd { return cmd } -func execCmd() *exec.Cmd { - return command("go", []string{"run", "main.go", "start", "-port", "3000"}, "..") +func execCmd(task string, watchDir string) *exec.Cmd { + parts := strings.Fields(task) + + return command(parts[0], parts[1:], watchDir) +} + +var task, context, ignore string + +func init() { + flag.StringVar(&task, "task", "", "the command to execute") + flag.StringVar(&context, "context", ".", "the file or directory from which to execute the task") + flag.StringVar(&ignore, "ignore", ".", "the file or directory to ignore") + + flag.Parse() + + if task == "" { + log.Println("task was not provided. Exiting the watcher...") + os.Exit(1) + } } func main() { @@ -56,6 +76,8 @@ func main() { w.IgnoreHiddenFiles(true) w.SetMaxEvents(1) + targets := flag.Args() + var e *exec.Cmd go func() { @@ -74,7 +96,7 @@ func main() { } // Starting it again here or starting for the first time. - e = execCmd() + e = execCmd(task, context) case err := <-w.Error: log.Fatalln(err) case <-w.Closed: @@ -83,14 +105,22 @@ func main() { } }() - if err := w.AddRecursive(".."); err != nil { - log.Fatalln(err) + if ignore != "" { + if err := w.Ignore(ignore); err != nil { + log.Fatalln(errors.Wrapf(err, "ignoring %s", ignore)) + } } - e = execCmd() + for _, target := range targets { + if err := w.AddRecursive(target); err != nil { + log.Fatalln(errors.Wrap(err, "watching the given pattern")) + } + } + + e = execCmd(task, context) log.Printf("watching %d files", len(w.WatchedFiles())) if err := w.Start(time.Millisecond * 1000); err != nil { - log.Fatalln(err) + log.Fatalln(errors.Wrap(err, "starting watcher")) } } diff --git a/scripts/web/dev.sh b/scripts/web/dev.sh index efaddabb..cbe972fa 100755 --- a/scripts/web/dev.sh +++ b/scripts/web/dev.sh @@ -37,4 +37,4 @@ set +a devServerPID=$! # run server -(cd "$serverPath/watcher" && go run main.go) +(cd "$basePath/pkg/watcher" && go run main.go --task="go run main.go start -port 3000" --context="$serverPath" "$serverPath")