0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

commitlintとCommitizenの導入(huskyもあるよ)

Posted at

はじめに

GitCommitのコミットコメントを統一化する手法として、commitlintとCommitizenの導入が考えられる。
commitlintとCommitizenは特に問題ないが、huskyで詰まったのでメモとして残す。

使用するパッケージ

  • commitlint
    コミットメッセージが規約に沿っているかをチェックするツール。

  • Commitizen
    フォーマットされたコミットメッセージを簡単に作成できるCLIツール。

  • husky
    Gitフックを簡単に管理・設定し、自動化スクリプトを実行できるツール。

大まかな手順

  1. commitlintの導入と設定
  2. huskyの導入と初期設定
  3. Commitizenの導入と設定
  4. huskyのフック設定

commitlintの導入と設定

commitlintのインストール

npmを利用してライブラリをインストールする。(yarnでも良い)

npm install --save-dev @commitlint/cli @commitlint/config-conventional

package.jsonにも追加されている

commitlintの設定ファイルを追加

commitlint.config.js
module.exports = {extends: ['@commitlint/config-conventional']}

Cursorをなどを使用しているとJSをESモジュールに変換するように促してくるが、変換はせずこのまま(CommonJSのまま)にしておくこと

動作確認

以下のコマンドでcommitlintの動作確認を行う。 設定に失敗していればエラーメッセージが表示される。
なお、コミットしてまだプッシュしていないものがあれば、Conventional Commitsに沿っていない場合はその旨のメッセージが表示される。

npx commitlint -l

huskyの導入と設定

huskyのインストール

npmを利用してライブラリをインストールする。(yarnでも良い)

npm install husky --save-dev

huskyの初期設定

initオプションを使用して初期設定を行う。
以前はnpx husky installnpx husky add 〜を使用していたが、v9になってから非推奨のオプションになった。

npx husky init

これでGitフックの準備ができている。
以下のように.husky/pre-commitファイルにtestコマンドが登録されているため、不要であればコメントアウトしておく。

.husky/pre-commit
#npm test

Commitizenの導入と設定

Commitizenのインストール

npmを利用してライブラリをインストールする。(yarnでも良い)

npm install --save-dev commitizen

Commitizenの初期設定

以下のコマンドで設定を行う。package.jsonにも設定が追加される。

npx commitizen init cz-conventional-changelog --save-dev --save-exact

Commitizenの設定追加

package.jsonにscriptsを追加する。 ※huskyがあると思うのでその下に"commit": "cz"を追加する。

package.json
{
  
  "scripts": {
    "prepare": "husky",
    "commit": "cz"
  },
  
}

Commitizenの動作確認

npm run commitを実行してエラーがないことを確認する。
なお、ファイルがGitのステージングに無いときは↓のようなエラーになるが、これは問題ない。

% npm run commit
Debugger attached.

> commit
> cz

Debugger attached.
No files added to staging! Did you forget to run git add?
Waiting for the debugger to disconnect...
Waiting for the debugger to disconnect...

huskyのフック設定

huskyのフック設定を行い、git commitでCommitizenの実行とcommitlintを実行するようにする。

prepare-commit-msgフックの設定

prepare-commit-msgフックで、git commit時にCommitizenを実行する。
.husky/prepare-commit-msgファイルを作成し以下の内容を記述する。

.husky/prepare-commit-msg
exec < /dev/tty && git cz --hook || true

もし上記で上手くいかないようであれば↓のようにしてみる。

.husky/prepare-commit-msg
exec < /dev/tty && node_modules/.bin/cz --hook || true

commit-msgフックの設定

commit-msgフックでコミットメッセージを入力した時にcommitlintを実行する。
.husky/commit-msgファイルを作成し以下の内容を記述する。

.husky/commit-msg
npx --no -- commitlint --edit ${1}

.husky以下のファイルは↓のようになっていると思う。
スクリーンショット 2025-01-13 0.51.35.png

以上で設定完了。
変更ファイルをgit addでステージングに配置し、git commitで動作するはず。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?