
const rm = require('rimraf');
const path = require('path');
const webpack = require('webpack');
const chalk = require('chalk');
const ora = require('ora');
const fs = require('fs-extra');

const exec = require('../scripts/helpers/exec');
const webpackConfig = require('./webpack.prod.babel')
const argv = require('../../server/argv');
const spinner = ora('building for production...')
spinner.start()

function logStep(name) {
  console.log(`${chalk.gray('>> Release:')} ${chalk.magenta.bold(name)}`);
}

function packageExists({ name, version }) {
  const { stdout } = execa.sync('npm', ['info', `${name}@${version}`]);
  return stdout.length > 0;
}
async function release() {
  
}
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'];
  //提交
  
  async function release() {
    try {
      await exec('git', ['add', '.']);
      await exec('git', ['commit', '-m', 'chore: packages', '-n']);
      await exec('git', ['push', '-n']);
    } catch(error) {
  
    }
    await exec('np', ['patch', '--no-publish', '--no-cleanup', '--yolo', '-n']);
    await exec('npm', ['run', 'log']);
    try {
      await fs.copy('./civbase', '../CivWebPublish/build/civbase');
      const pkg = require('../../package.json');
      await fs.copy('./civbase/manifest.json', `../CivWebPublish/build/civbase/manifest_${pkg.version}.json`);
      console.log('success!')
    } catch (err) {
      console.error(err)
    }
  }

  //发布

  //比较上一个版本资源与当前资源文件指纹比对

  release().catch((err) => {
    console.error(err);
    process.exit(1);
  });
  
})
