Closed4

Commitizenでコミットメッセージをいい感じにする 👍

Y.Y.

インストール

ローカルにインストールしてプロジェクト毎でコミットメッセージの管理もできるが、今回はグローバルにインストールする。
https://github.com/commitizen/cz-cli

インストール方法(グローバル)

npm install commitizen -g
npm install -g cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' >> ~/.czrc
exec $SHELL -l

インストール方法(ローカル)

ローカルへのインストールは下記の記事を参照
https://honmushi.com/2021/11/12/commitizen/

Y.Y.

使い方

git addの後にgit commitをする代わりに次のコマンドを実行

git cz

↓こんな感じで対話形式でコミットできる。issueに関連しているかなど他にも対話がある。

? Select the type of change that you're committing: (Use arrow keys)
❯ feat:        A new feature
  fix:         A bug fix
  improvement: An improvement to a current feature
  docs:        Documentation only changes
  style:       Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
  refactor:    A code change that neither fixes a bug nor adds a feature
  perf:        A code change that improves performance
Y.Y.

日本語化する方法

コミットメッセージは英語の方がかっこいい(?)が日本語の方がわかりやすいので日本語化する。

日本語のモジュールをインストール

npm install -g cz-conventional-changelog-ja

./czrcファイルで設定

~/.czrc
{
  "path": "cz-conventional-changelog-ja"
}
Y.Y.

自分用にカスタマイズ

初期状態ではコミットタイプが多すぎたりするので下記のようにカスタマイズできる。
下記より拝借しました 🙇
https://zenn.dev/chida/articles/96870755a855e8

~/changelog.config.js
module.exports = {
  disableEmoji: false,
  format: "{type}: {subject}",
  list: [
    "fix",
    "feat",
    "refactor",
    "test",
    "style",
    "chore",
    "docs",
    "perf",
    "ci",
  ],
  maxMessageLength: 64,
  minMessageLength: 3,
  questions: ["type", "subject"],
  scopes: [],
  types: {
    chore: {
      description: "ドキュメントの生成やビルドプロセス、ライブラリなどの変更",
      value: "chore",
    },
    ci: {
      description: "CI用の設定やスクリプトに関する変更",
      value: "ci",
    },
    docs: {
      description: "ドキュメントのみの変更",
      value: "docs",
    },
    feat: {
      description: "新機能",
      value: "feat",
    },
    fix: {
      description: "不具合の修正",
      value: "fix",
    },
    perf: {
      description: "パフォーマンス改善を行うためのコードの変更",
      value: "perf",
    },
    refactor: {
      description: "バグ修正や機能の追加を行わないコードの変更",
      value: "refactor",
    },
    style: {
      description: "コードの処理に影響しない変更(スペースや書式設定など)",
      value: "style",
    },
    test: {
      description: "テストコードの変更",
      value: "test",
    },
  },
};
このスクラップは6ヶ月前にクローズされました