"use strict";

/* eslint consistent-return:0 import/order:0 */
var express = require('express');

var logger = require('./logger');

var argv = require('./argv');

var port = require('./port');

var setup = require('./middlewares/frontendMiddleware');

var isDev = process.env.NODE_ENV !== 'production';

var pkg = require('../package.json');

var ngrok = isDev && process.env.ENABLE_TUNNEL || argv.tunnel ? require('ngrok') : false;

var _require = require('path'),
    resolve = _require.resolve;

var app = express();
setup(app, {
  outputPath: resolve(process.cwd(), pkg.name.toLocaleLowerCase()),
  publicPath: "/".concat(pkg.name.toLocaleLowerCase())
});
var customHost = argv.host || process.env.HOST;
var host = customHost || null;
var prettyHost = customHost || 'localhost'; // use the gzipped bundle

app.get('*.js', function (req, res, next) {
  req.url = req.url + '.gz'; // eslint-disable-line

  res.set('Content-Encoding', 'gzip');
  next();
}); // Start your app.

app.listen(port, host, function _callee(err) {
  var url;
  return regeneratorRuntime.async(function _callee$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          if (!err) {
            _context.next = 2;
            break;
          }

          return _context.abrupt("return", logger.error(err.message));

        case 2:
          if (!ngrok) {
            _context.next = 15;
            break;
          }

          _context.prev = 3;
          _context.next = 6;
          return regeneratorRuntime.awrap(ngrok.connect(port));

        case 6:
          url = _context.sent;
          _context.next = 12;
          break;

        case 9:
          _context.prev = 9;
          _context.t0 = _context["catch"](3);
          return _context.abrupt("return", logger.error(_context.t0));

        case 12:
          logger.appStarted(port, prettyHost, url);
          _context.next = 16;
          break;

        case 15:
          logger.appStarted(port, prettyHost);

        case 16:
        case "end":
          return _context.stop();
      }
    }
  }, null, null, [[3, 9]]);
});