'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports['default'] = build;

var _webpack = require('webpack');

var _webpack2 = _interopRequireDefault(_webpack);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

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

var _fsExtra2 = _interopRequireDefault(_fsExtra);

var _mkdirp = require('mkdirp');

var _mkdirp2 = _interopRequireDefault(_mkdirp);

var _rimraf = require('rimraf');

var _rimraf2 = _interopRequireDefault(_rimraf);

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

var _context2 = _interopRequireDefault(_context);

var _warning = require('../common/warning');

var _warning2 = _interopRequireDefault(_warning);

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

var _initialize2 = _interopRequireDefault(_initialize);

var _getAllFiles = require('./common/getAllFiles');

var _getAllFiles2 = _interopRequireDefault(_getAllFiles);

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

function transformVendor() {
  var _context$choerodonCon = _context2['default'].choerodonConfig,
      output = _context$choerodonCon.output,
      distBasePath = _context$choerodonCon.distBasePath;


  var venderName = (0, _getAllFiles2['default'])();
  if (venderName !== '') {
    var venderPath = _path2['default'].join(process.cwd(), distBasePath, output, 'dis', venderName);
    var venderStr = _fsExtra2['default'].readFileSync(venderPath, { encoding: 'utf-8' });
    _fsExtra2['default'].writeFileSync(venderPath, venderStr.replace(/dis/g, 'lib/dist/dis'));
  }
}

function copyAndTransform() {
  var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'index.html';
  var _context$choerodonCon2 = _context2['default'].choerodonConfig,
      output = _context$choerodonCon2.output,
      distBasePath = _context$choerodonCon2.distBasePath,
      htmlPath = _context$choerodonCon2.htmlPath;


  var htmlDistPath = _path2['default'].join(process.cwd(), distBasePath, output, name);
  var dest = _path2['default'].join(process.cwd(), htmlPath, name);

  _fsExtra2['default'].copySync(htmlDistPath, dest);

  var str = _fsExtra2['default'].readFileSync(dest, { encoding: 'utf-8' });
  var transformStr = str.replace(/href="\/dis/g, 'href="/lib/dist/dis').replace(/src="\/dis/g, 'src="/lib/dist/dis').replace(/\/favicon/g, '/lib/dist/favicon');
  _fsExtra2['default'].writeFileSync(dest, transformStr);

  _fsExtra2['default'].unlink(htmlDistPath);
}

function dist(mainPackage, env) {
  var _context$choerodonCon3 = _context2['default'].choerodonConfig,
      entryName = _context$choerodonCon3.entryName,
      output = _context$choerodonCon3.output,
      distBasePath = _context$choerodonCon3.distBasePath;

  var distPath = _path2['default'].join(process.cwd(), distBasePath, output);
  _rimraf2['default'].sync(distPath);
  _mkdirp2['default'].sync(distPath);
  (0, _generateEntryFile2['default'])(mainPackage, entryName);

  var webpackConfig = (0, _updateWebpackConfig2['default'])('build', env);
  webpackConfig.plugins.push(new _webpack2['default'].DefinePlugin({
    'process.env.NODE_ENV': JSON.stringify(env)
  }));

  (0, _webpack2['default'])(webpackConfig, function (err, stats) {
    if (err !== null) {
      (0, _warning2['default'])(false, err);
    } else if (stats.hasErrors()) {
      (0, _warning2['default'])(false, stats.toString('errors-only'));
    }
    copyAndTransform();
    copyAndTransform('withoutsider.html');
    transformVendor();
    (0, _copyFonts2['default'])();
  });
}

function build(program) {
  (0, _initialize2['default'])(program);
  var env = program.env || process.env.NODE_ENV || 'production';
  if (program.modules) {
    (0, _installSubmoduleDependencies2['default'])(function (mainPackage) {
      return dist(mainPackage, env);
    });
  } else {
    var mainPackagePath = (0, _getPackagePath2['default'])();
    var mainPackage = require(mainPackagePath);
    dist(mainPackage, env);
  }
}