release.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env node
  2. import { execSync } from 'child_process'
  3. import inquirer from 'inquirer'
  4. import fs from 'fs'
  5. function run(cmd) {
  6. execSync(cmd, { stdio: 'inherit' })
  7. }
  8. function getVersion() {
  9. return JSON.parse(fs.readFileSync('package.json', 'utf8')).version
  10. }
  11. function hasPreviousTag() {
  12. try {
  13. const tags = execSync('git tag --sort=-creatordate', { encoding: 'utf8' })
  14. .trim()
  15. .split('\n')
  16. .filter(Boolean)
  17. return tags.length > 0
  18. } catch {
  19. return false
  20. }
  21. }
  22. async function main() {
  23. console.log('🚀 开始发布流程...')
  24. // 1. 选择版本类型
  25. const { versionType } = await inquirer.prompt([
  26. {
  27. type: 'list',
  28. name: 'versionType',
  29. message: '请选择版本类型:',
  30. choices: [
  31. { name: '补丁版本 (patch)', value: 'patch' },
  32. { name: '小版本 (minor)', value: 'minor' },
  33. { name: '大版本 (major)', value: 'major' },
  34. ],
  35. },
  36. ])
  37. // 2. 更新版本号(会生成新 tag)
  38. run(`npm version ${versionType} -m "new version published: v%s"`)
  39. // 读取新版本号
  40. const version = getVersion()
  41. // 3. 生成 changelog(增量优先)
  42. if (hasPreviousTag()) {
  43. console.log('📝 检测到已有 tag,执行增量 changelog 生成...')
  44. run('npx conventional-changelog -p angular -i CHANGELOG.md -s')
  45. } else {
  46. console.log('📝 未检测到历史 tag,执行全量 changelog 生成...')
  47. run('npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0')
  48. }
  49. // 4. 自动提交 changelog
  50. run('git add CHANGELOG.md')
  51. run('git commit --amend --no-edit') // 合并到刚才的提交
  52. // 5. 推送代码 & tag
  53. run('git push --force-with-lease')
  54. run('git push --tags')
  55. // 6. 彩色提示
  56. console.log('\x1b[36m=========================\x1b[0m')
  57. console.log('\x1b[32m🎉 发布成功!\x1b[0m')
  58. console.log(`\x1b[33m📦 版本号:v${version}\x1b[0m`)
  59. console.log('\x1b[35m⚙️ 部署:\x1b[0m')
  60. console.log('\x1b[34m * 打包并部署到测试环境(单机版):npm run deploy:test\x1b[0m')
  61. console.log('\x1b[35m⚙️ 打包:\x1b[0m')
  62. console.log('\x1b[34m * 开发环境:npm run build:dev\x1b[0m')
  63. console.log('\x1b[34m * 测试环境:npm run build:test\x1b[0m')
  64. console.log('\x1b[34m * 生产环境:npm run build\x1b[0m')
  65. console.log('\x1b[35m⚙️ 同步代码:\x1b[0m')
  66. console.log('\x1b[34m * 同步dev分支代码到prod分支:bash scripts/merge_dev_to_pro.sh\x1b[0m')
  67. console.log('\x1b[36m=========================\x1b[0m')
  68. }
  69. main()