From ae2b6cc7c1169297e062c134e8b9f039a2ef939e Mon Sep 17 00:00:00 2001 From: AGWAM001 Date: Tue, 2 Jun 2026 12:49:44 +0100 Subject: [PATCH] ci: add Husky pre-commit hooks for lint and formatting --- .husky/pre-commit | 1 + .husky/pre-push | 1 + .prettierignore | 7 +++++++ .prettierrc.json | 8 ++++++++ CONTRIBUTING.md | 49 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 17 ++++++++++++++-- 6 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 .husky/pre-commit create mode 100644 .husky/pre-push create mode 100644 .prettierignore create mode 100644 .prettierrc.json diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100644 index 0000000..2312dc5 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1 @@ +npx lint-staged diff --git a/.husky/pre-push b/.husky/pre-push new file mode 100644 index 0000000..e453499 --- /dev/null +++ b/.husky/pre-push @@ -0,0 +1 @@ +npx tsc --noEmit diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..a762026 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,7 @@ +.next +out +build +node_modules +public/mockServiceWorker.js +public/sw.js +package-lock.json diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..2aa8717 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,8 @@ +{ + "semi": true, + "singleQuote": false, + "tabWidth": 2, + "trailingComma": "es5", + "printWidth": 100, + "endOfLine": "lf" +} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 231ca7d..b73d941 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,5 +1,54 @@ # Contributing to ILN Frontend +## Pre-commit Hooks (Husky) + +This project uses [Husky](https://typicode.github.io/husky/) and [lint-staged](https://github.com/lint-staged/lint-staged) to catch lint and formatting issues locally before they reach CI. + +### First-time Setup + +After cloning the repo, install dependencies to activate the hooks: + +```bash +npm install +``` + +The `prepare` script runs `husky` automatically, registering the hooks in `.husky/`. + +### What the hooks do + +| Hook | Trigger | Action | +|------|---------|--------| +| `pre-commit` | `git commit` | Runs `eslint --fix` and `prettier --write` on staged files only | +| `pre-push` | `git push` | Runs `tsc --noEmit` to catch type errors before the branch is pushed | + +Hooks are scoped to staged files via `lint-staged`, so they typically complete in well under 10 seconds. + +### Skipping hooks (not recommended) + +If you genuinely need to bypass a hook in an emergency: + +```bash +# Skip pre-commit only +git commit --no-verify -m "your message" + +# Skip pre-push only +git push --no-verify +``` + +Do not make a habit of skipping — the same checks run in CI and will block your PR. + +### Prettier configuration + +Formatting rules live in `.prettierrc.json`. Files and directories excluded from formatting are listed in `.prettierignore`. + +To format the entire codebase manually: + +```bash +npx prettier --write . +``` + +--- + ## Visual Regression Testing with Chromatic This project uses Chromatic for visual regression testing to catch unintended UI changes before they reach production. diff --git a/package.json b/package.json index 189df05..1b08bcf 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "test:e2e": "playwright test", "storybook": "storybook dev -p 6006", "build-storybook": "storybook build", - "chromatic": "chromatic --exit-zero-on-changes" + "chromatic": "chromatic --exit-zero-on-changes", + "prepare": "husky" }, "dependencies": { "@stellar/freighter-api": "^6.0.1", @@ -67,7 +68,19 @@ "eslint-plugin-storybook": "^10.4.1", "playwright": "^1.60.0", "@vitest/browser-playwright": "^4.1.5", - "@vitest/coverage-v8": "^4.1.5" + "@vitest/coverage-v8": "^4.1.5", + "husky": "9.1.7", + "lint-staged": "15.5.2", + "prettier": "3.5.3" + }, + "lint-staged": { + "*.{ts,tsx,js,jsx,mjs,cjs}": [ + "eslint --fix", + "prettier --write" + ], + "*.{json,css,md}": [ + "prettier --write" + ] }, "msw": { "workerDirectory": [