setup-pre-commit

Configures Husky pre-commit hooks with lint-staged, Prettier, typecheck, and tests.

Updated Sep 20, 2024
One-click install
npx skills add https://github.com/AnasIsmai1/dotfiles --skill setup-pre-commit-anasismai1
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: setup-pre-commit
Source: https://github.com/AnasIsmai1/dotfiles/tree/main/agents/.agents/skills/setup-pre-commit
Command: npx skills add https://github.com/AnasIsmai1/dotfiles --skill setup-pre-commit-anasismai1

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires husky, lint-staged, prettier.

What problem does it solve? Setting up pre-commit hooks manually involves juggling Husky initialization, lint-staged configuration, Prettier defaults, and package manager differences, which is error-prone and easy to get wrong. ## Core Features & Use Cases - Husky Hook Setup: Initializes Husky and creates a .husky/pre-commit hook that runs lint-staged, typecheck, and tests. - Package Manager Detection: Detects npm, pnpm, yarn, or bun from lockfiles and adapts all commands accordingly. - Sensible Defaults: Creates .lintstagedrc and a Prettier config only when missing, then verifies and commits the setup as a smoke test. - Use Case: You just scaffolded a new TypeScript project and want every commit automatically formatted, type-checked, and tested before it lands. ## Quick Start Set up Husky pre-commit hooks with lint-staged and Prettier in this repository.

Frequently Asked Questions about setup-pre-commit

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up Husky pre-commit hooks with lint-staged?▼

Install husky, lint-staged, and prettier as devDependencies, run npx husky init, then create a .husky/pre-commit file that runs lint-staged, typecheck, and tests. Add a .lintstagedrc mapping files to prettier --write.

How to run Prettier on staged files before commit?▼

Use lint-staged with a config like {"*": "prettier --ignore-unknown --write"} invoked from the Husky pre-commit hook. The --ignore-unknown flag skips files Prettier cannot parse, such as images.

Does Husky work with pnpm, yarn, or bun?▼

Yes, Husky works with any package manager. Detect the manager from lockfiles (pnpm-lock.yaml, yarn.lock, bun.lockb) and substitute its commands in the hook file instead of npm.

Why does my Husky hook fail if typecheck or test scripts are missing?▼

The pre-commit hook calls npm run typecheck and npm run test, which fail if those scripts are absent from package.json. Omit those lines from the hook when the repo does not define them.

Do Husky v9 hook files need a shebang line?▼

No, Husky v9 and later do not require shebang lines in hook files. The .husky/pre-commit file can contain plain commands like npx lint-staged directly.