"use strict";

var _cluster = _interopRequireDefault(require("cluster"));
var _minimist = _interopRequireDefault(require("minimist"));
var _server = _interopRequireDefault(require("./server"));
var _types = require("./types");
var _messages = require("./server/messages");
var _utils = require("./server/utils");
var _loglevel = require("loglevel");
var _logger = require("./server/logger");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function shutdownOnException(reason) {
  if (_utils.isShuttingDown.current) return;
  const error = reason instanceof Error ? reason.stack ?? reason.message : reason;
  _logger.logger.error(error);
  process.exitCode = -1;
  if (_cluster.default.isWorker) (0, _messages.emitWorkerMessage)({
    type: 'error',
    payload: {
      error
    }
  });
  if (_cluster.default.isPrimary && !_utils.isShuttingDown.current) void (0, _utils.shutdownWorkers)();
}
process.on('uncaughtException', shutdownOnException);
process.on('unhandledRejection', shutdownOnException);
if (_cluster.default.isWorker) process.on('SIGINT', _types.noop);
if (_cluster.default.isPrimary) process.on('SIGINT', _utils.shutdown);
const argv = (0, _minimist.default)(process.argv.slice(2), {
  string: ['browser', 'config', 'reporter', 'reportDir', 'screenDir', 'storybookUrl'],
  boolean: ['debug', 'trace', 'ui', 'saveReport', 'tests'],
  default: {
    port: 3000,
    saveReport: true
  },
  alias: {
    port: 'p',
    config: 'c',
    debug: 'd',
    update: 'u'
  }
});

// @ts-expect-error: define log level for storybook
global.LOGLEVEL = argv.trace ? 'trace' : argv.debug ? 'debug' : 'warn';
if (argv.trace) {
  _logger.logger.setDefaultLevel(_loglevel.levels.TRACE);
  (0, _loglevel.setDefaultLevel)(_loglevel.levels.TRACE);
} else if (argv.debug) {
  _logger.logger.setDefaultLevel(_loglevel.levels.DEBUG);
  (0, _loglevel.setDefaultLevel)(_loglevel.levels.DEBUG);
} else {
  _logger.logger.setDefaultLevel(_loglevel.levels.INFO);
  (0, _loglevel.setDefaultLevel)(_loglevel.levels.INFO);
}
void (0, _server.default)(argv);