'use strict';

var _regenerator;

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

var runAndUpdateCI = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(_ref) {
    var id = _ref.id,
        scopePath = _ref.scopePath,
        verbose = _ref.verbose,
        directory = _ref.directory,
        keep = _ref.keep;
    var addCIAttrsInTheModel, startTime, save, buildResults, testResults, dists;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            addCIAttrsInTheModel = function addCIAttrsInTheModel(_ref3) {
              var error = _ref3.error,
                  startTime = _ref3.startTime;

              var endTime = Date.now().toString();
              var ciProps = { startTime, endTime, error: undefined };

              if (error) {
                var serializedError = (0, (_serializeError || _load_serializeError()).default)(error);
                ciProps.error = serializedError;
                return (0, (_scope || _load_scope()).modifyCIProps)(scopePath, id, ciProps);
              }

              return (0, (_scope || _load_scope()).modifyCIProps)(scopePath, id, ciProps);
            };

            startTime = Date.now().toString();
            _context.prev = 2;

            // define options
            save = true;
            _context.next = 6;
            return (0, (_scope || _load_scope()).buildInScope)({ id, scopePath, save, verbose, directory, keep });

          case 6:
            buildResults = _context.sent;
            _context.next = 9;
            return (0, (_scope || _load_scope()).testInScope)({ id, scopePath, save, verbose, directory, keep });

          case 9:
            testResults = _context.sent;
            dists = buildResults ? buildResults.dists : null;
            _context.next = 13;
            return addCIAttrsInTheModel({ startTime });

          case 13:
            return _context.abrupt('return', { specsResults: testResults, dists });

          case 16:
            _context.prev = 16;
            _context.t0 = _context['catch'](2);
            _context.next = 20;
            return addCIAttrsInTheModel({ error: _context.t0, startTime });

          case 20:
            throw _context.t0;

          case 21:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this, [[2, 16]]);
  }));

  return function runAndUpdateCI(_x) {
    return _ref2.apply(this, arguments);
  };
}();

var _serializeError;

function _load_serializeError() {
  return _serializeError = _interopRequireDefault(require('serialize-error'));
}

var _scope;

function _load_scope() {
  return _scope = require('../../api/scope');
}

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"); }); }; }

module.exports = runAndUpdateCI;