commitlint:标准化你的 git message

一个 git message 的 lint 工具
更新于: 2022-05-19 16:03:09

安装

# Install commitlint cli and conventional config
npm install --save-dev @commitlint/{config-conventional,cli}
# For Windows:
npm install --save-dev @commitlint/config-conventional @commitlint/cli

配置

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

测试安装情况

# 支持的情况
echo "feat: random subject" | npx commitlint
# 不支持的情况
echo "add: random subject" | npx commitlint

配置文件其它格式

  • .commitlintrc
  • .commitlintrc.json
  • .commitlintrc.yaml
  • .commitlintrc.yml
  • .commitlintrc.js
  • .commitlintrc.cjs
  • .commitlintrc.ts
  • commitlint.config.js
  • commitlint.config.cjs
  • commitlint.config.ts
  • commitlint field in package.json

单词

  • conventional:依照惯例的;遵循习俗的;墨守成规的;普通平凡的;传统的;习惯的;非核的;常规的

可分享的配置

  • 来源npm包
  • 来源本地
  • 来源 @scope 的包
// commitlint.config.js
module.exports = {
  extends: ['example'], // => commitlint-config-example
};
// commitlint.config.js
module.exports = {
  extends: ['./example'], // => ./example.js
};
// commitlint.config.js
module.exports = {
  extends: ['@commitlint/config-conventional'], // => @commitlint/config-conventional
};

定义自己的 rule

开发自己的插件,定义一条自己的 rule

// commitlint.config.js
module.exports = {
  rules: {
    'hello-world-rule': [2, 'always'],
  },
  plugins: [
    {
      rules: {
        'hello-world-rule': ({subject}) => {
          const HELLO_WORLD = 'Hello World';
          return [
            subject.includes(HELLO_WORLD),
            `Your subject should contain ${HELLO_WORLD} message`,
          ];
        },
      },
    },
  ],
};
> echo "feat: random subject" | npx commitlint # fails
> echo "feat: Hello World" | npx commitlint # passes

参考