Skip to content

Conversation

@Hell1213
Copy link
Contributor

@Hell1213 Hell1213 commented Nov 9, 2025

Description

This PR implements native Git pre-commit hooks to automatically format code before committing, with automated setup via npm postinstall script.

Changes made:
=> Created .githooks/pre-commit for native Git hook implementation
=> Added postinstall script in frontend/package.json to automatically configure Git hooks on npm install
=> Pre-commit hook automatically runs:

  • Prettier on staged frontend files (JS/TS/JSON/CSS/MD) via npm run lint
  • gofmt on staged backend Go files (if Go is installed)
    => Updated CONTRIBUTING.md with automated setup instructions
    => Updated development/README.md to mention automatic Git hooks setup
    => Removed scripts/setup-git-hooks.sh as hooks now configure automatically

How it works:
When contributors run npm install in the frontend directory, the postinstall script automatically configures Git to use the .githooks directory. The pre-commit hook then:

  1. Detects staged files
  2. Runs npm run pre-commitnpm run lintnpx prettier --write . for frontend files
  3. Runs gofmt for backend Go files
  4. Re-stages the formatted files

This ensures consistent code style without any manual setup or intervention.

Why this approach:
Based on maintainer feedback, this implementation:
=> Uses native Git hooks instead of Husky (avoids root package.json complexity)
=> Automatically configures on npm install (no manual setup script needed)
=> Follows the exact pattern suggested by the maintainer

Testing:

  • Tested with badly formatted frontend files - automatically formatted on commit

  • Verified hook only formats staged files

  • Confirmed hook executes before commit and re-stages formatted files

  • Gracefully handles missing gofmt (shows warning, doesn't fail)

  • Frontend tests pass (126 tests)

  • Fixes: Husky setup for running prettier automatically before committing #114

Checklist

  • Ran npm run lint (for formatting)
  • Ran gofmt -w . (for Go backend) - Note: Go not installed on test system, but hook is configured correctly
  • Ran npm test (for JS/TS testing) - All 126 tests passed
  • Added unit tests, if applicable - Not applicable for Git hooks setup
  • Verified all tests pass
  • Updated documentation, if needed

Additional Notes

For Contributors:
After pulling this PR, simply run npm install in the frontend directory. The Git hooks will be automatically configured, and your code will be formatted on every commit.

For Reviewers:
You can test the hook by:

  1. Running npm install in the frontend directory to configure the hook
  2. Creating a badly formatted file in frontend/src/
  3. Staging it with git add
  4. Committing and watching it format automatically

This implementation matches the maintainer's requested approach exactly, using native Git hooks with automated setup via npm postinstall.

- Add setup.sh script to start all services in tmux session
- Add stop.sh script for graceful shutdown
- Add comprehensive development/README.md with setup instructions
- Include prerequisite checks for Go, Node.js, npm, Docker, and tmux
- Validate environment files before starting services
- Auto-install dependencies if missing
- Update main README.md with development setup section

Fixes CCExtractor#155
- Change backend/.env to ./backend/.env to avoid confusion
- Add note about running backend in separate user environment
- Clarify sync server startup command in service URLs
- Add CCSync documentation website references in setup.sh error messages
- Add documentation link for OAuth credential setup
- Setup Husky v9.1.7 with lint-staged for pre-commit hooks
- Automatically run Prettier on staged frontend files (JS/TS/JSON/CSS/MD)
- Automatically run gofmt on staged backend Go files
- Add .prettierignore to exclude build artifacts and dependencies
- Update CONTRIBUTING.md with Husky setup information
- Add .husky/README.md for hook documentation
- Update .gitignore to exclude node_modules and package-lock.json at root

This prevents contributors from forgetting to run formatting commands
manually, ensuring consistent code style across all commits.

Fixes: CCExtractor#114
Remove deprecated shebang and husky.sh sourcing to prepare for Husky v10
@Hell1213
Copy link
Contributor Author

Hell1213 commented Nov 9, 2025

hey @its-me-abhishek .

This PR is ready for review. I've implemented Husky pre-commit hooks to automatically format code before committing, which solves issue #114.

@its-me-abhishek
Copy link
Collaborator

hi, while this looks fine, i think having another package.json will just make this project more complicated. i think we can shall either use a Githook instead of Husky, or if we continue to integrate this Husky into the package.json of frontend dir (which will not work with the root dir so thats a drawback).

@Hell1213
Copy link
Contributor Author

Hell1213 commented Nov 9, 2025

hey @its-me-abhishek ,

thanks for the feedback! You're absolutely right - adding a root package.json does add unnecessary complexity.

I'll refactor this to use native Git hooks instead of Husky. Will update the PR soon.

- Remove Husky dependency and root package.json
- Add setup-git-hooks.sh script for native Git hook installation
- Maintain automatic formatting functionality for frontend and backend
- Update CONTRIBUTING.md with new setup instructions

This addresses maintainer feedback to avoid adding complexity with
a root package.json. Contributors run ./scripts/setup-git-hooks.sh
once after cloning to enable automatic code formatting.

Fixes: CCExtractor#114
@Hell1213 Hell1213 force-pushed the feat/husky-prettier-pre-commit-hook branch from 37e7435 to 0b59350 Compare November 10, 2025 03:50
@Hell1213 Hell1213 changed the title feat: Add Husky pre-commit hooks for automatic code formatting feat: Add native Git hooks for automatic code formatting Nov 10, 2025
@Hell1213
Copy link
Contributor Author

hi, while this looks fine, i think having another package.json will just make this project more complicated. i think we can shall either use a Githook instead of Husky, or if we continue to integrate this Husky into the package.json of frontend dir (which will not work with the root dir so thats a drawback).

hey @its-me-abhishek,
I've updated the PR based on your feedback. Replaced Husky with native Git hooks to avoid the root package.json complexity.

@its-me-abhishek
Copy link
Collaborator

This looks fine but will add another caveat or layer, to setup inorder to make things work.

It'd be better to add something similar this to the githooks directory along with go fmt

#! /bin/sh 

echo "This runs before the commit to test frontend linting!"
cd frontend
npm run pre-commit

and then add these scripts to the frontends' package.json

    "postinstall": "git config core.hooksPath ../.githooks",
    "pre-commit": "npm run lint",

It'll be setup automatically, then, after the user runs npm install, hence making it an automated workflow!

- Created .githooks/pre-commit for native Git hook implementation
- Added postinstall script to automatically configure Git hooks on npm install
- Hook runs prettier for frontend files and gofmt for backend files
- Updated documentation to reflect automated setup process
- Removed manual setup script as hooks now configure automatically

Resolves maintainer feedback to avoid root package.json complexity
@Hell1213 Hell1213 force-pushed the feat/husky-prettier-pre-commit-hook branch from 3be08f2 to 237620b Compare November 11, 2025 05:24
@Hell1213
Copy link
Contributor Author

hey @its-me-abhishek ,

Updated the PR based on your feedback!

  1. Created .githooks/pre-commit with your suggested implementation
  2. Added postinstall and pre-commit scripts to frontend/package.json
  3. Hooks configure automatically on npm install - no manual setup needed
  4. Tested and working

Let me know if any adjustments are needed!

Copy link
Collaborator

@its-me-abhishek its-me-abhishek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@its-me-abhishek its-me-abhishek merged commit 29473ad into CCExtractor:main Nov 11, 2025
4 checks passed
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.

Husky setup for running prettier automatically before committing

2 participants