Code quality is important for development ecosystem. Good code can improve quality of faster business turnaround, standardization, technical extendsibility &scalability and lower bug rates.
Workflow like pull request make sense. Tooling like linting and test cases make sense. Pipelines deployment checks bring values. git and version control make sense.
One often overlooked perspective is enforcing quality from the earlier stage and beginning of the code submission entry to the release and test pipeline.
Garbage in ,Garbage out
In this essence, we should give ownership to each individual coder to validate their own codes thoroughly before he / she push for others to review the code in the pull request.
That being said, we can introduce pre-commit or post-commit hooks to activate checks to warn/prevent a developer from committing subpar code. The definition of subpar really depend on the key criteria of a project.
In the most basic cases, passing all the linting and test cases are requirement criterias.
We will utilize git precommit / postcommit to trigger automated optional/mandatory checking for each time coder tries to commit for push. Any issue that appeared then , should be fixed or aware at least.
This post will show you how to do that. Let’s start!
The final code available here.
git clone --branch feature/pre-commit https://github.com/seanlon/demo-angular-app/
Install pre-commit package npm.
npm i --save pre-commit
Then, add our own action so whenever a developer commits his/her code. This script will run. Our case, we will print notification and run linting and mandatory requirement to pass linting before can commit.
"precommit-check": "echo \"start lint\" && npm run lint && exit 0"
Then, we test our command script is working.
npm run precommit-check
Then, we configure our pre-commit to hook to our previous command.
//invokes npm run precommit-check and npm run e2e ; upon commit. "pre-commit": [ "precommit-check" , "e2e" ],
Then, we do a normal git commit.
git add . && git commit -m "package.json"
You will see the script is triggered and evaluated.
Finally, if we want to make it optional for the precommit git, we can do it this way.
...... || exit 0
In cases where your project is at fixed different folder level. You may conveniently use
cd /yourpath/code && npm run lint && ....&& exit 0
Thanks. Please share and comment.