From f74a758ebde78ccb1a45fbecde9b7e0ce11bd70e Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 28 Nov 2018 09:46:06 +0100 Subject: [PATCH] chore(changelog): add commit message verification --- package.json | 13 ++++++++++++- scripts/verifyCommitMsg.js | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 scripts/verifyCommitMsg.js diff --git a/package.json b/package.json index c52af1130..70364e41a 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,18 @@ "test:types": "tsc -p types/test", "docs": "vuepress dev docs", "docs:build": "vuepress build docs", - "release": "bash build/release.sh" + "release": "bash build/release.sh", + "prepublishOnly": "npm run build && conventional-changelog -p angular -r 2 -i CHANGELOG.md -s" + }, + "gitHooks": { + "pre-commit": "lint-staged", + "commit-msg": "node scripts/verifyCommitMsg.js" + }, + "lint-staged": { + "*.{js,vue}": [ + "eslint --fix", + "git add" + ] }, "devDependencies": { "babel-core": "^6.24.1", diff --git a/scripts/verifyCommitMsg.js b/scripts/verifyCommitMsg.js new file mode 100644 index 000000000..2c2f58707 --- /dev/null +++ b/scripts/verifyCommitMsg.js @@ -0,0 +1,18 @@ +const chalk = require('chalk') // eslint-disable-line +const msgPath = process.env.GIT_PARAMS +const msg = require('fs').readFileSync(msgPath, 'utf-8').trim() + +const commitRE = /^(v\d+\.\d+\.\d+(-(alpha|beta|rc.\d+))?$)|((revert: )?(feat|fix|docs|style|refactor|perf|test|workflow|ci|chore|types)(\(.+\))?: .{1,50})/ + +if (!commitRE.test(msg)) { + console.log() + console.error( + ` ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid commit message format.`)}\n\n` + + chalk.red(` Proper commit message format is required for automated changelog generation. Examples:\n\n`) + + ` ${chalk.green(`feat(compiler): add 'comments' option`)}\n` + + ` ${chalk.green(`fix(v-model): handle events on blur (close #28)`)}\n\n` + + chalk.red(` See .github/COMMIT_CONVENTION.md for more details.\n`) + + chalk.red(` You can also use ${chalk.cyan(`npm run commit`)} to interactively generate a commit message.\n`) + ) + process.exit(1) +}