Husky Git Hook 工具
husky Git Hook 工具,为 git 提供一系列钩子函数,在提交前(pre-commit)、提交消息(commit-msg)等钩子触发时可以为我们执行一些脚本。
我们可以使用 husky 工具来进行代码提交前的自动格式化,以及 commit message 的校验。
提交前代码格式化
- 首先安装 husky
bash
pnpm add husky -D1
- 初始化 husky
bash
pnpm husky install1
并在 package.json 中添加如下内容
json
{
"scripts": {
//...
"prepare": "husky install"
}
}1
2
3
4
5
6
2
3
4
5
6
- 添加 git hook
bash
pnpm husky add .husky/pre-commit1
到这里之后我们还需要使用另外一个工具: lint-staged,它是对 git 暂存区文件进行 lint 检查的工具。
- 安装 lint-staged
bash
pnpm add lint-staged -D1
- 在
package.json中添加如下配置
json
{
//...
"lint-staged": {
"*.{js,ts,jsx,tsx}": [
"prettier --write",
"eslint --fix"
],
"*.vue": [
"stylelint --fix",
"prettier --write",
"eslint --fix"
],
"*.{less,css}": [
"stylelint --fix",
"prettier --write"
]
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- 在
.husky/pre-commit文件中写入以下内容
shell
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
pnpm lint-staged1
2
3
4
2
3
4
经过以上配置之后,我们就可以在每次提交之前对所有代码进行格式化,保证线上代码的规范性。
在实际中如果遇见
Use the --allow-empty option to continue, or check your task configuration这个问题。 我们可以修改pnpm lint-staged为pnpm lint-staged --allow-empty来暂时屏蔽这个问题。