Skip to content

Instantly share code, notes, and snippets.

@adborden
Created June 26, 2015 17:35
Show Gist options
  • Save adborden/a4172167ec50b10c4c70 to your computer and use it in GitHub Desktop.
Save adborden/a4172167ec50b10c4c70 to your computer and use it in GitHub Desktop.
fibrous = require 'fibrous'
fs = require 'fs'
stream = require 'stream'
{spawn} = require 'child_process'
# the process
stdout = new stream.PassThrough()
# our code
build = {
log: []
logText: ->
@log.join '\n'
appendLog: (data, cb) ->
@log.push data.toString()
setTimeout ->
console.log data.toString()
cb()
, 100 + Math.floor(Math.random() * 100) # some jitter
}
fibrous.run ->
fs.writeFileSync './demo.sh', '''
#!/bin/sh
echo ARG1=$1
echo ARG2=$2
(sleep 3 ; echo awake) &
echo hello world
exit 0
''', mode: 0o0755
child = spawn './demo.sh', ['arg1', 'arg2']
child.stdout.highWaterMark = 64
child.stdout.on 'data', (data) ->
console.log {start: data.toString()}
build.appendLog data, ->
child.stdout.on 'end', ->
console.log 'end'
child.stdout.on 'close', ->
console.log 'close'
((cb) ->
child.on 'exit', (code, signal) ->
console.log {child: 'exit', code, signal}
child.on 'close', (code, signal) ->
console.log {child: 'close', code, signal}
cb()
).sync()
console.log 'done waiting'
# $ coffee demo.coffee
# { child: 'exit', code: 0, signal: null }
# { start: 'ARG1=arg1\nARG2=arg2\nhello world\n' }
# ARG1=arg1
# ARG2=arg2
# hello world
#
# { start: 'awake\n' }
# end
# { child: 'close', code: 0, signal: null }
# done waiting
# close
# awake
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment