'use strict';

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

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _bluebird = require('bluebird');

var _bluebird2 = _interopRequireDefault(_bluebird);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _helpers = require('../formatter/helpers');

var _helpers2 = require('./helpers');

var _install_validator = require('./install_validator');

var _i18n = require('./i18n');

var I18n = _interopRequireWildcard(_i18n);

var _configuration_builder = require('./configuration_builder');

var _configuration_builder2 = _interopRequireDefault(_configuration_builder);

var _events = require('events');

var _events2 = _interopRequireDefault(_events);

var _builder = require('../formatter/builder');

var _builder2 = _interopRequireDefault(_builder);

var _fs = require('mz/fs');

var _fs2 = _interopRequireDefault(_fs);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _pickle_filter = require('../pickle_filter');

var _pickle_filter2 = _interopRequireDefault(_pickle_filter);

var _master = require('../runtime/parallel/master');

var _master2 = _interopRequireDefault(_master);

var _runtime = require('../runtime');

var _runtime2 = _interopRequireDefault(_runtime);

var _support_code_library_builder = require('../support_code_library_builder');

var _support_code_library_builder2 = _interopRequireDefault(_support_code_library_builder);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

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

var Cli = function () {
  function Cli(_ref) {
    var argv = _ref.argv,
        cwd = _ref.cwd,
        stdout = _ref.stdout;
    (0, _classCallCheck3.default)(this, Cli);

    this.argv = argv;
    this.cwd = cwd;
    this.stdout = stdout;
  }

  (0, _createClass3.default)(Cli, [{
    key: 'getConfiguration',
    value: function () {
      var _ref2 = (0, _bluebird.coroutine)(function* () {
        var fullArgv = yield (0, _helpers2.getExpandedArgv)({ argv: this.argv, cwd: this.cwd });
        return _configuration_builder2.default.build({ argv: fullArgv, cwd: this.cwd });
      });

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

      return getConfiguration;
    }()
  }, {
    key: 'initializeFormatters',
    value: function () {
      var _ref4 = (0, _bluebird.coroutine)(function* (_ref3) {
        var _this = this;

        var eventBroadcaster = _ref3.eventBroadcaster,
            formatOptions = _ref3.formatOptions,
            formats = _ref3.formats,
            supportCodeLibrary = _ref3.supportCodeLibrary;

        var streamsToClose = [];
        var eventDataCollector = new _helpers.EventDataCollector(eventBroadcaster);
        yield _bluebird2.default.map(formats, function () {
          var _ref6 = (0, _bluebird.coroutine)(function* (_ref5) {
            var _context;

            var type = _ref5.type,
                outputTo = _ref5.outputTo;

            var stream = _this.stdout;
            if (outputTo) {
              var fd = yield _fs2.default.open(_path2.default.resolve(_this.cwd, outputTo), 'w');
              stream = _fs2.default.createWriteStream(null, { fd: fd });
              streamsToClose.push(stream);
            }
            var typeOptions = (0, _extends3.default)({
              eventBroadcaster: eventBroadcaster,
              eventDataCollector: eventDataCollector,
              log: (_context = stream).write.bind(_context),
              stream: stream,
              supportCodeLibrary: supportCodeLibrary
            }, formatOptions);
            return _builder2.default.build(type, typeOptions);
          });

          return function (_x2) {
            return _ref6.apply(this, arguments);
          };
        }());
        return function () {
          return _bluebird2.default.each(streamsToClose, function (stream) {
            return _bluebird2.default.promisify(stream.end.bind(stream))();
          });
        };
      });

      function initializeFormatters(_x) {
        return _ref4.apply(this, arguments);
      }

      return initializeFormatters;
    }()
  }, {
    key: 'getSupportCodeLibrary',
    value: function getSupportCodeLibrary(_ref7) {
      var supportCodeRequiredModules = _ref7.supportCodeRequiredModules,
          supportCodePaths = _ref7.supportCodePaths;

      supportCodeRequiredModules.map(function (module) {
        return require(module);
      });
      _support_code_library_builder2.default.reset(this.cwd);
      supportCodePaths.forEach(function (codePath) {
        return require(codePath);
      });
      return _support_code_library_builder2.default.finalize();
    }
  }, {
    key: 'run',
    value: function () {
      var _ref8 = (0, _bluebird.coroutine)(function* () {
        yield (0, _install_validator.validateInstall)(this.cwd);
        var configuration = yield this.getConfiguration();
        if (configuration.listI18nLanguages) {
          this.stdout.write(I18n.getLanguages());
          return { success: true };
        }
        if (configuration.listI18nKeywordsFor) {
          this.stdout.write(I18n.getKeywords(configuration.listI18nKeywordsFor));
          return { success: true };
        }
        var supportCodeLibrary = this.getSupportCodeLibrary(configuration);
        var eventBroadcaster = new _events2.default();
        var cleanup = yield this.initializeFormatters({
          eventBroadcaster: eventBroadcaster,
          formatOptions: configuration.formatOptions,
          formats: configuration.formats,
          supportCodeLibrary: supportCodeLibrary
        });
        var testCases = yield (0, _helpers2.getTestCasesFromFilesystem)({
          cwd: this.cwd,
          eventBroadcaster: eventBroadcaster,
          featureDefaultLanguage: configuration.featureDefaultLanguage,
          featurePaths: configuration.featurePaths,
          order: configuration.order,
          pickleFilter: new _pickle_filter2.default(configuration.pickleFilterOptions)
        });
        var success = void 0;
        if (configuration.parallel) {
          var parallelRuntimeMaster = new _master2.default({
            eventBroadcaster: eventBroadcaster,
            options: configuration.runtimeOptions,
            supportCodePaths: configuration.supportCodePaths,
            supportCodeRequiredModules: configuration.supportCodeRequiredModules,
            testCases: testCases
          });
          yield new _bluebird2.default(function (resolve) {
            parallelRuntimeMaster.run(configuration.parallel, function (s) {
              success = s;
              resolve();
            });
          });
        } else {
          var runtime = new _runtime2.default({
            eventBroadcaster: eventBroadcaster,
            options: configuration.runtimeOptions,
            supportCodeLibrary: supportCodeLibrary,
            testCases: testCases
          });
          success = yield runtime.start();
        }
        yield cleanup();
        return {
          shouldExitImmediately: configuration.shouldExitImmediately,
          success: success
        };
      });

      function run() {
        return _ref8.apply(this, arguments);
      }

      return run;
    }()
  }]);
  return Cli;
}();

exports.default = Cli;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 10896 characters]