Git Hooks
We use lint-staged and Husky for our Git Hooks.
- lint-staged - Run linters against staged git files and don’t let 💩 slip into your code base!
- Husky - Husky improves your commits and more 🐶 woof!
Installation
npm install --save-dev lint-staged
npx husky-init && npm install
pre-commit
Combine ESLint, Stylelint, Prettier, and lint-staged to format code.
{
"scripts": {
"lint": "eslint .",
},
// ...
"lint-staged": {
"*.{js,ts,svelte}": "eslint --cache --fix",
"*.css": "stylelint --fix",
"*.{js,css,md,ts,svelte,css,scss,json}": "prettier --write"
}
}
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
current_branch=$(git branch --show-current)
if [ "$current_branch" = "main" ]; then
echo "Error: You cannot commit directly to main branch!"
exit 1
fi
npx lint-staged
npm run lint
pre-push
Perform TypeScript type checking, run tests with Vitest, and finally check for conflicts.
{
"scripts": {
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"test:unit:run": "vitest run",
"typecheck": "tsc --noEmit"
}
}
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npm run typecheck
npm run check
npm run test:unit:run
git fetch origin
git merge --no-commit --no-ff origin/main
git reset --hard HEAD