setup-pre-commit
它能做什么
它添加 Husky 和 lint-staged,使每次提交前都运行 Prettier 格式化、类型检查和测试。它适合希望在代码提交前就发现问题的开发者。
适用场景
- ✓你希望每次提交时都自动格式化代码的时候。
- ✓你希望提交通过之前运行类型检查和测试的时候。
- ✓你正在为某个项目设置 Husky 或 lint-staged 的时候。
技能链接
https://github.com/mattpocock/skills/blob/main/skills/misc/setup-pre-commit/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请设置提交前检查,让我的代码每次提交前都先格式化并经过测试。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体安装这些工具,创建一个提交前运行的文件,依次执行格式化、类型检查和测试;如果缺少格式化配置,它还会添加一份配置。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
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
常见问题
这个钩子会运行什么?
它会对暂存文件运行 lint-staged 和 Prettier,然后在项目有相应脚本时运行 typecheck 和 test 脚本。
它会覆盖我的 Prettier 设置吗?
不会。只有在不存在任何配置时,它才会创建 Prettier 配置。
它支持 npm、pnpm 和 yarn 吗?
支持。它会检测你的项目使用哪个包管理器,无法确定时默认使用 npm。
详情
作者的原始描述
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.
repo: mattpocock/skills
热度
- GitHub 星标
- 310k
- 排名
- #122
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …