设置预提交
- 作者仓库星标 0
- 作者更新于 2026年8月24日 14:19
- 作者仓库 skills
- 领域
- 工程开发
- 作者声明的智能体
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 88 / 100 · 启发式扫描,不代表运行安全
- 运行环境证据
- 未执行;当前没有隔离运行测试证据
- 作者 / 版本 / 许可证
- @mattpocock · 未声明许可证
- 上下文用量评级
- 低消耗
- 接入复杂程度
- 需简单配置
- 是否需要外部接口密钥
- 不需要
- 兼容的系统
- macOS · Linux · Windows
- 运行环境要求
- Bun
- 检测到的文件与系统行为
-
- 只读
- 允许写入 / 修改
- 命令执行
- 网络行为
- 仅限本地
- 安装命令数
- 无(仅作为资料)
需要注意: 未声明工具权限范围;实际权限由运行环境决定。
# 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
```bash
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`
```json
{
"*": "prettier --ignore-unknown --write"
}
```
### 6. Create `.prettierrc` (if missing)
Only create if no Prettier config exists. Use these defaults:
```json
{
"useTabs": false,
"tabWidth": 2,
"printWidth": 80,
"singleQuote": false,
"trailingComma": "es5",
"semi": true,
"arrowParens": "always"
}
```
### 7. Verify
- [ ] `.husky/pre-commit` exists and is executable
- [ ] `.lintstagedrc` exists
- [ ] `prepare` script in package.json is `"husky"`
- [ ] `prettier` config exists
- [ ] Run `npx lint-staged` to 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-unknown` skips files Prettier can't parse (images, etc.)
- The pre-commit runs lint-staged first (fast, staged-only), then full typecheck and tests
章节与文件索引
按说明文件中的顺序排列。
这设置了什么
1.检测包管理器
2.安装依赖
3.初始化Husky
4. 创建.husky/预提交
文件/命令 -> package-lock.json · pnpm-lock.yaml · yarn.lock · bun.lockb · .husky/ · prepare: "husky" · .husky/pre-commit · npm
内容校验值 -> e8443d215670
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」是为了让最快的反馈先出。适合的场景
不适合
配套
ship(合并前的最终质检入口)、land-and-deploy(合并后的部署入口)、tdd/test-driven-development(让<pm> run test真有东西可跑)、receiving-code-review(hook 抓不到的事让 review 抓)。