1 |
|
2 | common = require './common'
|
3 | debug = require('debug')('fbp-spec:subprocess')
|
4 |
|
5 | exports.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 |
|
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 |
|
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
|