release.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. async function main() {
  12. console.log('🚀 开始发布流程...')
  13. // 1. 选择版本类型
  14. const { versionType } = await inquirer.prompt([
  15. {
  16. type: 'list',
  17. name: 'versionType',
  18. message: '请选择版本类型:',
  19. choices: [
  20. { name: '补丁版本 (patch)', value: 'patch' },
  21. { name: '小版本 (minor)', value: 'minor' },
  22. { name: '大版本 (major)', value: 'major' },
  23. ],
  24. },
  25. ])
  26. // 2. 选择日志生成模式
  27. const { changelogMode } = await inquirer.prompt([
  28. {
  29. type: 'list',
  30. name: 'changelogMode',
  31. message: '请选择 changelog 生成模式:',
  32. choices: [
  33. { name: '全量生成(会覆盖整个文件)', value: 'full' },
  34. { name: '增量生成(只追加本次更新的内容)', value: 'incremental' },
  35. ],
  36. },
  37. ])
  38. // 3. 根据模式生成 changelog
  39. console.log('📝 正在生成 changelog...')
  40. if (changelogMode === 'full') {
  41. run('npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0')
  42. } else {
  43. run('npx conventional-changelog -p angular -i CHANGELOG.md -s')
  44. }
  45. // 4. 提交 changelog
  46. run('git add CHANGELOG.md')
  47. run('git commit -m "chore: update changelog"')
  48. // 5. 更新版本号并生成 tag(会合并 changelog 提交)
  49. run(`npm version ${versionType} -m "new version published: v%s"`)
  50. const version = getVersion()
  51. // 6. 推送代码 & tag
  52. run('git push --follow-tags')
  53. // 7. 彩色提示
  54. console.log('\x1b[36m=========================\x1b[0m')
  55. console.log('\x1b[32m🎉 发布成功!\x1b[0m')
  56. console.log(`\x1b[33m📦 版本号:v${version}\x1b[0m`)
  57. console.log('\x1b[35m⚙️ 部署:\x1b[0m')
  58. console.log('\x1b[34m * 测试环境:npm run deploy:test\x1b[0m')
  59. console.log('\x1b[34m * 开发环境:npm run build:dev\x1b[0m')
  60. console.log('\x1b[34m * 生产环境:npm run build\x1b[0m')
  61. console.log('\x1b[36m=========================\x1b[0m')
  62. }
  63. main()