setup-pre-commit
What it does
setup-pre-commit wires up Husky, lint-staged, and Prettier so that every commit runs formatting, a type check, and the test suite before it's allowed through. It detects your package manager, installs the three dependencies, initializes Husky, writes the pre-commit hook, and creates a Prettier config if one doesn't already exist.
The result is a repo where "I forgot to run the formatter" or "I committed something that doesn't type-check" stops being possible, not just discouraged.
When to reach for it
Type /setup-pre-commit, or ask to add pre-commit hooks, set up Husky, configure lint-staged, or add commit-time formatting, type-checking, or tests, and the agent will reach for it.
Reach for it once per repo, early, ideally before more than a couple of contributors have already built up their own local habits around when they format and test. It's a one-time setup, not something you run per feature.
What gets installed
- Husky, for the
pre-commithook itself. - lint-staged, running Prettier on staged files only, so the hook stays fast.
- A
typecheckand atestscript invocation inside the hook, if the repo already has those scripts inpackage.json. If it doesn't have one or both, the skill omits that line and tells you, rather than inventing a script that doesn't exist.
Steps, in order
- Detect the package manager from the lockfile present:
package-lock.json→ npm,pnpm-lock.yaml→ pnpm,yarn.lock→ yarn,bun.lockb→ bun. Defaults to npm if none is clearly present. - Install
husky,lint-staged, andprettieras dev dependencies. - Initialize Husky:
npx husky init, which creates.husky/and adds apreparescript topackage.json. - Write
.husky/pre-commit, running lint-staged, then typecheck, then tests, in that order, with the detected package manager's command prefix. Husky v9+ doesn't need a shebang line here. - Write
.lintstagedrc, pointing every staged file atprettier --ignore-unknown --write. - Write
.prettierrc, but only if the repo has no Prettier config already; an existing config is never overwritten. - Verify: the hook file exists and is executable,
.lintstagedrcexists,prepareis set to"husky", a Prettier config exists, andnpx lint-stagedactually runs. - Commit everything the setup created, with a message describing the hooks added. That commit runs through the brand-new hooks itself, which doubles as the smoke test that the whole chain actually works.
Common questions
What if the repo has no test script?
The test line is left out of the pre-commit hook, and you're told it was skipped, rather than the hook silently failing on every commit because npm run test doesn't exist. Add a test script later and re-run the skill (or add the line yourself) to bring it into the hook.
Will this overwrite my existing Prettier config? No. A Prettier config already present is left untouched; the default one the skill writes only shows up when there was nothing there before.
Why does the setup commit end with the hooks running on themselves? Because that's the cheapest real test of whether the hook actually works end to end, on real staged files, in the exact way every future commit will experience it. A setup that "looks right" but has never actually fired isn't verified yet.
The order is lint-staged, then typecheck, then tests. Why that order? Fast, staged-file-only formatting first, so the common case (a formatting nit) fails and gets fixed in under a second. The slower, whole-repo checks (typecheck, tests) only run once the fast one is already clean.
It's working if
.husky/pre-commitexists, is executable, and runs lint-staged, then typecheck, then tests, in that order..lintstagedrcruns Prettier on staged files.package.json'spreparescript is"husky".- A Prettier config exists, and it's the one that was already there if the repo had one.
- The setup commit itself went through the new hook rather than being pushed with
--no-verify.
Where it fits
A standalone, one-time-per-repo setup skill, closest in spirit to git-guardrails-claude-code: both exist to make a class of mistake structurally impossible rather than relying on anyone (human or agent) remembering to be careful. Neither depends on the other, and installing both is the common case, not the exception.