release.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import inquirer from 'inquirer'
  2. import { execSync } from 'child_process'
  3. import fs from 'fs'
  4. import path from 'path'
  5. // changelog 文件路径
  6. const changelogPath = path.resolve('CHANGELOG.md')
  7. // 运行命令工具
  8. const run = (cmd) => execSync(cmd, { stdio: 'inherit' })
  9. // 检查 Git 工作区是否干净
  10. const isGitClean = () => {
  11. const status = execSync('git status --porcelain').toString().trim()
  12. return status === ''
  13. }
  14. // 检查是否有符合规范的提交(feat、fix 等)
  15. const hasConventionalCommits = () => {
  16. const lastTag = execSync('git describe --tags --abbrev=0', { stdio: 'pipe' }).toString().trim()
  17. const log = execSync(`git log ${lastTag}..HEAD --pretty=format:%s`, { stdio: 'pipe' })
  18. .toString()
  19. .trim()
  20. return /^(feat|fix|docs|style|refactor|perf|test|chore)(\(.+\))?:/m.test(log)
  21. }
  22. // 1. 确保 changelog 文件存在
  23. if (!fs.existsSync(changelogPath)) {
  24. console.log(`📄 检测到不存在 ${changelogPath},已自动创建空文件`)
  25. fs.writeFileSync(changelogPath, '', 'utf-8')
  26. }
  27. // 2. 检查 git 工作区
  28. if (!isGitClean()) {
  29. console.error('❌ Git 工作区有未提交的改动,请先提交后再发版!')
  30. process.exit(1)
  31. }
  32. // 3. 检查是否有符合规范的提交
  33. if (!hasConventionalCommits()) {
  34. console.error('⚠️ 没有检测到符合 Angular 提交规范的提交,跳过 changelog 生成!')
  35. process.exit(1)
  36. }
  37. // 4. 询问生成 changelog 的方式
  38. const { mode } = await inquirer.prompt([
  39. {
  40. type: 'list',
  41. name: 'mode',
  42. message: '请选择 changelog 生成方式:',
  43. choices: [
  44. { name: '只追加日志(推荐)', value: 'incremental' },
  45. { name: '全量生成日志(会覆盖原文件)', value: 'all' },
  46. ],
  47. default: 'incremental',
  48. },
  49. ])
  50. // 5. 生成 changelog
  51. if (mode === 'all') {
  52. run('npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0')
  53. } else {
  54. run('npx conventional-changelog -p angular -i CHANGELOG.md -s')
  55. }
  56. // 6. 选择版本号类型
  57. const { versionType } = await inquirer.prompt([
  58. {
  59. type: 'list',
  60. name: 'versionType',
  61. message: '请选择版本号更新类型:',
  62. choices: [
  63. { name: '补丁版本 (patch)', value: 'patch' },
  64. { name: '小版本 (minor)', value: 'minor' },
  65. { name: '大版本 (major)', value: 'major' },
  66. { name: '自定义版本', value: 'custom' },
  67. ],
  68. },
  69. ])
  70. if (versionType === 'custom') {
  71. const { customVersion } = await inquirer.prompt([
  72. {
  73. type: 'input',
  74. name: 'customVersion',
  75. message: '请输入自定义版本号(例如 1.2.3):',
  76. validate: (input) => /^\d+\.\d+\.\d+$/.test(input) || '版本号格式不正确,请输入 x.y.z',
  77. },
  78. ])
  79. run(`npm version ${customVersion} -m "new version published: v%s"`)
  80. } else {
  81. run(`npm version ${versionType} -m "new version published: v%s"`)
  82. }
  83. // 7. 推送代码和 tag
  84. run('git push && git push --tags')
  85. console.log('✅ 发布完成!')