<commit_before># node.js child-process
# http://nodejs.org/docs/v0.6.3/api/child_processes.html

$ = require 'jquery'
_ = require 'underscore'

module.exports =
class ChildProccess
  @exec: (command, options={}) ->
    deferred = $.Deferred()

    if options.bufferLines
      options.stdout = @bufferLines(options.stdout) if options.stdout
      options.stderr = @bufferLines(options.stderr) if options.stderr

    $native.exec command, options, (exitStatus, stdout, stderr) ->
      if exitStatus != 0
        error = new Error("Exec failed (#{exitStatus}) command '#{command}'")
        error.exitStatus = exitStatus
        deferred.reject(error)
      else
        deferred.resolve(stdout, stderr)

    deferred

  @bufferLines: (callback) ->
    buffered = ""
    (data) ->
      buffered += data
      lastNewlineIndex = buffered.lastIndexOf('\n')
      if lastNewlineIndex >= 0
        callback(buffered.substring(0, lastNewlineIndex + 1))
        buffered = buffered.substring(lastNewlineIndex + 1)
<commit_msg>Non-zero exit codes reject promise with a detailed message<commit_after># node.js child-process
# http://nodejs.org/docs/v0.6.3/api/child_processes.html

$ = require 'jquery'
_ = require 'underscore'

module.exports =
class ChildProccess
  @exec: (command, options={}) ->
    deferred = $.Deferred()

    if options.bufferLines
      options.stdout = @bufferLines(options.stdout) if options.stdout
      options.stderr = @bufferLines(options.stderr) if options.stderr

    $native.exec command, options, (exitStatus, stdout, stderr) ->
      if exitStatus != 0
        deferred.reject("Command '#{command}' failed with exit status #{exitStatus} and stderr '#{stderr}'")
      else
        deferred.resolve(stdout, stderr)

    deferred

  @bufferLines: (callback) ->
    buffered = ""
    (data) ->
      buffered += data
      lastNewlineIndex = buffered.lastIndexOf('\n')
      if lastNewlineIndex >= 0
        callback(buffered.substring(0, lastNewlineIndex + 1))
        buffered = buffered.substring(lastNewlineIndex + 1)

{'fraction_non_alphanumeric': 0.1255495847581827, 'fraction_numerical': 0.007816316560820713, 'mean_word_length': 3.222680412371134, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '2740080', 'n_tokens_mistral': 631, 'n_tokens_neox': 602, 'n_words': 140}