Use child_process.spawn from Project.scan

This commit is contained in:
Kevin Sawicki 2013-03-07 17:36:21 -08:00
parent eddfb8a1bb
commit c22d14c5b0
3 changed files with 24 additions and 14 deletions

View File

@ -1,6 +1,7 @@
Project = require 'project'
fs = require 'fs'
_ = require 'underscore'
BufferedProcess = require 'buffered-process'
describe "Project", ->
beforeEach ->
@ -239,12 +240,12 @@ describe "Project", ->
expect(fs.base(paths[4])).toBe "utfa\u0306.md"
it "handles breaks in the search subprocess's output following the filename", ->
spyOn $native, 'exec'
spyOn(BufferedProcess.prototype, 'bufferStream')
iterator = jasmine.createSpy('iterator')
project.scan /a+/, iterator
stdout = $native.exec.argsForCall[0][1].stdout
stdout = BufferedProcess.prototype.bufferStream.argsForCall[0][1]
stdout ":#{require.resolve('fixtures/dir/a')}\n"
stdout "1;0 3:aaa bbb\n2;3 2:cc aa cc\n"

View File

@ -6,7 +6,7 @@ Buffer = require 'buffer'
EditSession = require 'edit-session'
EventEmitter = require 'event-emitter'
Directory = require 'directory'
ChildProcess = require 'child-process'
BufferedProcess = require 'buffered-process'
module.exports =
class Project
@ -159,9 +159,7 @@ class Project
_.remove(@buffers, buffer)
scan: (regex, iterator) ->
command = "#{require.resolve('ag')} --ackmate '#{regex.source}' '#{@getPath()}'"
bufferedData = ""
state = 'readingPath'
path = null
@ -193,11 +191,22 @@ class Project
match = lineText.substr(column, length)
iterator({path, range, match})
ChildProcess.exec command , bufferLines: true, stdout: (data) ->
deferred = $.Deferred()
exit = (code) ->
if code is -1
deferred.reject({command, code})
else
deferred.resolve()
stdout = (data) ->
lines = data.split('\n')
lines.pop() # the last segment is a spurious '' because data always ends in \n due to bufferLines: true
for line in lines
readPath(line) if state is 'readingPath'
readLine(line) if state is 'readingLines'
command = require.resolve('ag')
args = ['--ackmate', regex.source, @getPath()]
new BufferedProcess({command, args, stdout, exit})
deferred
_.extend Project.prototype, EventEmitter

View File

@ -2,8 +2,8 @@ ChildProcess = nodeRequire 'child_process'
module.exports =
class BufferedProcess
constructor: (options={}) ->
process = ChildProcess.spawn(options.command, options.args)
constructor: ({command, args, options, stdout, stderr, exit}={}) ->
process = ChildProcess.spawn(command, args, options)
stdoutClosed = true
stderrClosed = true
@ -11,21 +11,21 @@ class BufferedProcess
exitCode = 0
triggerExitCallback = ->
if stdoutClosed and stderrClosed and processExited
options.exit?(exitCode)
exit?(exitCode)
if options.stdout
if stdout
stdoutClosed = false
@bufferStream process.stdout, options.stdout, ->
@bufferStream process.stdout, stdout, ->
stdoutClosed = true
triggerExitCallback()
if options.stderr
if stderr
stderrClosed = false
@bufferStream process.stderr, options.stderr, ->
@bufferStream process.stderr, stderr, ->
stderrClosed = true
triggerExitCallback()
if options.exit
if exit
processExited = false
process.on 'exit', (code) ->
exitCode = code