设置预提交

工程开发 社区

技能解读

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 抓)。

流狐档案 运行与权限由流狐静态检查推断,不代表作者声明或实际测试结果。
领域
工程开发
作者声明的智能体
未找到明确声明;不据此推断已兼容或已测试
静态检查
88 / 100 · 启发式扫描,不代表运行安全
运行环境证据
未执行;当前没有隔离运行测试证据
作者 / 版本 / 许可证
@mattpocock · 未声明许可证
上下文用量评级
低消耗
接入复杂程度
需简单配置
是否需要外部接口密钥
不需要
兼容的系统
macOS · Linux · Windows
运行环境要求
Bun
检测到的文件与系统行为
  • 只读
  • 允许写入 / 修改
  • 命令执行
网络行为
仅限本地
安装命令数
无(仅作为资料)

需要注意: 未声明工具权限范围;实际权限由运行环境决定。

作者说明文件(原文) SKILL.md

# 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

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。