54 lines
1.3 KiB
JavaScript
54 lines
1.3 KiB
JavaScript
const { execSync } = require('child_process')
|
|
const fs = require('fs')
|
|
const path = require('path')
|
|
|
|
const rootDir = path.resolve(__dirname, '..')
|
|
|
|
const run = (cmd) => {
|
|
console.log(`[release] ${cmd}`)
|
|
execSync(cmd, { stdio: 'inherit', cwd: rootDir })
|
|
}
|
|
|
|
const read = (cmd) => execSync(cmd, { cwd: rootDir, encoding: 'utf8' }).trim()
|
|
|
|
const readJson = (file) =>
|
|
JSON.parse(fs.readFileSync(path.join(rootDir, file), 'utf8'))
|
|
|
|
const ensureReleaseReady = () => {
|
|
const branch = read('git branch --show-current')
|
|
if (branch !== 'main') {
|
|
throw new Error(`release 只能在 main 分支执行,当前分支:${branch}`)
|
|
}
|
|
|
|
const status = read('git status --short')
|
|
if (status) {
|
|
throw new Error('release 前请先提交或清理当前改动,工作区必须保持干净')
|
|
}
|
|
}
|
|
|
|
const main = () => {
|
|
const type = process.argv[2] || 'patch'
|
|
|
|
ensureReleaseReady()
|
|
|
|
run(`npm version ${type} --no-git-tag-version`)
|
|
run('npm run version:sync')
|
|
|
|
const pkg = readJson('package.json')
|
|
const version = pkg.version
|
|
|
|
const filesToAdd = [
|
|
'package.json',
|
|
'package-lock.json',
|
|
'android/app/build.gradle',
|
|
]
|
|
run(`git add ${filesToAdd.join(' ')}`)
|
|
|
|
run(`git commit -m "chore: release v${version}"`)
|
|
run(`git tag v${version}`)
|
|
|
|
console.log(`[release] 完成: v${version}`)
|
|
}
|
|
|
|
main()
|