1
1
package runner
2
2
3
3
import (
4
+ "fmt"
4
5
"io"
5
6
"os"
6
7
"os/exec"
@@ -144,6 +145,9 @@ func (e *Engine) watching(root string) error {
144
145
func (e * Engine ) cacheFileChecksums (root string ) error {
145
146
return filepath .Walk (root , func (path string , info os.FileInfo , err error ) error {
146
147
if err != nil {
148
+ if info == nil {
149
+ return err
150
+ }
147
151
if info .IsDir () {
148
152
return filepath .SkipDir
149
153
}
@@ -199,7 +203,7 @@ func (e *Engine) cacheFileChecksums(root string) error {
199
203
200
204
func (e * Engine ) watchDir (path string ) error {
201
205
if err := e .watcher .Add (path ); err != nil {
202
- e .watcherLog ("failed to watching %s, error: %s" , path , err .Error ())
206
+ e .watcherLog ("failed to watch %s, error: %s" , path , err .Error ())
203
207
return err
204
208
}
205
209
e .watcherLog ("watching %s" , e .config .rel (path ))
@@ -314,19 +318,29 @@ func (e *Engine) start() {
314
318
continue
315
319
}
316
320
}
321
+
317
322
time .Sleep (e .config .buildDelay ())
318
323
e .flushEvents ()
324
+
325
+ // clean on rebuild https://stackoverflow.com/questions/22891644/how-can-i-clear-the-terminal-screen-in-go
326
+ if e .config .Screen .ClearOnRebuild {
327
+ fmt .Println ("\033 [2J" )
328
+ }
329
+
319
330
e .mainLog ("%s has changed" , e .config .rel (filename ))
320
331
case <- firstRunCh :
321
332
// go down
322
333
break
323
334
}
324
335
336
+ // already build and run now
325
337
select {
326
338
case <- e .buildRunCh :
327
339
e .buildRunStopCh <- true
328
340
default :
329
341
}
342
+
343
+ // if current app is running, stop it
330
344
e .withLock (func () {
331
345
if e .binRunning {
332
346
e .binStopCh <- true
@@ -382,16 +396,16 @@ func (e *Engine) flushEvents() {
382
396
func (e * Engine ) building () error {
383
397
var err error
384
398
e .buildLog ("building..." )
385
- cmd , stdout , stderr , err := e .startCmd (e .config .Build .Cmd )
399
+ cmd , stdin , stdout , stderr , err := e .startCmd (e .config .Build .Cmd )
386
400
if err != nil {
387
401
return err
388
402
}
389
403
defer func () {
390
404
stdout .Close ()
391
405
stderr .Close ()
406
+ stdin .Close ()
392
407
}()
393
- _ , _ = io .Copy (os .Stdout , stdout )
394
- _ , _ = io .Copy (os .Stderr , stderr )
408
+
395
409
// wait for building
396
410
err = cmd .Wait ()
397
411
if err != nil {
@@ -403,20 +417,15 @@ func (e *Engine) building() error {
403
417
func (e * Engine ) runBin () error {
404
418
var err error
405
419
e .runnerLog ("running..." )
406
- cmd , stdout , stderr , err := e .startCmd (e .config .Build .Bin )
420
+ cmd , stdin , stdout , stderr , err := e .startCmd (e .config .Build .Bin )
407
421
if err != nil {
408
422
return err
409
423
}
410
424
e .withLock (func () {
411
425
e .binRunning = true
412
426
})
413
427
414
- go func () {
415
- _ , _ = io .Copy (os .Stdout , stdout )
416
- _ , _ = io .Copy (os .Stderr , stderr )
417
- }()
418
-
419
- go func (cmd * exec.Cmd , stdout io.ReadCloser , stderr io.ReadCloser ) {
428
+ go func (cmd * exec.Cmd , stdin io.WriteCloser , stdout io.ReadCloser , stderr io.ReadCloser ) {
420
429
defer func () {
421
430
select {
422
431
case <- e .exitCh :
@@ -429,6 +438,7 @@ func (e *Engine) runBin() error {
429
438
defer func () {
430
439
stdout .Close ()
431
440
stderr .Close ()
441
+ stdin .Close ()
432
442
}()
433
443
pid , err := e .killCmd (cmd )
434
444
if err != nil {
@@ -449,7 +459,7 @@ func (e *Engine) runBin() error {
449
459
if err = os .Remove (cmdBinPath ); err != nil {
450
460
e .mainLog ("failed to remove %s, error: %s" , e .config .rel (e .config .binPath ()), err )
451
461
}
452
- }(cmd , stdout , stderr )
462
+ }(cmd , stdin , stdout , stderr )
453
463
return nil
454
464
}
455
465
0 commit comments