Skip to content

Conversation

@Han5991
Copy link
Contributor

@Han5991 Han5991 commented May 27, 2025

Overview

This PR adds a git pre-commit hook to automatically run the prettier:write script before code commits. This improvement was implemented because many team members were experiencing failed GitHub Actions due to prettier check failures when submitting PRs. By automating the formatting process at commit time, we can prevent these failures and improve the development workflow.

Key Changes

  1. Installation of husky and lint-staged packages
  2. Configuration of pre-commit hook (automatic formatting before code commits)
  3. Setup of lint-staged (formatting only staged files)

Detailed Description

Installed Packages

  • husky: A tool for easily setting up Git hooks
  • lint-staged: A tool for running lint commands only on staged files

Configuration Files

  1. package.json:

    • Added prepare script: "prepare": "husky && chmod +x .husky/*"
    • This ensures that when a new developer clones the project and runs yarn install, husky is automatically installed and hook files are granted execution permissions.
  2. .husky/pre-commit:

    • Configured to run yarn lint-staged command before commits
    • This file requires execution permissions
  3. .lintstagedrc.json:

    {
      "**/*.{ts,tsx,css}": ["yarn prettier:write"]
    }

Han5991 added 3 commits May 27, 2025 09:58
Introduced lint-staged for formatting staged files and integrated Husky to run lint-staged as a pre-commit hook. Updated dependencies and scripts to support these tools, ensuring consistent code formatting and improved development workflow.
Removed unnecessary `husky install` from the prepare script to streamline the setup process. Husky initialization now aligns with the updated best practices.
Replaced Husky's pre-commit setup to only run lint-staged. Added `chmod +x` to the prepare script to ensure Husky hooks have executable permissions, preventing potential setup issues.
@rtivital
Copy link
Member

I'd rather not have precommit hooks:

  • I make up to 30 commits per day and waiting for them to run is a waste of time. Most editors have built-in plugins for prettier that formats on save.
  • They do not actually work if you commit though GitHub interface. Most of the prettier errors come from PRs made from GitHub interface.

A better option will be to run a GitHub action that modifies the source code, not sure if that's possible.

@Han5991 Han5991 marked this pull request as draft May 27, 2025 05:32
@Han5991 Han5991 closed this May 27, 2025
@Han5991 Han5991 deleted the feature/add-lint-stage branch June 22, 2025 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants