'use strict';

var _typeof2 = require('babel-runtime/helpers/typeof');

var _typeof3 = _interopRequireDefault(_typeof2);

var _promise = require('babel-runtime/core-js/promise');

var _promise2 = _interopRequireDefault(_promise);

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

var findBin = require('./findBin');
var execa = require('execa');

module.exports = function runScript(commands, pathsToLint, packageJson, options) {
  var trackersArray = Array.isArray(commands) ? commands : [commands];
  return trackersArray.map(function (tracker) {
    return {
      title: tracker,
      task: function task() {
        try {
          var _ret = function () {
            var res = findBin(tracker, pathsToLint, packageJson, options);
            var execaOptions = res.bin !== 'npm' && options && options.gitDir ? { cwd: options.gitDir } : {};
            return {
              v: new _promise2.default(function (resolve, reject) {
                execa(res.bin, res.args, execaOptions).then(function () {
                  resolve(tracker + ' passed!');
                }).catch(function (err) {
                  reject(new Error('\n\uD83D\uDEAB  ' + tracker + ' failed.\n\n' + err.stderr + '\n' + err.stdout + '\n                '));
                });
              })
            };
          }();

          if ((typeof _ret === 'undefined' ? 'undefined' : (0, _typeof3.default)(_ret)) === "object") return _ret.v;
        } catch (err) {
          throw err;
        }
      }
    };
  });
};