setup-pre-commit
- 信任分
- 88/100
- 兼容 Agent
- 1
- 领域
- 工程开发
- 兼容 Agent
- Claude Code
- 信任分
- 88 / 100 · 社区维护
- 作者 / 版本 / 许可
- @mattpocock · 未声明 license
- 安装命令数
- 1 条
需要注意: 未限定 allowed-tools,默认拥有全部工具权限。
想读作者英文原文? ↓ 滚到正文区切换 · 在 GitHub 查看 ↗
setup-pre-commit 把仓库的 commit 前质量门一次性装好:Husky 起 pre-commit、lint-staged 跑只 staged 的 Prettier、再串 typecheck 与 test 脚本。落地后每次 git commit 都会先过 staged 文件 lint、再跑全量类型与测试。
设计思路
作者希望「质量门越早越便宜」——staged 文件用 lint-staged 限定 scope,避免每次都全仓 Prettier;类型检查与测试放 commit hook 里,把回归在合并之前抓住。Husky v9 不需要 shebang,hook 文件保持极简。
工作流
① 检包管理:扫 package-lock.json / pnpm-lock.yaml / yarn.lock / bun.lockb,没匹配默认 npm;② 装依赖:husky / lint-staged / prettier 作为 devDeps;③ npx husky init 初始化 .husky/ 与 prepare: "husky";④ 写 .husky/pre-commit 三行(npx lint-staged → <pm> run typecheck → <pm> run test),把 <pm> 替换成检测到的;package.json 没有 typecheck / test 脚本就略掉那两行并明确告诉用户;⑤ 写 .lintstagedrc:{"*": "prettier --ignore-unknown --write"};⑥ .prettierrc 缺则建(useTabs:false、tabWidth:2、printWidth:80、singleQuote:false、trailingComma:"es5"、semi:true、arrowParens:"always");⑦ 验:四件套都在、prepare 已配、npx lint-staged 跑得动;⑧ 把变更 commit,借这一次 commit 当 smoke test。
注意
prettier --ignore-unknown 跳过 Prettier 不认的文件(图片等);hook 顺序「lint-staged 先(快、限 staged)→ 全量 typecheck → 全量 test」是为了让最快的反馈先出。
适合的场景
- 新仓库首次接 commit 时质量门
- 已有部分 lint 但 commit 时不强制:本技能补齐 hook 部分
- 希望多人 / 多 agent 在 commit 阶段就看到统一格式
何时不要用
- 团队偏 server-side hook(GitHub Actions 阻塞合并):客户端 hook 多此一举
- monorepo 用 Nx / Turborepo / pnpm 自带 hook 体系:用它们的方案而不是再装 Husky
配套
ship(合并前的最终质检入口)、land-and-deploy(合并后的部署入口)、tdd / test-driven-development(让 <pm> run test 真有东西可跑)、receiving-code-review(hook 抓不到的事让 review 抓)。
Setup 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
Steps
1. Detect package manager
Check for package-lock.json (npm), pnpm-lock.yaml (pnpm), yarn.lock (yarn), bun.lockb (bun). Use whichever is present. Default to npm if unclear.
2. Install dependencies
Install as devDependencies:
husky lint-staged prettier
3. Initialize Husky
npx husky init
This creates .husky/ dir and adds prepare: "husky" to package.json.
4. Create .husky/pre-commit
Write this file (no shebang needed for Husky v9+):
npx lint-staged
npm run typecheck
npm run test
Adapt: Replace npm with detected package manager. If repo has no typecheck or test script in package.json, omit those lines and tell the user.
5. Create .lintstagedrc
{
"*": "prettier --ignore-unknown --write"
}
6. Create .prettierrc (if missing)
Only create if no Prettier config exists. Use these defaults:
{
"useTabs": false,
"tabWidth": 2,
"printWidth": 80,
"singleQuote": false,
"trailingComma": "es5",
"semi": true,
"arrowParens": "always"
}
7. Verify
-
.husky/pre-commitexists and is executable -
.lintstagedrcexists -
preparescript in package.json is"husky" -
prettierconfig exists - Run
npx lint-stagedto verify it works
8. Commit
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.
Notes
- Husky v9+ doesn't need shebangs in hook files
prettier --ignore-unknownskips files Prettier can't parse (images, etc.)- The pre-commit runs lint-staged first (fast, staged-only), then full typecheck and tests