@@ -21,7 +21,9 @@ import (
2121type RunWatchOptions struct {
2222 FailOnError bool `long:"fail-on-error" description:"If true the command will fail on an error while running the sub command"`
2323
24- Paths []string `long:"path" short:"p" description:"The paths to watch. Can be patterns in the form of ./**/my-file.txt"`
24+ SkipInitial bool `long:"skip-initial" description:"If true will not execute the command immediately."`
25+ Silent bool `long:"silent" description:"If true will not print any warning about restarting the command."`
26+ Paths []string `long:"path" short:"p" description:"The paths to watch. Can be patterns in the form of ./**/my-file.txt"`
2527}
2628
2729func RunWatch (ctx context.Context , args []string , handler types2.ExecHandler , log log.Logger ) error {
@@ -37,23 +39,29 @@ func RunWatch(ctx context.Context, args []string, handler types2.ExecHandler, lo
3739 return fmt .Errorf ("usage: run_watch --path MY_PATH -- my_command" )
3840 }
3941
40- w := & watcher {}
41- return w .Watch (ctx , options .Paths , options .FailOnError , func (ctx context.Context ) error {
42+ w := & watcher {
43+ options : * options ,
44+ }
45+ return w .Watch (ctx , func (ctx context.Context ) error {
4246 return handler .ExecHandler (ctx , args )
4347 }, log )
4448}
4549
46- type watcher struct {}
50+ type watcher struct {
51+ options RunWatchOptions
52+ }
53+
54+ func (w * watcher ) Watch (ctx context.Context , action func (ctx context.Context ) error , log log.Logger ) error {
55+ patterns := w .options .Paths
4756
48- func (w * watcher ) Watch (ctx context.Context , patterns []string , failOnError bool , action func (ctx context.Context ) error , log log.Logger ) error {
4957 // prepare patterns
5058 for i , p := range patterns {
5159 patterns [i ] = strings .TrimSuffix (strings .TrimPrefix (strings .TrimSpace (p ), "./" ), "/" )
5260 }
5361
5462 // get folders from patterns
5563 pathsToWatch := map [string ]bool {}
56- for _ , p := range patterns {
64+ for i , p := range patterns {
5765 patternsSplitted := strings .Split (filepath .ToSlash (p ), "/" )
5866 lastIndex := len (patternsSplitted ) - 1
5967 for i , s := range patternsSplitted {
@@ -66,6 +74,8 @@ func (w *watcher) Watch(ctx context.Context, patterns []string, failOnError bool
6674 targetPath := strings .Join (patternsSplitted [:lastIndex ], "/" )
6775 if targetPath == "" {
6876 targetPath = "."
77+ } else {
78+ patterns [i ] = strings .TrimPrefix (patterns [i ], targetPath + "/" )
6979 }
7080
7181 absolutePath , err := filepath .Abs (filepath .FromSlash (targetPath ))
@@ -131,12 +141,18 @@ func (w *watcher) Watch(ctx context.Context, patterns []string, failOnError bool
131141 }
132142
133143 // start command
134- return w .handleCommand (ctx , patterns , failOnError , action , globalChannel , log )
144+ return w .handleCommand (ctx , patterns , action , globalChannel )
135145}
136146
137- func (w * watcher ) handleCommand (ctx context.Context , patterns []string , failOnError bool , action func (ctx context.Context ) error , events chan string , log log. Logger ) error {
147+ func (w * watcher ) handleCommand (ctx context.Context , patterns []string , action func (ctx context.Context ) error , events chan string ) error {
138148 hc := interp .HandlerCtx (ctx )
139- t := w .startCommand (ctx , action )
149+ var t * tomb.Tomb
150+ if ! w .options .SkipInitial {
151+ t = w .startCommand (ctx , action )
152+ } else {
153+ t = & tomb.Tomb {}
154+ t .Go (func () error { return nil })
155+ }
140156 numEvents := 0
141157 lastChange := ""
142158 for {
@@ -158,7 +174,9 @@ func (w *watcher) handleCommand(ctx context.Context, patterns []string, failOnEr
158174 case <- time .After (time .Second * 2 ):
159175 if numEvents > 0 {
160176 // kill application and wait for exit
161- _ , _ = hc .Stderr .Write ([]byte (fmt .Sprintf ("\n %s Restarting command because '%s' has changed...\n \n " , ansi .Color ("warn" , "red+b" ), lastChange )))
177+ if ! w .options .Silent {
178+ _ , _ = hc .Stderr .Write ([]byte (fmt .Sprintf ("\n %s Restarting command because '%s' has changed...\n \n " , ansi .Color ("warn" , "red+b" ), lastChange )))
179+ }
162180 t .Kill (nil )
163181 select {
164182 case <- ctx .Done ():
@@ -174,7 +192,7 @@ func (w *watcher) handleCommand(ctx context.Context, patterns []string, failOnEr
174192 }
175193
176194 // check if terminated
177- if failOnError && t .Terminated () && t .Err () != nil {
195+ if w . options . FailOnError && t .Terminated () && t .Err () != nil {
178196 return t .Err ()
179197 }
180198 }
0 commit comments