/*
 * @Author: sjl
 * @Date: 2023-05-07 20:15:43
 * @Descripttion:
 */
// #!/usr/bin/env node

import { runBuildConfig } from './buildConf';
import chalk from 'chalk';

const fs = require('fs');
const path = require('path');

const filename = path.join(__dirname, process.argv[1]);

function removeFile(filename) {
  fs.unlink(filename, (err) => {
    if (err) {
      console.error('删除本地配置文件:', err);
    } else {
      console.log(`删除本地配置文件: ${filename} 成功！`);
    }
  });
}

import pkg from '../../package.json';

export const runBuild = async () => {
  try {
    const argvList = process.argv.splice(2);

    // Generate configuration file
    if (!argvList.includes('disabled-config')) {
      runBuildConfig();
    }

    console.log(`✨ ${chalk.cyan(`[${pkg.name}]`)}` + ' - build successfully!');
    console.log(`✨ ${chalk.cyan(`[${pkg.name}]`)}` + filename + '打包完成了!');
    removeFile('./target/dist/_app.config.js');
    removeFile('./target/dist/grant.js');
    removeFile('./target/dist/mapConfig.js');
    removeFile('./target/dist/assets/systemLogo.f771b316.png');
    removeFile('./target/dist/favicon.ico');
  } catch (error) {
    console.log(chalk.red('vite build error:\n' + error));
    process.exit(1);
  }
};
runBuild();
