release.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import inquirer from 'inquirer'
  2. import { execSync } from 'child_process'
  3. import fs from 'fs'
  4. // 运行命令工具
  5. const run = (cmd) => execSync(cmd, { stdio: 'inherit' })
  6. // 检查 Git 工作区是否干净
  7. const isGitClean = () => {
  8. const status = execSync('git status --porcelain').toString().trim()
  9. return status === ''
  10. }
  11. // 检查是否有符合规范的提交(feat、fix 等)
  12. const hasConventionalCommits = () => {
  13. const log = execSync('git log $(git describe --tags --abbrev=0)..HEAD --pretty=format:%s')
  14. .toString()
  15. .trim()
  16. return /^(feat|fix|docs|style|refactor|perf|test|chore)(\(.+\))?:/m.test(log)
  17. }
  18. // 1. 检查 git 工作区
  19. if (!isGitClean()) {
  20. console.error('❌ Git 工作区有未提交的改动,请先提交后再发版!')
  21. process.exit(1)
  22. }
  23. // 2. 检查是否有符合规范的提交
  24. if (!hasConventionalCommits()) {
  25. console.error('⚠️ 没有检测到符合 Angular 提交规范的提交,跳过 changelog 生成!')
  26. process.exit(1)
  27. }
  28. // 3. 询问生成 changelog 的方式
  29. const { mode } = await inquirer.prompt([
  30. {
  31. type: 'list',
  32. name: 'mode',
  33. message: '请选择生成 changelog 的方式:',
  34. choices: [
  35. { name: '只追加日志(推荐)', value: 'incremental' },
  36. { name: '全量生成日志(会覆盖原文件)', value: 'all' },
  37. ],
  38. default: 'incremental',
  39. },
  40. ])
  41. // 4. 生成 changelog
  42. if (mode === 'all') {
  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. // 5. 检查 changelog 是否真的更新
  48. const changelog = fs.readFileSync('CHANGELOG.md', 'utf8')
  49. if (!changelog.includes(new Date().getFullYear().toString())) {
  50. console.error('⚠️ changelog 没有生成新的内容,请检查提交记录!')
  51. process.exit(1)
  52. }
  53. // 6. 更新版本号(补丁版本)
  54. run('npm version patch -m "new version published: v%s"')
  55. // 7. 推送代码和 tag
  56. run('git push && git push --tags')
  57. console.log('✅ 发布完成!')