'use strict';

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

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

var _execa;

function _load_execa() {
  return _execa = _interopRequireDefault(require('execa'));
}

var _generalError;

function _load_generalError() {
  return _generalError = _interopRequireDefault(require('../error/general-error'));
}

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

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

/**
 * get diff between files using git diff command
 */
exports.default = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(fileA, fileB) {
    var colors = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
    var params, result;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.prev = 0;
            params = ['diff'];

            params.push('--no-index'); // ignores the working tree (in case the project is managed by git)
            if (colors) params.push('--color');
            params.push(fileA);
            params.push(fileB);

            _context.next = 8;
            return (0, (_execa || _load_execa()).default)('git', params);

          case 8:
            result = _context.sent;
            return _context.abrupt('return', result.stdout);

          case 12:
            _context.prev = 12;
            _context.t0 = _context['catch'](0);

            if (!(_context.t0.code && Number.isInteger(_context.t0.code) && _context.t0.stdout)) {
              _context.next = 16;
              break;
            }

            return _context.abrupt('return', _context.t0.stdout);

          case 16:
            if (!(_context.t0.code === 'ENOENT')) {
              _context.next = 18;
              break;
            }

            throw new (_generalError || _load_generalError()).default('unable to run git diff command, please make sure you have git installed');

          case 18:
            throw _context.t0;

          case 19:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this, [[0, 12]]);
  }));

  function diffFiles(_x2, _x3) {
    return _ref.apply(this, arguments);
  }

  return diffFiles;
}();