UNPKG

1.3 kBtext/coffeescriptView Raw
1
2common = require './common'
3debug = require('debug')('fbp-spec:subprocess')
4
5exports.start = (command, options, callback) ->
6 try
7 child_process = require 'child_process'
8 catch err
9 return callback err
10
11 options.timeout = 300 if not options.timeout?
12
13 started = false
14 stderr = ""
15 stdout = ""
16
17 # FIXME: using sh to interpret command will Unix-specific
18 prog = 'sh'
19 args = [ '-c', command ]
20 child = child_process.spawn prog, args
21
22 debug 'spawned', "'#{prog} #{args.join(' ')}'"
23 debug 'waiting for output'
24
25 child.on 'error', (err) ->
26 return callback err
27
28 child.stdout.on 'data', (data) ->
29 data = data.toString()
30 stdout += data
31 debug 'sub stdout', data
32 if not started
33 debug 'got output, transitioning to started'
34 started = true
35 # give process some time to open port
36 setTimeout callback, 100
37
38 child.stderr.on 'data', (data) ->
39 data = data.toString()
40 stderr += data
41 debug 'sub stderr', data
42 if not started
43 debug 'got stderr, failing'
44 started = true
45 return callback new Error "Subprocess wrote on stderr: '#{stderr}'"
46
47 setTimeout () ->
48 if not started
49 debug 'timeout waiting for output, assuming started'
50 started = true
51 return callback null
52 , options.timeout
53
54 return child