跳到正文
Skillpedia
部署与云(DevOps)310k · #122

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/mattpocock/skills/blob/main/skills/misc/setup-pre-commit/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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-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
常见问题

这个钩子会运行什么?

它会对暂存文件运行 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能