<commit_before>/*eslint no-console: 0 */
// @flow

import {which} from 'shelljs'
import exec from '../util/exec'
import program from 'commander'
import chalk from 'chalk'
import WorkingDirectory from '../lib/working-directory'
import PackageJSONLoader from '../lib/package-json-loader'
import generateCircleYml from './nca-generate'

const COMMAND_DESC = `
  bump-level:
    p: patch level (0.0.1)
    m: minor level (0.1.0)
    j: major level (1.0.0)
    r: re-release (0.0.0)
`

const optionNames = {
    p: 'patch',
    m: 'minor',
    j: 'major',
}


export default function run() {

    program
        .arguments('<bump-level>', /[pmjr]/)
        .option('-s, --skipCircle', 'No generation of circle.yml')
        .description(COMMAND_DESC)
        .parse(process.argv)

    const arg = program.args[0]

    if (!arg) {
        program.help()
    }

    const bin = which('bmp') || which('yangpao')

    if (!bin) {
        console.log(chalk.red(HOW_TO_INSTALL_BMP_OR_YANGPAO))
        process.exit(1)
    }

    if (program.skipCircle) {
        console.log('skip generating circle.yml')
    }
    else {
        generateCircleYml()
    }

    const verb = arg === 'r' ? 're-release' : 'release'

    const optionName = optionNames[arg]

    if (optionName) {
        exec(`${bin} --${optionName}`)
    }

    const version = getCurrentVersion()

    exec('git add -A')
    exec(`git commit --allow-empty -m "${verb} ${version}"`)
    console.log(NOTICE_AFTER_BUMPING)

    return 0
}


/**
 * Get the current version
 * @private
 */
function getCurrentVersion(): string {
    const cwd = new WorkingDirectory().resolve()
    return PackageJSONLoader.load(cwd).version
}


const HOW_TO_INSTALL_BMP_OR_YANGPAO = `
    You need to install one of the version-bumping tools of the followings.

        - [bmp](https://github.com/kt3k/bmp)
        - [yangpao](https://github.com/januswel/yangpao)

    ## install bmp
    \tgem install bmp

    ## install yangpao
    \tgo get github.com/januswel/yangpao
`

/**
 * Show notice after git commit
 * @private
 */
const NOTICE_AFTER_BUMPING = `
--------------------------------------------------------
   if you mistakenly ran this command, you can reset by<commit_msg>show how to skip circle.yml generation<commit_after>/*eslint no-console: 0 */
// @flow

import {which} from 'shelljs'
import exec from '../util/exec'
import program from 'commander'
import chalk from 'chalk'
import WorkingDirectory from '../lib/working-directory'
import PackageJSONLoader from '../lib/package-json-loader'
import generateCircleYml from './nca-generate'

const COMMAND_DESC = `
  bump-level:
    p: patch level (0.0.1)
    m: minor level (0.1.0)
    j: major level (1.0.0)
    r: re-release (0.0.0)
`

const optionNames = {
    p: 'patch',
    m: 'minor',
    j: 'major',
}


export default function run() {

    program
        .arguments('<bump-level>', /[pmjr]/)
        .option('-s, --skipCircle', 'No generation of circle.yml')
        .description(COMMAND_DESC)
        .parse(process.argv)

    const arg = program.args[0]

    if (!arg) {
        program.help()
    }

    const bin = which('bmp') || which('yangpao')

    if (!bin) {
        console.log(chalk.red(HOW_TO_INSTALL_BMP_OR_YANGPAO))
        process.exit(1)
    }

    if (program.skipCircle) {
        console.log('skip generating circle.yml')
    }
    else {
        generateCircleYml()
        console.log(HOW_TO_SKIP_GENERATION_OF_CIRCLE_YML)
    }

    const verb = arg === 'r' ? 're-release' : 'release'

    const optionName = optionNames[arg]

    if (optionName) {
        exec(`${bin} --${optionName}`)
    }

    const version = getCurrentVersion()

    exec('git add -A')
    exec(`git commit --allow-empty -m "${verb} ${version}"`)
    console.log(NOTICE_AFTER_BUMPING)

    return 0
}


/**
 * Get the current version
 * @private
 */
function getCurrentVersion(): string {
    const cwd = new WorkingDirectory().resolve()
    return PackageJSONLoader.load(cwd).version
}

const HOW_TO_SKIP_GENERATION_OF_CIRCLE_YML = `

    To skip circle.yml generation,
    run with --skipCircle (or -s) option.

`

const HOW_TO_INSTALL_BMP_OR_YANGPAO = `
    You need to install one of the version-bumping tools of the followings.

        - [bmp](https://github.com/kt3k/bmp)
        - [yangpao](https://github.com/januswel/yangpao)

    ## install bmp
    \tgem install bmp

    ## install yangpao
    \tgo get github.com/januswel/yangpao
`

/**
 * Show notice after git commit
 * @private
 */
const NOTICE_AFTER_BUMPING = `
--------------------------------------------------------
   if you mistakenly ran this command, you can reset by
{'fraction_non_alphanumeric': 0.1587335774283868, 'fraction_numerical': 0.007322851604566014, 'mean_word_length': 2.494356659142212, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '763973', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1536, 'n_words': 377}