<commit_before>log   = require('./logger')
spawn = require('child_process').spawn

module.exports = ->
  @Processes = {}

  class @Process
    constructor: (file, message, options = {}) ->
      @file = file
      @message = message
      @options = options

    start: (args...) ->
      unless @_started
        @_started = $.defer()
        @_process = spawn('node', [@file].concat(args))
        @_process.stdout.on 'data', @_stdout
        @_process.stderr.on 'data', @_stderr

        @_process.on 'exit', @_exit
        @_process.on 'error', @_error
        @_process.on 'close', @_close
        @_process.on 'disconnect', @_disconnect
        @_process.on 'message', @_message

        process.on 'exit', @stop
        process.on 'error', @stop

      @_started.promise

    stop: =>
      @_process.kill() and (@_process = null) if @_process
      $.fulfilled(!@_process)

    _stdout: (data) =>
      @_logBuffer(log.info, "STDOUT", data)

      if @_started.promise.isPending()
        @_started.fulfill() if ~data.toString().indexOf(@message)

    _stderr: (data) =><commit_msg>[breaking] Allow non-node Processes.<commit_after>log   = require('./logger')
spawn = require('child_process').spawn

module.exports = ->
  @Processes = {}

  class @Process
    constructor: (file, message, options = {}) ->
      @file = file
      @message = message
      @options = options

    start: (args...) ->
      unless @_started
        @_started = $.defer()
        @_process = spawn(@file, args, @options)
        @_process.stdout.on 'data', @_stdout
        @_process.stderr.on 'data', @_stderr

        @_process.on 'exit', @_exit
        @_process.on 'error', @_error
        @_process.on 'close', @_close
        @_process.on 'disconnect', @_disconnect
        @_process.on 'message', @_message

        process.on 'exit', @stop
        process.on 'error', @stop

      @_started.promise

    stop: =>
      @_process.kill() and (@_process = null) if @_process
      $.fulfilled(!@_process)

    _stdout: (data) =>
      @_logBuffer(log.info, "STDOUT", data)

      if @_started.promise.isPending()
        @_started.fulfill() if ~data.toString().indexOf(@message)

    _stderr: (data) =>
{'fraction_non_alphanumeric': 0.1616575591985428, 'fraction_numerical': 0.0, 'mean_word_length': 2.498407643312102, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '4410165', 'n_tokens_mistral': 724, 'n_tokens_neox': 695, 'n_words': 154}