NPM——通过commitlint来规范commit的提交信息

2024-08-15 15:32:28 浏览数 (1)

前言

commitlint: https://commitlint.js.org/ guides-local-setup: https://commitlint.js.org/#/guides-local-setup config-conventional: config-conventional

内容

安装配置

Windows PowerShell中的字符编码

代码语言:javascript复制
# Install and configure if needed
npm install --save-dev @commitlint/{cli,config-conventional}
# For Windows:
npm install --save-dev @commitlint/config-conventional @commitlint/cli

# Configure commitlint to use conventional config 
# 这里windows下有坑,最好直接创建commitlint.config.js然后复制进去 | 当然也可以创建后再调整编码和换行符
# 使用echo创建的时候默认是UTF-16LE,具体请看上面的Windows PowerShell中的字符编码
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js

husky

安装

代码语言:javascript复制
# Install Husky v6
npm install husky --save-dev
# or
yarn add husky --dev

# Activate hooks
npx husky install
# or
yarn husky install

配置

代码语言:javascript复制
npx husky add .husky/commit-msg  "npx --no -- commitlint --edit ${1}"

测试

代码语言:javascript复制
## 错误示例
PS C:UsersWangYangDocumentsProjectelectron-vite-vue> git commit -m "commitlint"
→ No staged files match any configured task.
⧗   input: commitlint
✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]

✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

husky - commit-msg hook exited with code 1 (error)


## 正确示例
PS C:UsersWangYangDocumentsProjectelectron-vite-vue> git commit -m "chore: commitlint" 
→ No staged files match any configured task.
[main ee32252] chore: commitlint
 5 files changed, 4903 insertions( ), 5629 deletions(-)
 create mode 100644 .husky/commit-msg
 create mode 100644 commitlint.config.js
 rewrite pnpm-lock.yaml (98%)

0 人点赞