$npx -y skills add stevesolun/ctx --skill setup-pre-commitSet 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.
| 1 | # Setup Pre-Commit Hooks |
| 2 | |
| 3 | ## What This Sets Up |
| 4 | |
| 5 | - **Husky** pre-commit hook |
| 6 | - **lint-staged** running Prettier on all staged files |
| 7 | - **Prettier** config (if missing) |
| 8 | - **typecheck** and **test** scripts in the pre-commit hook |
| 9 | |
| 10 | ## Steps |
| 11 | |
| 12 | ### 1. Detect package manager |
| 13 | |
| 14 | 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. |
| 15 | |
| 16 | ### 2. Install dependencies |
| 17 | |
| 18 | Install as devDependencies: |
| 19 | |
| 20 | ``` |
| 21 | husky lint-staged prettier |
| 22 | ``` |
| 23 | |
| 24 | ### 3. Initialize Husky |
| 25 | |
| 26 | ```bash |
| 27 | npx husky init |
| 28 | ``` |
| 29 | |
| 30 | This creates `.husky/` dir and adds `prepare: "husky"` to package.json. |
| 31 | |
| 32 | ### 4. Create `.husky/pre-commit` |
| 33 | |
| 34 | Write this file (no shebang needed for Husky v9+): |
| 35 | |
| 36 | ``` |
| 37 | npx lint-staged |
| 38 | npm run typecheck |
| 39 | npm run test |
| 40 | ``` |
| 41 | |
| 42 | **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. |
| 43 | |
| 44 | ### 5. Create `.lintstagedrc` |
| 45 | |
| 46 | ```json |
| 47 | { |
| 48 | "*": "prettier --ignore-unknown --write" |
| 49 | } |
| 50 | ``` |
| 51 | |
| 52 | ### 6. Create `.prettierrc` (if missing) |
| 53 | |
| 54 | Only create if no Prettier config exists. Use these defaults: |
| 55 | |
| 56 | ```json |
| 57 | { |
| 58 | "useTabs": false, |
| 59 | "tabWidth": 2, |
| 60 | "printWidth": 80, |
| 61 | "singleQuote": false, |
| 62 | "trailingComma": "es5", |
| 63 | "semi": true, |
| 64 | "arrowParens": "always" |
| 65 | } |
| 66 | ``` |
| 67 | |
| 68 | ### 7. Verify |
| 69 | |
| 70 | - [ ] `.husky/pre-commit` exists and is executable |
| 71 | - [ ] `.lintstagedrc` exists |
| 72 | - [ ] `prepare` script in package.json is `"husky"` |
| 73 | - [ ] `prettier` config exists |
| 74 | - [ ] Run `npx lint-staged` to verify it works |
| 75 | |
| 76 | ### 8. Commit |
| 77 | |
| 78 | Stage all changed/created files and commit with message: `Add pre-commit hooks (husky + lint-staged + prettier)` |
| 79 | |
| 80 | This will run through the new pre-commit hooks — a good smoke test that everything works. |
| 81 | |
| 82 | ## Notes |
| 83 | |
| 84 | - Husky v9+ doesn't need shebangs in hook files |
| 85 | - `prettier --ignore-unknown` skips files Prettier can't parse (images, etc.) |
| 86 | - The pre-commit runs lint-staged first (fast, staged-only), then full typecheck and tests |