'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _child_process = require('child_process');

var _child_process2 = _interopRequireDefault(_child_process);

var _fsExtra = require('fs-extra');

var _fsExtra2 = _interopRequireDefault(_fsExtra);

var _ = require('../');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function prepend(value, array) {
  var newArray = array.slice(0);
  newArray.unshift(value);
  return newArray;
}

var abeProcess = function abeProcess(name) {
  var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];

  args = prepend('ABE_WEBSITE=' + _.config.root, args);
  args = prepend('ABEJS_PATH=' + __dirname + '/../../../dist', args);

  if (!_.abeExtend.lock.create(name)) {
    return false;
  }

  var proc;
  var file = __dirname + '/../../cli/process/' + name + '.js';
  try {
    var stats = _fsExtra2.default.statSync(file);
    if (stats.isFile()) {
      proc = _child_process2.default.fork(file, args);
    }
  } catch (err) {
    try {
      file = _.abeExtend.plugins.instance.getProcess(name);
      stats = _fsExtra2.default.statSync(file);
      if (stats.isFile()) {
        proc = _child_process2.default.fork(file, args);
      }
    } catch (err) {
      console.log('process fork failed');
    }
  }

  if (typeof proc !== 'undefined' && proc !== null) {
    proc.on('message', function () {
      _.abeExtend.lock.remove(name);
      proc.kill();
    });
    return true;
  }

  return false;
};

exports.default = abeProcess;