verify-commit.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // @ts-check
  2. import pico from 'picocolors'
  3. import { readFileSync } from 'node:fs'
  4. import path from 'node:path'
  5. import { exec } from './utils.js'
  6. main()
  7. async function main() {
  8. const { stdout: branch } = await exec('git', ['branch', '--show-current'])
  9. // only verify commit message on main and minor branches
  10. if (branch !== 'main' && branch !== 'minor') {
  11. return
  12. }
  13. const msgPath = path.resolve('.git/COMMIT_EDITMSG')
  14. const msg = readFileSync(msgPath, 'utf-8').trim()
  15. const commitRE =
  16. /^(revert: )?(feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release)(\(.+\))?: .{1,50}/
  17. if (!commitRE.test(msg)) {
  18. console.log()
  19. console.error(
  20. ` ${pico.white(pico.bgRed(' ERROR '))} ${pico.red(
  21. `invalid commit message format.`,
  22. )}\n\n` +
  23. pico.red(
  24. ` Proper commit message format is required for automated changelog generation. Examples:\n\n`,
  25. ) +
  26. ` ${pico.green(`feat(compiler): add 'comments' option`)}\n` +
  27. ` ${pico.green(
  28. `fix(v-model): handle events on blur (close #28)`,
  29. )}\n\n` +
  30. pico.red(` See .github/commit-convention.md for more details.\n`),
  31. )
  32. process.exit(1)
  33. }
  34. }