Permalink
Browse files

Rerun commands if changes happen during command run.

  • Loading branch information...
1 parent 1535830 commit 3e73aeaba47ab36cb6c2134b4929aebb45de091a @epeli committed Jun 16, 2011
Showing with 22 additions and 4 deletions.
  1. +22 −4 lib/main.coffee
View
26 lib/main.coffee
@@ -18,6 +18,9 @@ class Watcher
if @settings.watchdir.substr(0,1) isnt "/"
@settings.watchdir = path.join(@cwd, @settings.watchdir)
+ @rerun = false
+ @running = false
+
start: ->
watch.createMonitor @settings.watchdir, (monitor) =>
@@ -35,17 +38,32 @@ class Watcher
break
return unless ok
- console.log "#{ filepath } changed on #{ @name }"
- @runCMD()
+ # Oh we are already running. Just request restart for this change.
+ if @running
+ @rerun = true
+ else
+ @runCMD()
+
+
+ console.log "Change on '#{ filepath }' running '#{ @name }'!"
runCMD: ->
+ @running = true
cmd = exec @settings.cmd, cwd: @cwd, (err) =>
+ @running = false
+
if err
- console.log "Error in #{ @name }"
+ console.log "Error in '#{ @name }'"
+
+ if @rerun
+ # There has been a change(s) during this run. Let's rerun it.
+ console.log "Rerunning '#{ @name }'"
+ @rerun = false
+ @runCMD()
else
- console.log "\nRan", @name, "successfully!"
+ console.log "\nRan '#{ @name }' successfully!"
cmd.stdout.on "data", (data) -> process.stdout.write data
cmd.stderr.on "data", (data) -> process.stderr.write data

0 comments on commit 3e73aea

Please sign in to comment.