Create helper to buffer process lines

This commit is contained in:
Kevin Sawicki
2013-03-07 16:58:09 -08:00
parent f3c2b41f4f
commit 01faf6ea1e
2 changed files with 53 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
Point = require 'point'
ChildProcess = nodeRequire 'child_process'
$ = require 'jquery'
BufferedProcess = require 'buffered-process'
module.exports =
class TagGenerator
@@ -15,18 +16,15 @@ class TagGenerator
null
generate: ->
deferred = $.Deferred()
tags = []
command = "#{require.resolve('ctags')}"
args = ['--fields=+KS', '-nf', '-', @path]
ctags = ChildProcess.spawn(command, args)
deferred = $.Deferred()
output = ''
ctags.stdout.setEncoding('utf8')
ctags.stdout.on 'data', (data) ->
output += data
ctags.stdout.on 'close', =>
tags = []
for line in output.split('\n')
stdout = (lines) =>
for line in lines.split('\n')
tag = @parseTagLine(line)
tags.push(tag) if tag
exit = ->
deferred.resolve(tags)
new BufferedProcess({command, args, stdout, exit})
deferred

View File

@@ -0,0 +1,46 @@
ChildProcess = nodeRequire 'child_process'
module.exports =
class BufferedProcess
constructor: (options={}) ->
process = ChildProcess.spawn(options.command, options.args)
stdoutClosed = true
stderrClosed = true
processExited = true
triggerExitCallback = ->
if stdoutClosed and stderrClosed and processExited
options.exit?()
if options.stdout
stdoutClosed = false
@bufferStream process.stdout, options.stdout, ->
stdoutClosed = true
triggerExitCallback()
if options.stderr
stderrClosed = false
@bufferStream process.stderr, options.stderr, ->
stderrClosed = true
triggerExitCallback()
if options.exit
processExited = false
process.on 'exit', ->
processExited = true
triggerExitCallback()
bufferStream: (stream, onLines, onDone) ->
stream.setEncoding('utf8')
buffered = ''
stream.on 'data', (data) ->
buffered += data
lastNewlineIndex = buffered.lastIndexOf('\n')
if lastNewlineIndex isnt -1
onLines(buffered.substring(0, lastNewlineIndex + 1))
buffered = buffered.substring(lastNewlineIndex + 1)
stream.on 'close', =>
onLines(buffered) if buffered.length > 0
onDone()