yarn bump自动升级package.json里的版本号

Talk is cheap, show me the code:

$ cat bump.mjs 

#!/usr/bin/env zx
/**
 * * 自动升级版本号并打标签 *
 * 相当于修改package.json里面version字段值,然后git commit -m 'xxx',再git tag -a ${version}
 * */

// https://github.com/google/zx
import 'zx/globals'
const filename = './package.json'
const { version } = require(filename)
const prompts = require('prompts')

console.log(chalk.blue('Long long ago, there is a tiger.'))
console.log('Current version: ' + version)

const [major, minor, patch] = version.split('.')
const nextMajor = String(Number(major) + 1) + '.0.0'
const nextMinor = major + '.' + String(Number(minor) + 1) + '.0'
const nextPatch = major + '.' + minor + '.' + String(Number(patch) + 1)

const questions = [
  {
    type: 'text',
    name: 'dish',
    message: 'Would you like some cocktail? ',
  },
  {
    type: 'select',
    name: 'version',
    message: 'Which part do you want to bump? ',
    choices: [
      { title: 'patch: ' + nextPatch, value: nextPatch },
      { title: 'minor: ' + nextMinor, value: nextMinor },
      { title: 'major: ' + nextMajor, value: nextMajor },
    ],
  },
  {
    type: prev => prev && 'confirm',
    name: 'commit',
    message: '是否执行git commit提交代码?',
    initial: true,
  },
  {
    type: prev => prev && 'text',
    name: 'message',
    message: "git commit的内容(留空则使用'Bump version'):",
  },
  {
    type: prev => (prev || prev === '') && 'confirm',
    name: 'tag',
    message: '是否执行git tag打标签?',
    initial: true,
  },
]

const response = await prompts(questions)
const newVersion = response.version
if (newVersion) {
  const data = await fs.readFile(filename)
  const content = String(data).replace(`"version": "${version}"`, `"version": "${newVersion}"`)
  await fs.writeFile(filename, content)
  console.log(chalk.green('`package.json` updated!'))
}
if (response.commit) {
  let message = response.message
  if (message === '') {
    message = `Bump version: ${version} → ${newVersion}`
  }
  if (message) {
    // console.log(`git add . && git commit -m '${message}'`)
    await $`git add . && git commit -m ${message}`
    if (response.tag) {
      const tag = 'v' + newVersion
      // console.log(`git tag -a ${tag} -m ''`)
      await $`git tag -a ${tag} -m ''`
      await $`git push && git push --tags`
    }
  } else {
    // message === undefined的情况
    console.log(chalk.yellow('I can see the first leaf falling.'))
  }
}

需要安装依赖:

yarn add --dev zx prompts

在package.json的scripts添加自定义命令bump

{
  ...
  "scripts": {
    "bump": "zx bump.mjs",
    ...
  },
  ...
}

然后就可以愉快地在终端里使用yarn bump来升版本号和打标签了:

yarn bump

附:前期js版描述了选择prompts的原因yarn bump交互式修改package.json版本号_waketzheng的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值