setup-pre-commit
设置提交前钩子
Section titled “设置提交前钩子”这会设置什么
Section titled “这会设置什么”- Husky 提交前钩子
- lint-staged 对所有暂存文件运行 Prettier
- Prettier 配置(如果还没有)
- 在提交前钩子中运行 typecheck 和 test 脚本
1. 检测包管理器
Section titled “1. 检测包管理器”检查是否存在 package-lock.json(npm)、pnpm-lock.yaml(pnpm)、yarn.lock(yarn)或 bun.lockb(bun)。使用找到的包管理器。如果不确定,默认用 npm。
2. 安装依赖
Section titled “2. 安装依赖”将以下包安装为开发依赖:
husky lint-staged prettier3. 初始化 Husky
Section titled “3. 初始化 Husky”npx husky init这会创建 .husky/ 目录,并在 package.json 中添加 prepare: "husky"。
4. 创建 .husky/pre-commit
Section titled “4. 创建 .husky/pre-commit”写入以下内容(Husky v9+ 不需要 shebang):
npx lint-stagednpm run typechecknpm run test调整:把 npm 换成检测到的包管理器。如果 package.json 中没有 typecheck 或 test 脚本,就删掉对应的行,并告诉用户。
5. 创建 .lintstagedrc
Section titled “5. 创建 .lintstagedrc”{ "*": "prettier --ignore-unknown --write"}6. 创建 .prettierrc(如果还没有)
Section titled “6. 创建 .prettierrc(如果还没有)”只有在不存在 Prettier 配置时才创建。使用以下默认值:
{ "useTabs": false, "tabWidth": 2, "printWidth": 80, "singleQuote": false, "trailingComma": "es5", "semi": true, "arrowParens": "always"}-
.husky/pre-commit存在且可执行 -
.lintstagedrc存在 - package.json 中的
prepare脚本是"husky" - Prettier 配置存在
- 运行
npx lint-staged验证它能正常工作
暂存所有已更改或新建的文件,并使用提交说明:Add pre-commit hooks (husky + lint-staged + prettier)
这会运行新加的提交前钩子,正好可以做一次冒烟检查。
- Husky v9+ 的钩子文件不需要 shebang
prettier --ignore-unknown会跳过 Prettier 无法解析的文件(如图片)- 提交前钩子会先运行 lint-staged(速度快,只处理已暂存文件),然后运行完整的类型检查和测试
原文链接和 commit
Section titled “原文链接和 commit”读者可以点名用它,模型也可以自己调用它。它不在 Claude Code 插件里。
配置 Husky 提交前钩子,让 lint-staged 运行 Prettier,并在提交时执行类型检查和测试。
Set up Husky pre-commit hooks with lint-staged (Prettier), type checking, and tests in the current repo. Use when user wants to add pre-commit hooks, set up Husky, configure lint-staged, or add commit-time formatting/typechecking/testing.- 仓库 mattpocock/skills
- 文件
skills/misc/setup-pre-commit/SKILL.md - 提交
d81f3a1 - 原仓库里这一则没有单独的说明页。
- 同目录文件
skills/misc/setup-pre-commit/agents/openai.yaml
英文 SKILL.md
---name: setup-pre-commitdescription: Set up Husky pre-commit hooks with lint-staged (Prettier), type checking, and tests in the current repo. Use when user wants to add pre-commit hooks, set up Husky, configure lint-staged, or add commit-time formatting/typechecking/testing.---
# 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
```bashnpx 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-stagednpm run typechecknpm 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非官方学习站,与 Matt Pocock 和 AI Hero 没有隶属关系。本站不提供可安装的中文技能包。
技能原文采用 MIT 许可。Copyright (c) 2026 Matt Pocock