"use strict";

var rm = require('rimraf');

var path = require('path');

var webpack = require('webpack');

var chalk = require('chalk');

var ora = require('ora');

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

var exec = require('../scripts/helpers/exec');

var webpackConfig = require('./webpack.prod.babel');

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

var spinner = ora('building for production...');
spinner.start();

function logStep(name) {
  console.log("".concat(chalk.gray('>> Release:'), " ").concat(chalk.magenta.bold(name)));
}

function packageExists(_ref) {
  var name = _ref.name,
      version = _ref.version;

  var _execa$sync = execa.sync('npm', ['info', "".concat(name, "@").concat(version)]),
      stdout = _execa$sync.stdout;

  return stdout.length > 0;
}

function release() {
  return regeneratorRuntime.async(function release$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
        case "end":
          return _context.stop();
      }
    }
  });
}

webpack(webpackConfig, function (err, stats) {
  spinner.stop();
  if (err) throw err;
  process.stdout.write(stats.toString({
    colors: true,
    color: true,
    progress: true,
    modules: false,
    children: false,
    chunks: true,
    chunkModules: true
  }) + '\n\n');

  if (stats.hasErrors()) {
    console.log(chalk.red('  Build failed with errors.\n'));
    process.exit(1);
  }

  console.log(chalk.cyan('  Build complete.\n'));
  console.log(chalk.yellow('  Tip: built files are meant to be served over an HTTP server.\n' + '  Opening index.html over file:// won\'t work.\n')); // const bumps = ['patch', 'minor', 'major', 'prerelease-alpha', 'prerelease-beta', 'premajor'];
  //提交

  function release() {
    var pkg;
    return regeneratorRuntime.async(function release$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _context2.prev = 0;
            _context2.next = 3;
            return regeneratorRuntime.awrap(exec('git', ['add', '.']));

          case 3:
            _context2.next = 5;
            return regeneratorRuntime.awrap(exec('git', ['commit', '-m', 'chore: packages', '-n']));

          case 5:
            _context2.next = 7;
            return regeneratorRuntime.awrap(exec('git', ['push', '-n']));

          case 7:
            _context2.next = 11;
            break;

          case 9:
            _context2.prev = 9;
            _context2.t0 = _context2["catch"](0);

          case 11:
            _context2.next = 13;
            return regeneratorRuntime.awrap(exec('np', ['patch', '--no-publish', '--no-cleanup', '--yolo', '-n']));

          case 13:
            _context2.next = 15;
            return regeneratorRuntime.awrap(exec('npm', ['run', 'log']));

          case 15:
            _context2.prev = 15;
            _context2.next = 18;
            return regeneratorRuntime.awrap(fs.copy('./civbase', '../CivWebPublish/build/civbase'));

          case 18:
            pkg = require('../../package.json');
            _context2.next = 21;
            return regeneratorRuntime.awrap(fs.copy('./civbase/manifest.json', "../CivWebPublish/build/civbase/manifest_".concat(pkg.version, ".json")));

          case 21:
            console.log('success!');
            _context2.next = 27;
            break;

          case 24:
            _context2.prev = 24;
            _context2.t1 = _context2["catch"](15);
            console.error(_context2.t1);

          case 27:
          case "end":
            return _context2.stop();
        }
      }
    }, null, null, [[0, 9], [15, 24]]);
  } //发布
  //比较上一个版本资源与当前资源文件指纹比对


  release()["catch"](function (err) {
    console.error(err);
    process.exit(1);
  });
});