yarn bump交互式修改package.json版本号

调研了如下开源包:

1. prompts

2. inquirer

其他几个忘记名称了。。。

最终选择了prompts,理由是:在vue3项目里使用的,用了vite作为工具,vite依赖的是prompts

代码如下:

#!/usr/bin/env node
/**
 * * 自动升级版本号并打标签 *
 * 相当于修改package.json里面version字段值,然后git commit -m 'xxx',再git tag -a ${version}
 * */
const fs = require('fs')
const { execSync } = require('child_process')
const prompts = require('prompts')
console.log("I'm a teapot.")

const filename = './package.json'
const data = fs.readFileSync(filename, 'utf-8')
const config = JSON.parse(data)

const askVersion = (config, filename, data) => {
  const version = config.version
  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: 'Do you like pizza? ',
    },
    {
      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 },
      ],
    },
  ]

  ;(async () => {
    const response = await prompts(questions)
    const newVersion = response.version
    const content = data.replace(`"version": "${version}"`, `"version": "${newVersion}"`)
    console.log({ newVersion })
    fs.writeFileSync(filename, content)
    console.log('Are you crazy?')
  })()
}

askVersion(config, filename, data)

MacOS自测通过

Windows系统据说不能按预期工作,待解决@2022-01-29

Updated@2022-04-17 后来换成zx版的了,见yarn bump自动升级package.json里的版本号_waketzheng的博客-CSDN博客

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值