release.js 2.2 KB

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