setup-pre-commit
- 作者仓库星标 0
- 作者更新于 2026年8月24日 22:19
- 作者仓库 skills
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
- 流狐分类
- 工程开发
- 作者声明 Agent
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 88 / 100 · 启发式扫描,不代表运行安全
- 作者 / 版本 / 许可
- @mattpocock · 未声明 license
- 流狐 Token 估算
- 低消耗
- 流狐接入估算
- 需简单配置
- 是否需要外部 API Key
- 未发现要求
- 检测到的系统要求
- macOS · Linux · Windows
- 底层运行要求
- Bun
- 检测到的文件与系统行为
-
- 只读
- 允许写入 / 修改
- Shell 执行
- 检测到的网络行为
- 仅限本地
- 安装命令数
- 无(仅作为资料)
档案由构建时根据 SKILL.md 与安装命令自动衍生,可能与作者实际意图存在差异。
需要注意: 未限定 allowed-tools,默认拥有全部工具权限。
作者没有在当前 SKILL.md 中定义固定输出样例。 Husky pre-commit hook lint-staged running Prettier on all staged files Prettier config (if missing)
Steps
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.
Install as devDependencies:
This creates .husky/ dir and adds prepare: "husky" to package.json.
Write this file (no shebang needed for Husky v9+): 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.
# 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
… 作者原文负责流程事实;流狐只索引当前章节、要点、文件与命令。
章节 -> What This Sets Up → Steps → 1. Detect package manager → 2. Install dependencies → 3. Initialize Husky → 4. Create .husky/pre-commit
要点 -> Husky · lint-staged · Prettier · typecheck · test · Adapt
文件/命令 -> package-lock.json · pnpm-lock.yaml · yarn.lock · bun.lockb · .husky/ · prepare: "husky" · .husky/pre-commit · npm
内容 SHA-256 -> 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 抓)。