From c22d14c5b09858b989f11742ef9aa5dbf127c837 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 7 Mar 2013 17:36:21 -0800 Subject: [PATCH] Use child_process.spawn from Project.scan --- spec/app/project-spec.coffee | 5 +++-- src/app/project.coffee | 17 +++++++++++++---- src/stdlib/buffered-process.coffee | 16 ++++++++-------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/spec/app/project-spec.coffee b/spec/app/project-spec.coffee index 136e61a85..4cc2fda5c 100644 --- a/spec/app/project-spec.coffee +++ b/spec/app/project-spec.coffee @@ -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" diff --git a/src/app/project.coffee b/src/app/project.coffee index fac99ce8d..1bba35f43 100644 --- a/src/app/project.coffee +++ b/src/app/project.coffee @@ -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 diff --git a/src/stdlib/buffered-process.coffee b/src/stdlib/buffered-process.coffee index 1de85b5e9..1781662f0 100644 --- a/src/stdlib/buffered-process.coffee +++ b/src/stdlib/buffered-process.coffee @@ -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