sync-exec.coffee (1512B)
1 2 child_process = require 'child_process' 3 4 create_pipes = require './lib/create-pipes' 5 proxy = require './lib/proxy' 6 read_pipes = require './lib/read-pipes' 7 timeout = require './lib/timeout' 8 9 10 # Blocking exec 11 # 12 # @param cmd String command to execute 13 # @param max_wait Number millisecond timeout value 14 # @param options Object execution options (like: encoding) 15 # 16 # @return Object {String stderr, String stdout, Number status} 17 18 module.exports = (cmd, max_wait, options) -> 19 20 if max_wait and typeof max_wait is 'object' 21 [options, max_wait] = [max_wait, null] 22 23 options ?= {} 24 25 unless options.hasOwnProperty 'encoding' 26 options.encoding = 'utf8' 27 28 unless typeof options is 'object' and options 29 throw new Error 'options must be an object' 30 31 max_wait ?= options.timeout or options.max_wait or 3600000 # 1hr default 32 unless not max_wait? or max_wait >= 1 33 throw new Error '`options.timeout` must be >=1 millisecond' 34 delete options.max_wait 35 36 # use native child_process.execSync if available (from node v0.12+) 37 if options.forceEmulation 38 delete options.forceEmulation 39 else if child_process.execSync 40 return proxy cmd, max_wait, options 41 42 delete options.timeout 43 44 dir = create_pipes() 45 cmd = '((((' + cmd + ' > ' + dir + '/stdout 2> ' + dir + '/stderr ) ' + 46 '&& echo $? > ' + dir + '/status) || echo $? > ' + dir + '/status) &&' + 47 ' echo 1 > ' + dir + '/done) || echo 1 > ' + dir + '/done' 48 child_process.exec cmd, options, -> 49 50 read_pipes dir, max_wait