import { getPackageInfo } from 'utils/file'
import chalk from 'chalk'
import semver from 'semver'
import inquirer from 'inquirer'
import { setTag } from 'utils/git'
import { npm } from 'utils/npm'

async function commitTag() {
  const { version } = getPackageInfo()
  const releaseVersion = version
  const result = await inquirer.prompt({
    type: 'list',
    name: 'incType',
    message: '自动升级版本，请选择升级版本类型',
    default: 'patch',
    choices: [
      {
        name: `小版本（${releaseVersion} -> ${semver.inc(
          releaseVersion,
          'patch'
        )}）`,
        value: 'patch',
      },
      {
        name: `中版本（${releaseVersion} -> ${semver.inc(
          releaseVersion,
          'minor'
        )}）`,
        value: 'minor',
      },
      {
        name: `大版本（${releaseVersion} -> ${semver.inc(
          releaseVersion,
          'major'
        )}）`,
        value: 'major',
      },
    ],
  })

  const incVersion = semver.inc(releaseVersion, result.incType) as string
  npm(['version', result.incType]).then((res: any) => {
    if(res.status === 0) {
      if(setTag(incVersion)) {
        console.log(chalk.green('标签添加成功'))
      } else {
        console.log(chalk.red('标签添加失败'))
      }
    } else {
      console.log(chalk.red('标签添加失败'))
    }
  })
}
export default commitTag
