setup-pre-commit
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSetup Pre-Commit Hooks
配置预提交钩子
What This Sets Up
配置内容
- Husky pre-commit hook
- lint-staged running Prettier on all staged files
- Prettier config (if missing)
- typecheck and test scripts in the pre-commit hook
- Husky 预提交钩子
- lint-staged 对所有暂存文件执行Prettier格式化
- Prettier 配置文件(若不存在则创建)
- 预提交钩子中加入typecheck和test脚本
Steps
步骤
1. Detect package manager
1. 检测包管理器
Check for (npm), (pnpm), (yarn), (bun). Use whichever is present. Default to npm if unclear.
package-lock.jsonpnpm-lock.yamlyarn.lockbun.lockb检查是否存在(npm)、(pnpm)、(yarn)或(bun)。使用已存在的包管理器,若无法确定则默认使用npm。
package-lock.jsonpnpm-lock.yamlyarn.lockbun.lockb2. Install dependencies
2. 安装依赖
Install as devDependencies:
husky lint-staged prettier作为开发依赖安装:
husky lint-staged prettier3. Initialize Husky
3. 初始化Husky
bash
npx husky initThis creates dir and adds to package.json.
.husky/prepare: "husky"bash
npx husky init此命令会创建目录,并在package.json中添加脚本。
.husky/prepare: "husky"4. Create .husky/pre-commit
.husky/pre-commit4. 创建.husky/pre-commit
文件
.husky/pre-commitWrite this file (no shebang needed for Husky v9+):
npx lint-staged
npm run typecheck
npm run testAdapt: Replace with detected package manager. If repo has no or script in package.json, omit those lines and tell the user.
npmtypechecktest写入以下内容(Husky v9+不需要添加shebang):
npx lint-staged
npm run typecheck
npm run test适配说明:将替换为检测到的包管理器。如果仓库的package.json中没有或脚本,则省略对应行并告知用户。
npmtypechecktest5. Create .lintstagedrc
.lintstagedrc5. 创建.lintstagedrc
文件
.lintstagedrcjson
{
"*": "prettier --ignore-unknown --write"
}json
{
"*": "prettier --ignore-unknown --write"
}6. Create .prettierrc
(if missing)
.prettierrc6. 创建.prettierrc
文件(若不存在)
.prettierrcOnly create if no Prettier config exists. Use these defaults:
json
{
"useTabs": false,
"tabWidth": 2,
"printWidth": 80,
"singleQuote": false,
"trailingComma": "es5",
"semi": true,
"arrowParens": "always"
}仅当Prettier配置不存在时创建,使用以下默认配置:
json
{
"useTabs": false,
"tabWidth": 2,
"printWidth": 80,
"singleQuote": false,
"trailingComma": "es5",
"semi": true,
"arrowParens": "always"
}7. Verify
7. 验证
- exists and is executable
.husky/pre-commit - exists
.lintstagedrc - script in package.json is
prepare"husky" - config exists
prettier - Run to verify it works
npx lint-staged
- 确认文件存在且可执行
.husky/pre-commit - 确认文件存在
.lintstagedrc - 确认package.json中的脚本为
prepare"husky" - 确认Prettier配置文件存在
- 执行验证功能正常
npx lint-staged
8. Commit
8. 提交代码
Stage all changed/created files and commit with message:
Add pre-commit hooks (husky + lint-staged + prettier)This will run through the new pre-commit hooks — a good smoke test that everything works.
将所有修改/创建的文件加入暂存区,并使用提交信息:
Add pre-commit hooks (husky + lint-staged + prettier)此提交会触发新配置的预提交钩子,是验证所有功能正常的良好冒烟测试。
Notes
注意事项
- Husky v9+ doesn't need shebangs in hook files
- skips files Prettier can't parse (images, etc.)
prettier --ignore-unknown - The pre-commit runs lint-staged first (fast, staged-only), then full typecheck and tests
- Husky v9+不需要在钩子文件中添加shebang
- 会跳过Prettier无法解析的文件(如图片等)
prettier --ignore-unknown - 预提交钩子会先执行lint-staged(速度快,仅处理暂存文件),再执行完整的类型检查和测试