1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
const { exec, execSync } = require('child_process') const inquirer = require('inquirer') const chalk = require('chalk') const ora = require('ora') const consola = require('consola') const { trim } = require('lodash') const { name: projectName, version: currentVersion } = require('../package')
const regVersion = /^[1-9]{1}\d*\.\d+\.\d+$/
const regLineBreak = /\s/
const getGitBranch = execSync('git name-rev --name-only HEAD', { encoding: 'utf-8' }).replace(regLineBreak, '')
const projectInfo = `(${chalk.yellowBright(getGitBranch)})`
const getNextDefaultVersion = () => { const numbers = currentVersion.split('.') const lastNumber = Number(numbers.pop()) return numbers.concat(lastNumber + 1).join('.') }
if (!/release/.test(getGitBranch)) { consola.warn('仅release分支用于版本更新,check-version脚本不会在其余分支运行,请勿手动修改package.json') process.exit(0) }
console.log('\n')
inquirer.prompt([ { type: 'confirm', name: 'needUpdateVersion', prefix: `${projectInfo}-`, suffix: `(当前版本号:${chalk.yellow(currentVersion)})`, message: `是否需要更新版本号?`, default: false }, { type: 'input', name: 'version', default: getNextDefaultVersion(), prefix: `${projectInfo}-`, suffix: `(当前版本号:${chalk.yellow(currentVersion)}) : \n`, message: `请输入版本号:`, when (question) { return question.needUpdateVersion }, validate (version) { if (!regVersion.test(version)) { console.log(chalk.yellow('输入的版本号格式未通过检查,请检查格式(eg:1.0.0,1.1.0)')) return false } return true } }, { type: 'confirm', name: 'needAddTag', message: `${projectInfo}(${chalk.yellow(currentVersion)})-是否为当前版本添加Tag?`, default: false, when ({ needUpdateVersion }) { return needUpdateVersion } }, { type: 'list', name: 'operationType', message: '请选择本次更新的类型', choices: ['fix', 'feat', 'chore', 'build', 'doc'], when (question) { return question.needAddTag } }, { type: 'input', name: 'versionDescr', message: '请输入版本描述*', when (question) { return question.needAddTag }, validate (desc) { return !!trim(desc) } } ]).then(async (answers) => { const { version: newVersion, versionDescr, operationType, needAddTag } = answers if (!answers.needUpdateVersion) { process.exit(0) } if (newVersion && newVersion !== currentVersion) { let spinner try { await command(`yarn version --no-git-tag-version --new-version ${newVersion}`) console.log(chalk.green(`\n ${projectName} package.json版本号更新成功,当前版本为:${newVersion}`)) spinner = ora('git commit中...请等待git hooks执行..').start() await command(`git add package.json && git commit -m "chore(package.json): 更新项目版本号为:${newVersion}"`) } catch (err) { err ? spinner.fail('git执行失败,请手动提交package.json') : spinner.succeed('git commit成功,请直接push代码') console.log(chalk.red(err)) console.log('\n') process.exit(1) } if (needAddTag) { try { spinner.start('正在对本次提交打tag,请耐心等待...') await command(`git tag -a v${newVersion} -m "${operationType}-${versionDescr}"`) spinner.start('正在push tag,请耐心等待...') await command(`git push origin --tags`) } catch (err) { console.log(err) consola.error('git Tag更新失败,请手动同步git tag') process.exit(1) } } await command(`git checkout develop`) console.log(chalk.green(`\n 校验通过,分支已切换回develop`)) spinner.stop() process.exit(0) } else { console.log(chalk.green(`本次未修改版本号,version:${newVersion} ! \n`)) } })
function command (cmd, options = {}) { return new Promise((resolve, reject) => { exec(cmd, options, (err, stdout, stderr) => { if (err) { reject(err) } else { console.log(stdout) resolve(true) } }) }) }
|