#!/usr/bin/env node
const pkg = require('../package.json');
const { program } = require('commander');
const generateFile = require('../lib/generate');
const revertFile = require('../lib/revert');
const chalk = require('chalk');

program.version(`${pkg.name} ${pkg.version}`).usage('<command> [options]');

program
  .command('generate <src> <langFolder>')
  .usage('<src> <langFolder> [options]')
  .description(
    `✨ 对当前命令执行下的 src 目录下的 vue/ts/js 文件进行国际化替换生成，\n${chalk.yellow(
      'eg：npx kt-vue3-i18n-generator generate ./src ./src/locale/lang'
    )}`
  )
  .option('-e, --exect', '精确生成（去掉冗余的 key）')
  .option('-f, --fileName <fileName>', '设置生成文件名，默认 zh_CN.js')
  .action((src, langFolder, { exect = false, fileName = 'zh_CN' }) => {
    generateFile.generate(src, langFolder, { fileName, exect });
  });

program
  .command('revert <src> <langFolder>')
  .usage('<src> <langFolder> [options]')
  .description(
    `✨ 对当前命令执行下的 src 目录下的 vue/ts/js 文件进行国际化还原，\n${chalk.yellow(
      'eg：npx kt-vue3-i18n-generator revert ./src ./src/locale/lang'
    )}`
  )
  .option('-f, --fileName <fileName>', '设置国际化文件名，默认 zh_CN.js')
  .action((src, langFolder, { fileName = 'zh_CN' }) => {
    revertFile.revert(src, langFolder, { fileName });
  });

program.on('command:*', function () {
  console.error(
    'Invalid command: %s\nSee --help for a list of available commands.',
    program.args.join(' ')
  );
  process.exit(1);
});

if (process.argv.length === 2) {
  program.help();
}

program.parse(process.argv);
