'use strict';

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

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

var _extends3 = _interopRequireDefault(_extends2);

exports['default'] = start;

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _webpack = require('webpack');

var _webpack2 = _interopRequireDefault(_webpack);

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

var _webpackDevServer2 = _interopRequireDefault(_webpackDevServer);

var _openBrowser = require('react-dev-utils/openBrowser');

var _openBrowser2 = _interopRequireDefault(_openBrowser);

var _context = require('./common/context');

var _context2 = _interopRequireDefault(_context);

var _initialize = require('./common/initialize');

var _initialize2 = _interopRequireDefault(_initialize);

var _getPackagePath = require('./common/getPackagePath');

var _getPackagePath2 = _interopRequireDefault(_getPackagePath);

var _generateEntryFile = require('./common/generateEntryFile');

var _generateEntryFile2 = _interopRequireDefault(_generateEntryFile);

var _updateWebpackConfig = require('../config/updateWebpackConfig');

var _updateWebpackConfig2 = _interopRequireDefault(_updateWebpackConfig);

var _installSubmoduleDependencies = require('./common/installSubmoduleDependencies');

var _installSubmoduleDependencies2 = _interopRequireDefault(_installSubmoduleDependencies);

var _copyFonts = require('./common/copyFonts');

var _copyFonts2 = _interopRequireDefault(_copyFonts);

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

/**
 * generateEntryFile and start webpack-dev-server
 * @param {*} mainPackage workspace pkg.json object
 * @param {*} dev 
 */
function run(mainPackage, dev) {
  var _context$choerodonCon = _context2['default'].choerodonConfig,
      entryName = _context$choerodonCon.entryName,
      devServerConfig = _context$choerodonCon.devServerConfig,
      output = _context$choerodonCon.output,
      port = _context$choerodonCon.port,
      proxyTarget = _context$choerodonCon.proxyTarget;

  (0, _generateEntryFile2['default'])(mainPackage, entryName, dev);

  var webpackConfig = (0, _updateWebpackConfig2['default'])('start', 'development');
  webpackConfig.plugins.push(new _webpack2['default'].HotModuleReplacementPlugin());
  var serverOptions = (0, _extends3['default'])({
    quiet: true,
    hot: true
  }, devServerConfig, {
    // contentBase: path.join(process.cwd(), output),
    contentBase: _path2['default'].join(process.cwd(), 'src', 'main', 'resources', 'lib', output),
    historyApiFallback: true,
    host: 'localhost',
    proxy: [{
      context: ['**', '!/', '!/dis/**'],
      target: proxyTarget,
      changeOrigin: true,
      secure: false,
      autoRewrite: true
    }]
  });
  _webpackDevServer2['default'].addDevServerEntrypoints(webpackConfig, serverOptions);
  (0, _copyFonts2['default'])();

  var compiler = (0, _webpack2['default'])(webpackConfig);

  var timefix = 11000;
  compiler.plugin('watch-run', function (watching, callback) {
    watching.startTime += timefix;
    callback();
  });
  compiler.plugin('done', function (stats) {
    stats.startTime -= timefix;
  });

  var server = new _webpackDevServer2['default'](compiler, serverOptions);
  server.listen(port, '0.0.0.0', function () {
    return (0, _openBrowser2['default'])('http://localhost:' + port);
  });
}

function start(program, dev) {
  (0, _initialize2['default'])(program, dev);
  if (!dev && program.modules) {
    (0, _installSubmoduleDependencies2['default'])(run);
  } else {
    var mainPackagePath = (0, _getPackagePath2['default'])();
    var mainPackage = require(mainPackagePath);
    run(mainPackage);
  }
}