调研了如下开源包:
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博客