"use strict";

var _jest = require("jest");

var _fs = _interopRequireDefault(require("fs"));

var _utils = require("../utils");

var _jsonHelper = require("../utils/jsonHelper");

var _coverageCollector = _interopRequireDefault(require("./coverageCollector"));

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

let options = (0, _utils.getOptions)();
let {
  app: {
    folder
  },
  test: {
    branchName,
    srcBranch,
    targetBranch,
    serviceName,
    impactServerDomain,
    isCommitCoverage
  }
} = options;
let argv = process.argv.slice(2);

let config = require('../configs/jest.config');

if (isCommitCoverage) {
  (0, _coverageCollector.default)(srcBranch, targetBranch, serviceName, impactServerDomain).then((res = {}) => {
    if (res.STATUS) {
      if (res.LIST.length > 0) {
        let content = {
          FILES: res.LIST
        };

        if (!_fs.default.existsSync('./coverageTest')) {
          _fs.default.mkdirSync('./coverageTest');

          _fs.default.writeFileSync('./coverageTest/result.json', JSON.stringify(content), 'utf8');
        } else {
          _fs.default.writeFileSync('./coverageTest/result.json', JSON.stringify(content), 'utf8');
        }

        argv.push('--findRelatedTests');
        argv = argv.concat(res.LIST);
        argv = argv.concat(['--config', JSON.stringify(config(null, true))]);
        argv.push('--no-cache');
        argv.push('--silent');
        (0, _jest.run)(argv);
      } else {
        (0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, ['tests', 'modifiedFileUnitCase', 'hasChanges'], false); // jsonHelper(
        //   `${process.cwd()}/result.json`,
        //   'testInfo.isBuildVerified',
        //   true
        // );

        (0, _jsonHelper.setTestInfoStatus)(`${process.cwd()}/result.json`, true);
        (0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, 'testInfo.failures', [{
          type: 'modifiedUnitcases',
          message: res.REASON
        }]);
        (0, _utils.log)('No JS changes with last changeset!');
      }
    } else {
      (0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, ['tests', 'modifiedFileUnitCase', 'isExecuted'], res.STATUS); // jsonHelper(
      //     `${process.cwd()}/result.json`,
      //     'testInfo.isBuildVerified',
      //     true
      // );

      (0, _jsonHelper.setTestInfoStatus)(`${process.cwd()}/result.json`, true); // jsonHelper(`${process.cwd()}/result.json`, 'testInfo.failures', [{
      //     type: 'modifiedUnitcases',
      //     message: res.REASON
      // }]);

      (0, _utils.log)(res.REASON);
    }
  });
} else {
  if (branchName === 'master' || branchName === null) {
    argv.push('--config', JSON.stringify(config(folder)), '--no-cache');
    (0, _jest.run)(argv);
  } else {
    (0, _jsonHelper.jsonHelper)(`${process.cwd()}/result.json`, ['tests', 'unitCase', 'isExecuted'], 'onlyMaster');
    (0, _utils.log)('Only runs with master branch');
  }
}