Appearance
Husky + lint-staged: reference setup
This is one of the four enforcement mechanisms you can implement in Workshop 7. Below is a working configuration. Copy and adapt.
Prereq: install Prettier
OrbitTasks does not ship Prettier yet (no prettier dep, no .prettierrc). The prettier --write step below needs both, so add them first:
bash
npm install --save-dev prettier
echo '{ "singleQuote": true, "semi": true }' > .prettierrcInstall
bash
npm install --save-dev husky lint-staged
npx husky inithusky init creates .husky/pre-commit with a default npm test invocation. Replace its contents with the version below.
.husky/pre-commit
sh
#!/usr/bin/env sh
npx lint-stagedIn your root package.json
json
{
"lint-staged": {
"*.{ts,tsx}": [
"eslint --fix",
"prettier --write"
],
"*.{json,md,yml}": [
"prettier --write"
]
}
}Test it
- Stage a file with deliberately bad formatting.
- Run
git commit. - Because
eslint --fixandprettier --writeauto-fix the file, lint-staged re-stages the fixed version and the commit succeeds, with the formatted file. (A rule violation that can't be auto-fixed is what blocks a commit.) - Show the diff: the staged file got cleaned up before it landed.
What this gives you
- Formatting consistency across the team without arguing about it in PR review.
- A failure mode developers can fix in 10 seconds (just rerun the commit), not in 10 minutes (waiting for CI).
- A clear policy boundary: "Did the pre-commit pass?" replaces "Is this style okay?"
What this doesn't give you
- It doesn't catch bypasses. A teammate with
git commit --no-verifyskips Husky entirely. CI is your backstop. - It doesn't catch issues in files the dev didn't stage. CI handles that too.
Treat Husky as the fast feedback loop, not the source of truth. CI is the source of truth.