跳转到内容

setup-pre-commit

  • Husky 提交前钩子
  • lint-staged 对所有暂存文件运行 Prettier
  • Prettier 配置(如果还没有)
  • 在提交前钩子中运行 typecheck 和 test 脚本

检查是否存在 package-lock.json(npm)、pnpm-lock.yaml(pnpm)、yarn.lock(yarn)或 bun.lockb(bun)。使用找到的包管理器。如果不确定,默认用 npm。

将以下包安装为开发依赖:

husky lint-staged prettier
终端窗口
npx husky init

这会创建 .husky/ 目录,并在 package.json 中添加 prepare: "husky"。

写入以下内容(Husky v9+ 不需要 shebang):

npx lint-staged
npm run typecheck
npm run test

调整:把 npm 换成检测到的包管理器。如果 package.json 中没有 typecheck 或 test 脚本,就删掉对应的行,并告诉用户。

{
"*": "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(速度快,只处理已暂存文件),然后运行完整的类型检查和测试

读者可以点名用它,模型也可以自己调用它。它不在 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.
英文 SKILL.md
---
name: setup-pre-commit
description: 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
```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

非官方学习站,与 Matt Pocock 和 AI Hero 没有隶属关系。本站不提供可安装的中文技能包。

技能原文采用 MIT 许可。Copyright (c) 2026 Matt Pocock

鲁ICP备2025189036号