The-DevOps-Daily/devops-daily

feat: Add pre-commit hooks with Husky

Ouverte

#535 ouverte le 2 déc. 2025

 (0 commentaire) (0 réaction) (0 personne assignée)TypeScript (392 forks)github user discovery
enhancementgood first issuehacktoberfest

Métriques du dépôt

Stars
 (1 087 étoiles)
Métriques de merge PR
 (Merge moyen 1j 23h) (82 PRs mergées en 30 j)

Description

Description

Set up Husky and lint-staged for automatic code quality checks before commits.

Problem

  • README mentions "Husky for pre-commit hooks" but no .husky/ directory exists
  • No automated checks before committing
  • Code style issues can be committed
  • Tests not run before push
  • Potential for broken commits to reach CI/CD

Current State

  • Prettier is configured (.prettierrc)
  • ESLint is configured (eslint.config.mjs)
  • No pre-commit automation

Proposed Solution

Install Husky with these hooks:

Pre-commit:

  • Run Prettier on staged files
  • Run ESLint on staged files
  • Run TypeScript type checking

Pre-push:

  • Run test suite (pnpm test)
  • Ensure no console.log statements (optional)

Acceptance Criteria

  • Install Husky and lint-staged
  • Configure pre-commit hook
  • Configure pre-push hook
  • Run Prettier on staged files
  • Run ESLint on staged files
  • Run TypeScript checks
  • Run tests before push
  • Update README with hook documentation
  • Add setup script in package.json
  • Test hooks work for new contributors

Implementation

// package.json
{
  "scripts": {
    "prepare": "husky install"
  },
  "lint-staged": {
    "*.{ts,tsx}": [
      "prettier --write",
      "eslint --fix"
    ],
    "*.{json,md}": [
      "prettier --write"
    ]
  }
}

Files to Create/Modify

  • package.json - Add dependencies and scripts
  • .husky/pre-commit - Pre-commit hook
  • .husky/pre-push - Pre-push hook
  • README.md - Update hook documentation

Guide contributeur