setup-pre-commit

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

Updated May 9, 2024
One-click install
npx skills add https://github.com/AceCodePt/dotfiles --skill setup-pre-commit-acecodept
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: setup-pre-commit
Source: https://github.com/AceCodePt/dotfiles/tree/main/.config/opencode/skills/setup-pre-commit
Command: npx skills add https://github.com/AceCodePt/dotfiles --skill setup-pre-commit-acecodept

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve? Setting up commit-time quality checks in a JavaScript or TypeScript repository involves wiring together Husky, lint-staged, and Prettier with the right configuration files, which is repetitive and easy to misconfigure. This Skill automates the entire setup so every commit is automatically formatted, type-checked, and tested. ## Core Features & Use Cases - Package Manager Detection: Detects npm, pnpm, yarn, or bun from lockfiles and adapts all commands accordingly. - Husky + lint-staged Setup: Installs devDependencies, initializes Husky, and creates a .husky/pre-commit hook that runs lint-staged, typecheck, and tests. - Prettier Configuration: Creates .lintstagedrc and a .prettierrc with sensible defaults only when no existing config is found. - Use Case: You just scaffolded a new Node.js project and want every commit to auto-format staged files with Prettier and block commits that fail type checking or tests, without manually writing hook scripts. ## Quick Start Set up Husky pre-commit hooks with lint-staged, Prettier, typecheck, and tests 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 npx lint-staged. Add a .lintstagedrc mapping file patterns to commands like prettier --write.

How to run Prettier only on staged files before commit?▼

Use lint-staged with a .lintstagedrc containing {"*": "prettier --ignore-unknown --write"}. The pre-commit hook runs npx lint-staged, which applies Prettier only to files staged in Git, keeping commits fast.

Does Husky v9 need a shebang in hook files?▼

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

Can I use Husky with pnpm, yarn, or bun instead of npm?▼

Yes, Husky works with any package manager. Detect the package manager from lockfiles (pnpm-lock.yaml, yarn.lock, bun.lockb) and substitute the matching command in the pre-commit hook and install steps.

What if my package.json has no typecheck or test script?▼

Omit those lines from the .husky/pre-commit file and inform the user. The hook should only reference scripts that actually exist in package.json, otherwise every commit will fail.