CI(pre-commit hooks): Enforce rules with Husky
Closed this issue ยท 2 comments
EandrewJones commented
Problem
We do not enforce any coding conventions on the project. Which, over time, leads to inconsistencies across the code base, code smells, etc.
Solution
- linting (eslint)
- formatting (prettier)
- TS rules
Enforce then prior to commit (pre-commit) via Husky.
rc10house commented
Assigned to me
EandrewJones commented
Decent blog for setting up eslint and prettier:
https://blog.logrocket.com/linting-typescript-eslint-prettier/
Example of a pre-commit
script from my previous start up:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
echo '๐๏ธ๐ท Styling, testing and building your project before committing'
# Check Prettier standards
yarn run check-format ||
(
echo "๐ Afraid to look! Your styling needs fixing. ๐
Prettier Check Failed. Run yarn run format, add changes and try commit again.";
false;
)
# Check ESLint Standards
yarn run check-lint ||
(
echo '๐ค๐ก๐ค Your failure to adhere to our standards is disappointing. Try again. ๐ค๐ก๐ค
ESLint Check Failed. Make the required changes listed above, add changes and try to commit again.'
false;
)
# Check tsconfig standards
yarn run check-types ||
(
echo 'โ๐ Upon closer inspection, your Types failed. ๐โ
Are you seriously trying to write that? Make the changes required above.'
false;
)
# If everything passes... Now we can commit
echo '๐ค๐ค๐ค๐ค... Alright... Code looks good to me... Trying to build now. ๐ค๐ค๐ค๐ค'
yarn run build ||
(
echo 'โ๐ท๐จโ Better call Bob... Because your build failed โ๐ท๐จโ
Vite build failed: View the errors above to see why.
'
false;
)
# After build, run unit tests
# Right now, runs all tests. Later scope to just unit tets, we can add e2e/integration as github actions on merge
yarn run test ||
(
echo '๐งช๐ฅ๐งช๐ฅ Uh oh! Looks like you broke something or failed to write tests ๐งช๐ฅ๐งช๐ฅ
Cypress tests failed: View the logs to see what broke and fix it before re-committing.
'
false;
)
# If everything passes... Now we can commit
echo 'โ
๐โ
๐ QuestStudio approves... Your contribution is appreciated and accepted for commit. โ
๐โ
๐'
Obviously, need to switch from yarn
--> npm
and change some of the messaging/comments.