'use strict';

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

var _open = require('open');

var _open2 = _interopRequireDefault(_open);

var _webpackDevServer = require('webpack-dev-server');

var _webpackDevServer2 = _interopRequireDefault(_webpackDevServer);

var _easycp = require('easycp');

var _easycp2 = _interopRequireDefault(_easycp);

var _fsExtra = require('fs-extra');

var _fsExtra2 = _interopRequireDefault(_fsExtra);

var _ora = require('ora');

var _ora2 = _interopRequireDefault(_ora);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _webpack = require('webpack');

var _webpack2 = _interopRequireDefault(_webpack);

var _webpack3 = require('../webpack');

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

exports.default = (() => {
  var _ref = _asyncToGenerator(function* (config, { spinner, log, webpackConfig }) {
    const paths = config.paths,
          options = config.options,
          ports = config.ports,
          action = config.action,
          platform = config.platform,
          port = config.port;

    if (options.storybook) {
      _fsExtra2.default.mkdirsSync(paths.storybook);
      _fsExtra2.default.copySync(_path2.default.resolve(__dirname, '../storybook'), paths.storybook);
      const storybookPath = _path2.default.resolve(paths.root, 'storybook');
      if (_fsExtra2.default.existsSync(storybookPath) && _fsExtra2.default.lstatSync(storybookPath).isDirectory()) {
        _fsExtra2.default.copySync(storybookPath, paths.storybook);
      }
      spinner.stop();
      yield (0, _easycp2.default)(`node ${require.resolve('@storybook/react/bin')} -p ${config.ports.storybook} -c ${paths.storybook}${options.debug ? ' -- --debug' : ''}${options.verbose ? ' -- --verbose' : ''}`);
      return null;
    }
    process.noDeprecation = true;
    const webpackClientConfig = (0, _webpack3.createWebpackConfig)(config, webpackConfig, 'client');
    log.write('webpackClientConfig', webpackClientConfig);
    const webpackServerConfig = (0, _webpack3.createWebpackConfig)(config, webpackConfig, 'server');
    log.write('webpackServerConfig', webpackServerConfig);
    _fsExtra2.default.mkdirsSync(_path2.default.resolve(paths.dist, 'public'));
    _fsExtra2.default.mkdirsSync(_path2.default.resolve(paths.src, 'public'));
    _fsExtra2.default.copySync(_path2.default.resolve(__dirname, '../public'), _path2.default.resolve(paths.dist, 'public'));
    _fsExtra2.default.copySync(_path2.default.resolve(paths.src, 'public'), _path2.default.resolve(paths.dist, 'public'));
    spinner.stop();
    spinner = (0, _ora2.default)('compiling client').start();
    const serverCompiler = (0, _webpack2.default)(webpackServerConfig);
    const clientCompiler = (0, _webpack2.default)(webpackClientConfig);
    const server = new _webpackDevServer2.default(clientCompiler, webpackClientConfig.devServer);
    let started = false;
    yield new Promise(function (resolve) {
      clientCompiler.plugin('done', function () {
        if (!started) {
          spinner.succeed('compiled client');
          spinner = (0, _ora2.default)('compiling server').start();
          return resolve();
        }
        return (0, _ora2.default)('recompiled').succeed();
      });
    });
    yield new Promise(function (resolve) {
      serverCompiler.watch({ quiet: true, stats: 'none' }, function (err, stats) {
        var _handleStats = (0, _webpack3.handleStats)(stats, config);

        const errors = _handleStats.errors,
              warnings = _handleStats.warnings;

        if (errors.length) {
          spinner.fail('failed to compile server');
        } else if (!started) {
          started = true;
          spinner[warnings.length ? 'warn' : 'succeed']('compiled server');
          spinner = (0, _ora2.default)(`started ${action} ${platform}`).succeed();
        }
        return resolve();
      });
    });
    yield new Promise(function (resolve, reject) {
      server.listen(ports.dev, webpackClientConfig.devServer.host, function (err) {
        if (err) {
          spinner.stop();
          return reject(err);
        }
        return resolve();
      });
    });
    yield new Promise(function (r) {
      return setTimeout(r, 3000);
    });
    return (0, _open2.default)(`http://localhost:${port}`);
  });

  function start(_x, _x2) {
    return _ref.apply(this, arguments);
  }

  return start;
})();