husky
从 husky version >= 9.0 开始,已经不再支持 npx husky install 初始化,需要使用 npx husky init。
husky 官方说明
husky init(recommended)The
initcommand simplifies setting up husky in a project. It creates apre-commitscript in.husky/and updates thepreparescript inpackage.json. Modifications can be made later to suit your workflow.
安装
npx husky init
生成配置文件
echo "npx --no -- commitlint --edit \$1" > .husky/commit-msg
默认情况下,上述指令将会在 .pre-commit 文件中添加 npm run test 命令,需要手动移除。
.pre-commit 配置示例
# .pre-commit
npx lint-staged
Commitlint
Commitlint 用于规范化 git commit message 。
安装
首先,开发环境安装 Commitlint CLI 和 Conventional Commits configuration:
npm install @commitlint/cli @commitlint/config-conventional --save-dev
配置
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js
lint-staged
lint-staged 用于对暂存文件进行处理。
安装
npm i -D lint-staged
配置
官方配置参考
NextJS 配置
此配置也可以作为通用配置。
const path = require('path')
const buildEslintCommand = (filenames) =>
`next lint --fix --file ${filenames.map((f) => path.relative(process.cwd(), f)).join(' --file ')}`
module.exports = {
'*.{js,jsx,ts,tsx}': [buildEslintCommand],
"*.{js,jsx,ts,tsx,md,html,css}": "prettier --write"
}
需要安装 prettier
在 .pre-commit 中添加 lint-staged 脚本
# .pre-commit
npx lint-staged