Skip to content

Commit 29473ad

Browse files
authored
feat: Add native Git hooks for automatic code formatting (#167)
* feat: add tmux-based development setup script - 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 #155 * docs: clarify environment setup and add documentation references - 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 * feat: add Husky pre-commit hooks for automatic code formatting - 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: #114 * chore: update Husky pre-commit hook for v10 compatibility Remove deprecated shebang and husky.sh sourcing to prepare for Husky v10 * refactor: replace Husky with native Git hooks - 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: #114 * refactor: implement automated Git hooks setup via npm postinstall - 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
1 parent 68f02cf commit 29473ad

7 files changed

Lines changed: 117 additions & 19 deletions

File tree

.githooks/pre-commit

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/sh
2+
3+
echo "Running pre-commit checks..."
4+
5+
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM)
6+
7+
if [ -z "$STAGED_FILES" ]; then
8+
exit 0
9+
fi
10+
11+
FRONTEND_FILES=$(echo "$STAGED_FILES" | grep -E '^frontend/.*\.(js|jsx|ts|tsx|json|css|scss|md)$' || true)
12+
BACKEND_FILES=$(echo "$STAGED_FILES" | grep -E '^backend/.*\.go$' || true)
13+
14+
if [ -n "$FRONTEND_FILES" ]; then
15+
echo "This runs before the commit to test frontend linting!"
16+
cd frontend
17+
npm run pre-commit
18+
if [ $? -ne 0 ]; then
19+
echo "Error: Frontend formatting failed"
20+
cd ..
21+
exit 1
22+
fi
23+
cd ..
24+
git add $FRONTEND_FILES
25+
fi
26+
27+
if [ -n "$BACKEND_FILES" ]; then
28+
if command -v gofmt >/dev/null 2>&1; then
29+
echo "Formatting backend Go files with gofmt..."
30+
echo "$BACKEND_FILES" | xargs gofmt -w
31+
if [ $? -ne 0 ]; then
32+
echo "Error: gofmt formatting failed"
33+
exit 1
34+
fi
35+
echo "$BACKEND_FILES" | xargs git add
36+
else
37+
echo "Warning: gofmt not found. Skipping Go file formatting."
38+
echo "Install Go to enable automatic Go formatting."
39+
fi
40+
fi
41+
42+
echo "Pre-commit checks completed successfully!"
43+
exit 0

.prettierignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
node_modules
2+
dist
3+
build
4+
coverage
5+
.next
6+
.nuxt
7+
.cache
8+
.vscode
9+
.idea
10+
*.min.js
11+
*.min.css
12+
package-lock.json
13+
yarn.lock
14+
pnpm-lock.yaml
15+
.husky
16+
backend/docs/swagger.json
17+
backend/docs/swagger.yaml
18+
*.log
19+
.env
20+
.env.*

CONTRIBUTING.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Scan through our [existing issues](https://github.com/its-me-abhishek/ccsync/iss
2525
1. Fork the repository.
2626

2727
- Using GitHub Desktop:
28+
2829
- [Getting started with GitHub Desktop](https://docs.github.com/en/desktop/installing-and-configuring-github-desktop/getting-started-with-github-desktop) will guide you through setting up Desktop.
2930
- Once Desktop is set up, you can use it to [fork the repo](https://docs.github.com/en/desktop/contributing-and-collaborating-using-github-desktop/cloning-and-forking-repositories-from-github-desktop)!
3031

@@ -48,8 +49,18 @@ Please follow these rules or conventions while committing any new changes:
4849
- `refactor`: refactoring production code, eg. renaming a variable
4950
- `test`: adding missing tests, refactoring tests
5051
- `chore`: updating grunt tasks, etc., no production code change
51-
- Run `npx prettier --write .` before commiting so as to adhere to the linting scheme of the project's frontend
52-
- Run `gofmt -w .` before commiting so as to adhere to the linting scheme of the project's backend
52+
53+
**Note:** This project uses Git pre-commit hooks to automatically format code before committing.
54+
55+
The hooks are set up automatically when you run `npm install` in the frontend directory. When you commit changes:
56+
57+
- Frontend files (JS/TS/JSON/CSS/MD) will be automatically formatted with Prettier
58+
- Backend Go files will be automatically formatted with gofmt (if Go is installed)
59+
60+
If you need to manually format files:
61+
62+
- Run `npm run pre-commit` for frontend formatting (from frontend directory)
63+
- Run `gofmt -w .` for backend formatting
5364

5465
### Pull Request
5566

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Alternatively, you can run each service separately:
119119

120120
- **Backend**: See [backend/README.md](backend/README.md)
121121
- **Frontend**: See [frontend/README.md](frontend/README.md)
122+
- **Full Stack**: Use `docker-compose up`
122123
- **Sync Server**: Use `docker-compose up syncserver`
123124

124125
## Testing with Postman

development/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The `setup.sh` script starts all three services (backend, frontend, and sync ser
66

77
> **Note:** The backend should ideally be run in a separate user environment (preferably root user) to avoid permission issues with Taskwarrior configuration files.
88
9+
10+
> **Git Hooks:** Pre-commit hooks are automatically configured when you run `npm install` in the frontend directory. These hooks will format your code before each commit.
11+
912
## Prerequisites
1013

1114
Before running the setup script, ensure you have the following installed:

frontend/package-lock.json

Lines changed: 33 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,10 @@
403403
"dev": "vite",
404404
"build": "tsc && vite build",
405405
"test": "jest",
406-
"coverage": "jest --coverage"
406+
"coverage": "jest --coverage",
407+
"postinstall": "git config core.hooksPath .githooks",
408+
"lint": "npx prettier --write .",
409+
"pre-commit": "npm run lint"
407410
},
408411
"keywords": [],
409412
"author": "",

0 commit comments

Comments
 (0)