<commit_before>'use strict';

var log = require('npmlog');
var execa = require('execa');
var Bluebird = require('bluebird');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var spawnargs = require('spawn-args');
var extend = require('lodash.assignin');

var envWithLocalPath = require('./env-with-local-path');
var fileutils = require('./fileutils');
var Process = require('./utils/process');

var fileExists = fileutils.fileExists;
var executableExists = fileutils.executableExists;

function ProcessCtl(name, config, options) {
  options = options || {};

  this.name = name;
  this.config = config;
  this.killTimeout = options.killTimeout || 5000;
}

ProcessCtl.prototype.__proto__ = EventEmitter.prototype;

ProcessCtl.prototype.prepareOptions = function(options) {
  var defaults = {
    env: envWithLocalPath(this.config)
  };

  return extend({}, defaults, options);
};

ProcessCtl.prototype._spawn = function(exe, args, options) {
  log.info('spawning: ' + exe + ' - ' + util.inspect(args));
  var p  = new Process(this.name, { killTimeout: this.killTimeout }, execa(exe, args, options));
  this.emit('processStarted', p);
  return Bluebird.resolve(p);
};

ProcessCtl.prototype.spawn = function(exe, args, options) {
  var _options = this.prepareOptions(options);
  var self = this;

  if (Array.isArray(exe)) {
    return Bluebird.reduce(exe, function(found, exe) {
      if (found) {
        return found;
      }

      return self.exeExists(exe, _options).then(function(exists) {
        if (exists) {
          return exe;
        }
      });
    }, false).then(function(found) {
      if (!found) {
        throw new Error('No executable found in: ' + util.inspect(exe));
      }

      return self._spawn.call(self, found, args, _options);
    });
  }

  return this._spawn.call(this, exe, args, _options);
};

ProcessCtl.prototype.exec = function(cmd, options) {
  log.info('executing: ' + cmd);
  var cmdParts = spawnargs(cmd);
  var exe = cmdParts[0];
  var args = cmdParts.slice(1);

  options = options || {};
  options.shell = true; // exec uses a shell by default

  return this.spawn(exe, args, options);
};

ProcessCtl.prototype.exeExists = function(exe, options) {
  return fileExists(exe).then(function(exists) {
    if (exists) {
      return exists;
    }

    return executableExists(exe, options);
  });
};

module.exports = ProcessCtl;<commit_msg>Incremental conversion of codebase to es6 classes - processctl<commit_after>'use strict';

var log = require('npmlog');
var execa = require('execa');
var Bluebird = require('bluebird');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var spawnargs = require('spawn-args');
var extend = require('lodash.assignin');

var envWithLocalPath = require('./env-with-local-path');
var fileutils = require('./fileutils');
var Process = require('./utils/process');

var fileExists = fileutils.fileExists;
var executableExists = fileutils.executableExists;

class ProcessCtl extends EventEmitter {
  constructor(name, config, options) {
    super();

    options = options || {};

    this.name = name;
    this.config = config;
    this.killTimeout = options.killTimeout || 5000;
  }

  prepareOptions(options) {
    var defaults = {
      env: envWithLocalPath(this.config)
    };

    return extend({}, defaults, options);
  }

  _spawn(exe, args, options) {
    log.info('spawning: ' + exe + ' - ' + util.inspect(args));
    var p  = new Process(this.name, { killTimeout: this.killTimeout }, execa(exe, args, options));
    this.emit('processStarted', p);
    return Bluebird.resolve(p);
  }

  spawn(exe, args, options) {
    var _options = this.prepareOptions(options);
    var self = this;

    if (Array.isArray(exe)) {
      return Bluebird.reduce(exe, (found, exe) => {
        if (found) {
          return found;
        }

        return self.exeExists(exe, _options).then(exists => {
          if (exists) {
            return exe;
          }
        });
      }, false).then(found => {
        if (!found) {
          throw new Error('No executable found in: ' + util.inspect(exe));
        }

        return self._spawn.call(self, found, args, _options);
      });
    }

    return this._spawn.call(this, exe, args, _options);
  }

  exec(cmd, options) {
    log.info('executing: ' + cmd);
    var cmdParts = spawnargs(cmd);
    var exe = cmdParts[0];
    var args = cmdParts.slice(1);

    options = options || {};
    options.shell = true; // exec uses a shell by default

    return this.spawn(exe, args, options);
  }

  exeExists(exe, options) {
    return fileExists(exe).then(exists => {
      if (exists) {
        return exists;
      }

      return executableExists(exe, options);
    });
  }
}

module.exports = ProcessCtl;
{'fraction_non_alphanumeric': 0.1478460895023003, 'fraction_numerical': 0.0027185278126306985, 'mean_word_length': 3.28584229390681, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4366384', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1485, 'n_words': 347}