verifyCommit.mjs 1006 B

1234567891011121314151617181920212223242526272829
  1. import chalk from 'chalk'
  2. import { readFileSync } from 'fs'
  3. import path from 'path'
  4. import { fileURLToPath } from 'url'
  5. const dirname = path.dirname(fileURLToPath(import.meta.url), '..')
  6. const msgPath = path.resolve(dirname, '../.git/COMMIT_EDITMSG')
  7. const msg = readFileSync(msgPath, 'utf-8').trim()
  8. const commitRE =
  9. /^(revert: )?(feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release)(\(.+\))?: .{1,50}/
  10. if (!commitRE.test(msg)) {
  11. console.log()
  12. console.error(
  13. ` ${chalk.bgRed.white(' ERROR ')} ${chalk.red(
  14. `invalid commit message format.`
  15. )}\n\n` +
  16. chalk.red(
  17. ` Proper commit message format is required for automated changelog generation. Examples:\n\n`
  18. ) +
  19. ` ${chalk.green(`feat(compiler): add 'comments' option`)}\n` +
  20. ` ${chalk.green(
  21. `fix(v-model): handle events on blur (close #28)`
  22. )}\n\n` +
  23. chalk.red(` See .github/commit-convention.md for more details.\n`)
  24. )
  25. process.exit(1)
  26. }