apache/flagon-useralejs

CI(pre-commit hooks): Enforce rules with Husky

Closed this issue ยท 2 comments

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.

Assigned to me

@rc10house

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.