我尝试使用赫斯基的pre-commit
和lint-staged
.
这些已安装:
"husky": "^5.1.3",
"lint-staged": "^10.5.4",
在 package.json 我有:
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
"start": "npm run develop",
"serve": "gatsby serve",
"clean": "gatsby clean",
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1",
"lint": "eslint --ext .js,.jsx,.ts,.tsx src --color",
"isready": "npm run format && npm run lint && npm run build"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged",
"pre-push": "npm test",
}
},
"lint-staged": {
"./src/*.{js,jsx,ts,tsx}": [
"npm run format",
"npm run lint",
"git add"
]
},
如果我跑步npm run lint
,目前我有2 problems (1 error, 1 warning)
。所以当我运行时git commit
,我不希望能够提交,对吗?但我可以继续并完成提交。
怎么了?
更新:
我将哈士奇降级为4.3.8
:
"husky": "^4.3.8",
"lint-staged": "^10.5.4",
在里面package.json
,在我的脚本中,我有:
"prettier": "prettier '**/*.{js,jsx,ts,tsx}' --write",
"lint": "eslint --ext .js,.jsx,.ts,.tsx src --color",
和:
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"src/**/*.{ts,tsx,js,jsx}": [
"npm run prettier",
"npm run lint",
"git add"
]
},
当我进行提交时,赫斯基仍然没有被解雇。怎么了?
更新 2:
我无法让赫斯基4
工作,所以我升级到版本6
:
npm install husky@6 --save-dev \
&& npx husky-init \
&& npm exec -- github:typicode/husky-4-to-6 --remove-v4-config
现在它工作正常。
我唯一不能去工作的是 lint-staged。
- 我添加了钩子
npx husky add .husky/pre-commit "lint-staged"
但后来我明白了.husky/pre-commit: line 4: lint-staged: command not found
吗?如何lint-staged
使用 Husky 版本 6?