
Setup Pre Commit
This automates the full pre-commit setup with Husky, lint-staged, and Prettier, and wires in typecheck and test scripts if they exist in your package.json. It detects whether you're using npm, pnpm, yarn, or bun, installs the right dependencies, and commits everything with a single message to trigger the hooks as a smoke test. The lint-staged config runs Prettier on all staged files, which is fast, then fires off your full type checking and tests. It's smart enough to skip creating a Prettier config if you already have one. Good for when you're past the prototype phase and want to stop broken code from sneaking into commits without manually setting up the whole Husky dance.
- skills.sh installs
- 5.7K
- GitHub stars
- 5.7K
- last updated
- Aug 21, 2026
Install
$ npx -y skills add vinvcn/mattpocock-skills-zh-cn --skill setup-pre-commit --agent claude-codeInstalls into .claude/skills of the current project.
Files
设置 Pre-Commit Hooks
这会配置什么
- Husky pre-commit hook
- lint-staged 对所有 staged files 运行 Prettier
- Prettier config(如果缺失)
- pre-commit hook 中的 typecheck 和 test scripts
步骤
1. 检测 package manager
检查 package-lock.json (npm)、pnpm-lock.yaml (pnpm)、yarn.lock (yarn)、bun.lockb (bun)。使用存在的那个。不清楚时默认 npm。
2. 安装依赖
作为 devDependencies 安装:
husky lint-staged prettier
3. 初始化 Husky
npx husky init
这会创建 .husky/ dir,并向 package.json 添加 prepare: "husky"。
4. 创建 .husky/pre-commit
写入这个文件(Husky v9+ 不需要 shebang):
npx lint-staged
npm run typecheck
npm run test
Adapt:把 npm 替换为检测到的 package manager。如果 repo 的 package.json 没有 typecheck 或 test script,就省略对应行并告知用户。
5. 创建 .lintstagedrc
{
"*": "prettier --ignore-unknown --write"
}
6. 创建 .prettierrc(如果缺失)
只有没有 Prettier config 时才创建。使用这些 defaults:
{
"useTabs": false,
"tabWidth": 2,
"printWidth": 80,
"singleQuote": false,
"trailingComma": "es5",
"semi": true,
"arrowParens": "always"
}
7. 验证
Sponsors
Details
- Repository
- github.com/vinvcn/mattpocock-skills-zh-cn
- Categories
- Frontend Development, Git & Pull Requests, Code Review & Quality
- First seen
- Jun 3, 2026
More from vinvcn/mattpocock-skills-zh-cn
All 39- Migrate To Shoehorn5.6K
- Teach5.4K
- Grilling4.7K
- Domain Modeling4.4K
- Code Review4.4K
- Ask Matt4.4K
- Diagnosing Bugs4.4K
- Implement4.3K
- Codebase Design4.3K
- Research4.2K
- Writing Shape3.7K
- Writing Fragments3.7K
- Writing Beats3.7K
- Resolving Merge Conflicts3.5K
- To Prd2.2K
- Design An Interface2.1K
- To Issues2.1K
- Qa2.1K
- Edit Article2.1K
- Obsidian Vault2.1K
- Request Refactor Plan2.1K
- Ubiquitous Language2.1K
- Writing Great Skills2.1K
- Diagnose1.7K