<commit_before>ChildProcess = require 'child_process'

module.exports =
class BufferedProcess
  process: null
  killed: false

  constructor: ({command, args, options, stdout, stderr, exit}={}) ->
    @process = ChildProcess.spawn(command, args, options)

    stdoutClosed = true
    stderrClosed = true
    processExited = true
    exitCode = 0
    triggerExitCallback = ->
      return if @killed
      if stdoutClosed and stderrClosed and processExited
        exit?(exitCode)

    if stdout
      stdoutClosed = false
      @bufferStream @process.stdout, stdout, ->
        stdoutClosed = true
        triggerExitCallback()

    if stderr
      stderrClosed = false
      @bufferStream @process.stderr, stderr, ->
        stderrClosed = true
        triggerExitCallback()

    if exit
      processExited = false
      @process.on 'exit', (code) ->
        exitCode = code
        processExited = true
        triggerExitCallback()

  bufferStream: (stream, onLines, onDone) ->
    stream.setEncoding('utf8')
    buffered = ''

    stream.on 'data', (data) =>
      return if @killed
      buffered += data
      lastNewlineIndex = buffered.lastIndexOf('\n')
      if lastNewlineIndex isnt -1<commit_msg>Put node directory in PATH when spawning<commit_after>ChildProcess = require 'child_process'
path = require 'path'
_ = require 'underscore'

module.exports =
class BufferedProcess
  process: null
  killed: false

  constructor: ({command, args, options, stdout, stderr, exit}={}) ->
    options ?= {}
    @addNodeDirectoryToPath(options)
    @process = ChildProcess.spawn(command, args, options)

    stdoutClosed = true
    stderrClosed = true
    processExited = true
    exitCode = 0
    triggerExitCallback = ->
      return if @killed
      if stdoutClosed and stderrClosed and processExited
        exit?(exitCode)

    if stdout
      stdoutClosed = false
      @bufferStream @process.stdout, stdout, ->
        stdoutClosed = true
        triggerExitCallback()

    if stderr
      stderrClosed = false
      @bufferStream @process.stderr, stderr, ->
        stderrClosed = true
        triggerExitCallback()

    if exit
      processExited = false
      @process.on 'exit', (code) ->
        exitCode = code
        processExited = true
        triggerExitCallback()

  addNodeDirectoryToPath: (options) ->
    options.env ?= process.env
    pathSegments = []
    nodeDirectoryPath = path.resolve(process.execPath, '..', '..', '..', '..', 'Resources')
    pathSegments.push(nodeDirectoryPath)
    pathSegments.push(options.env.PATH) if options.env.PATH
    options.env = _.extend({}, options.env, PATH: pathSegments.join(path.delimiter))

  bufferStream: (stream, onLines, onDone) ->
    stream.setEncoding('utf8')
    buffered = ''

    stream.on 'data', (data) =>
      return if @killed
      buffered += data
      lastNewlineIndex = buffered.lastIndexOf('\n')
      if lastNewlineIndex isnt -1
{'fraction_non_alphanumeric': 0.10425240054869685, 'fraction_numerical': 0.00205761316872428, 'mean_word_length': 2.783398184176394, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3499534', 'n_tokens_mistral': 880, 'n_tokens_neox': 829, 'n_words': 213}