Skip to content

Commit 011e414

Browse files
author
Juliette Hoisnard
committed
add husky pre-commit hooks setup
1 parent e13aaf0 commit 011e414

File tree

9 files changed

+58
-10
lines changed

9 files changed

+58
-10
lines changed

.husky/pre-commit

Lines changed: 0 additions & 1 deletion
This file was deleted.

webapp/.husky/pre-commit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npx lint-staged

webapp/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
22

3+
4+
## Git Hooks Setup (Monorepo Configuration)
5+
6+
In this monorepo setup, `.git` is at the root of the repository while the webapp package is in a subfolder. The git hooks are automatically set up when you run `npm install`:
7+
8+
- The `postinstall` script in `package.json` runs `scripts/setup-hooks.js`
9+
- This script automatically creates `.git/hooks/pre-commit` with the command: `cd webapp && npx lint-staged`
10+
- Each colleague who clones the repo and runs `npm install` will automatically have the hooks configured
11+
312
## Getting Started
413

514
First, run the development server:

webapp/app/layout.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { Geist, Geist_Mono } from 'next/font/google'
22

3-
// import type { Metadata } from 'next'
3+
import type { Metadata } from 'next'
44
import './globals.css'
55

6-
// import { Navbar } from '@/components/Navbar'
6+
import { Navbar } from '@/components/Navbar'
77

8-
// const geistSans = Geist({
9-
// variable: '--font-geist-sans',
10-
// subsets: ['latin']
11-
// })
8+
const geistSans = Geist({
9+
variable: '--font-geist-sans',
10+
subsets: ['latin']
11+
})
1212

1313
const geistMono = Geist_Mono({
1414
variable: '--font-geist-mono',

webapp/eslint.config.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ const config = [
122122
'build/**',
123123
'next-env.d.ts',
124124
'postcss.config.mjs',
125-
'eslint.config.mjs'
125+
'eslint.config.mjs',
126+
'scripts/setup-hooks.js'
126127
]
127128
}
128129
]

webapp/package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webapp/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"build": "next build",
88
"start": "next start",
99
"lint": "eslint",
10-
"prepare": "husky"
10+
"postinstall": "node scripts/setup-hooks.js"
1111
},
1212
"dependencies": {
1313
"@radix-ui/react-accordion": "^1.2.12",

webapp/scripts/setup-hooks.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Setup git hooks for monorepo configuration
5+
* This script runs after npm install and sets up the pre-commit hook
6+
* in .git/hooks (at the root) to run lint-staged from the webapp folder
7+
*/
8+
9+
const fs = require('fs')
10+
const path = require('path')
11+
12+
// Find the .git directory (should be at repo root)
13+
let gitDir = path.resolve(__dirname, '../../.git')
14+
15+
if (!fs.existsSync(gitDir)) {
16+
console.warn('⚠️ .git directory not found. Skipping git hooks setup.')
17+
process.exit(0)
18+
}
19+
20+
const hooksDir = path.join(gitDir, 'hooks')
21+
const preCommitPath = path.join(hooksDir, 'pre-commit')
22+
23+
// Create hooks directory if it doesn't exist
24+
if (!fs.existsSync(hooksDir)) {
25+
fs.mkdirSync(hooksDir, { recursive: true })
26+
}
27+
28+
// Git hook content
29+
const hookContent = `#!/bin/sh
30+
cd webapp && npx lint-staged
31+
`
32+
33+
// Write the pre-commit hook
34+
fs.writeFileSync(preCommitPath, hookContent, { mode: 0o755 })
35+
36+
console.log('✅ Git hooks setup successfully!')
37+
console.log(` Pre-commit hook created at: ${preCommitPath}`)

webapp/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
"**/*.mts",
3232
"i18n/i18next.ts"
3333
],
34-
"exclude": ["node_modules", "postcss.config.mjs", "eslint.config.mjs"]
34+
"exclude": ["node_modules", "postcss.config.mjs", "eslint.config.mjs", "scripts/setup-hooks.js"]
3535
}

0 commit comments

Comments
 (0)