commitlintというコミットコメントを規約に沿わせるツールを知った
始め方はドキュメントより
インストール
$ npm install --save-dev @commitlint/{cli,config-conventional}
コンフィグの用意
echo "export default { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js
huskyのインストール
$ npm install --save-dev husky
huskyはGitフックを使ってtestやlintを実行を制御するもの
前にlintなどのGitフックを実行する
huskyの初期化
$ npx husky init
commilintをコミット時に実行するようにする
echo "npx --no -- commitlint --edit \$1" > .husky/commit-msg
これで準備はできたので、以下のコミットコメントでコミットしてみると
$ git commit -m "foo: this will fail" ⧗ input: foo: this will fail ✖ type must be one of [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test] [type-enum] ✖ found 1 problems, 0 warnings ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint husky - commit-msg script failed (code 1)
このように失敗する
初期設定では、コミットコメントにbuild, chore, ci, docs, feat, fix, perf, refactor, revert, style, testのいずれかがなければならないようなので、以下のようなコメントにするとコミットが成功する
$ git commit -m "chore: this will fail"
このルールは以下を見て設定する