'use strict';

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

let init = (() => {
  var _ref = _asyncToGenerator(function* () {
    const app = yield createApp();
    const server = (0, _http.createServer)(app);
    startServer(server);
    return app;
  });

  return function init() {
    return _ref.apply(this, arguments);
  };
})();

let createApp = (() => {
  var _ref2 = _asyncToGenerator(function* () {
    var _ref3 = yield require('@reactant/web-isomorphic/server').default;

    const app = _ref3.app;

    app.disable('x-powered-by');
    app.use(function (err, req, res, _next) {
      if (err) {
        _log2.default.error(err.stack);
        if (module.hot) {
          serverError = true;
          req.err = err;
          return errorApp.handle(req, res);
        }
        return res.status(500).send('Server error');
      }
      return res.status(404).send('Page not found');
    });
    return app;
  });

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

require('babel-polyfill');

var _express = require('express');

var _express2 = _interopRequireDefault(_express);

var _log = require('@reactant/core/log');

var _log2 = _interopRequireDefault(_log);

var _ora = require('ora');

var _ora2 = _interopRequireDefault(_ora);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _ejs = require('ejs');

var _core = require('@reactant/core');

var _http = require('http');

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

if (_core.config.options.verbose) (0, _log.setLevel)('verbose');
if (_core.config.options.debug) (0, _log.setLevel)('debug');

let errorApp = null;
let serverError = false;
let serverSpinner = (0, _ora2.default)('starting server').start();

if (module.hot) {
  errorApp = (0, _express2.default)();
  errorApp.disable('x-powered-by');
  errorApp.set('views', _path2.default.resolve(__dirname, 'views'));
  errorApp.set('view engine', 'ejs');
  errorApp.engine('.ejs', _ejs.__express);
  errorApp.use(_express2.default.static(_path2.default.resolve(__dirname, 'views')));
  errorApp.use((req, res) => {
    return res.render('error', { config: _core.config, errStack: req.err.stack });
  });
}

function startServer(server) {
  const platform = _core.config.platform;

  server.listen(_core.config.port, err => {
    if (err) throw err;
    serverSpinner.succeed(`server listening on port ${_core.config.port}`);
    if (module.hot) {
      module.hot.accept(`~/../${platform}/server.js`, () => {
        _log2.default.info('[HMR] updating HMR');
        if (serverError) {
          serverError = false;
          serverSpinner = (0, _ora2.default)('restarting server');
          server.close(() => {
            const app = createApp();
            const newServer = (0, _http.createServer)(app);
            startServer(newServer);
          });
        }
      });
      _log2.default.info('[HMR] server HMR enabled');
    }
  });
}

exports.default = init();