const spawn = require('child_process').spawn;

const exec = function (command, option, callback) {
  var command_array = command.split(' ');
  var action = command_array.shift();
  var command_exec = spawn(action, command_array, option);
  var resulf_log = [];
  var error_log = [];

  command_exec.stdout.on('data', function (data) {
    data = data.toString();
    resulf_log.push(data);
  });

  command_exec.on('close', function (code) {
    code = code.toString();
    resulf_log = resulf_log.join('\n');
    if (error_log.length > 0) {
      error_log = error_log.join('\n');
      callback(error_log, resulf_log);
    } else {
      callback(null, resulf_log);
    }
  });
};

module.exports = exec;