-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole_command_runner.coffee
38 lines (30 loc) · 1.06 KB
/
console_command_runner.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
util = require('util')
exec = require('child_process').exec
emitter = require('events').EventEmitter
class ConsoleCommandRunner extends emitter
constructor:(@commands, @working_dir) ->
@current = 0
print_commands: ->
for command in @commands
console.log(command)
run: ->
@_runCommand(@commands[@current])
_runCommand:(command) ->
exec(command, {cwd: @working_dir}, (error, stdout, stderr) =>
@_print_command_error(command,error,stdout,stderr) unless error is null
if @_last_command()
@emit('command_runner_finished')
else
@_runCommand(@_get_next_command())
)
_print_command_error:(command,error, stdout, stderr) ->
console.log("Error from command: #{command}")
util.print('stdout: ' + stdout)
util.print('stderr: ' + stderr)
console.log('exec error: ' + error)
_last_command: ->
true if @current is @commands.length-1
_get_next_command: ->
@current += 1
@commands[@current] unless @current is @commands.length
exports.command_runner = ConsoleCommandRunner