Writing Custom Git Hooks With NodeJS
Tags: javascript, git, node
Published: 2020-01-17 13:54:39

Git hooks are a useful tool, especially when working in large teams.
They can help us apply code style and linting standards to our staged files.  

In the article, we'll write a few powerful Javascript git hooks that will help us manage our codebase and have a smoother developing experience.  

###Running The Script 
We are going to run our hooks with the help of Husky 🐶.
After we [installed Husky](https://medium.com/r/?url=https%3A%2F%2Fgithub.com%2Ftypicode%2Fhusky%23install) the next thing we need to do is run our node script.
Let's add our script to the package.json scripts section, and use husky to call it:  

```json
"scripts": {  
  "hooks:pre-commit": "node ./hooks/pre-commit.js",
  "hooks:pre-push": "node ./hooks/pre-push.js"
},
"husky": {
  "pre-commit": "npm run hooks:pre-commit",
  "pre-push": "npm run hooks:pre-push"
}
```

That's pretty much it, now let's see some useful implementations of 
`pre-commit` and `pre-push` hooks.  

####Exec.js 
I created an `exec.js` helper function for my hooks scripts, that wraps `shelljs`'s `exec` function. 
The `exec` function spawns a shell then executes a given command within that shell:

{% gist 
https://gist.github.com/shaharkazaz/57e5ecaf8776911e7c5bcc9a6e01d377 %}

###Pre-Commit 📦
####1. Branch Names Convention 
Allow to create only branches that have one of the following prefixes: `feature|fix|hotfix|chore|tests|automation`

{% gist https://gist.github.com/shaharkazaz/eeeef83bfdb5a8a4545e646b5c1f66cd %}

####2. Forbidden Tokens ✋
Who hasn't forgotten to remove a `debugger`? or an `fdescribe` in a test? no more!

{% gist https://gist.github.com/shaharkazaz/6908a3f48483111296048b1a10829512 %}

###Pre-Push 🚀
####1. Auto Sync Master
We noticed that developers often forget to update their branches regularly from the remote.  

This is a simple but important hook that updates your local branch from the remote `master`.

{% gist https://gist.github.com/shaharkazaz/6d08cbfa0d40ef28006b6b2f296e92a6 %}

####2. Forbidden Branches ✋
There are brunches that we don't want their commits ending up in master
such as a `staging` branch.  

We'll make a commit in these branches that will act as a "flag" 🚩.
Before pushing to the remote we will verify that this commit isn't part of the  branch being pushed (we will obviously remove this code in the `staging` branch).

{% gist https://gist.github.com/shaharkazaz/b902ddee9b826f4b6a6dfdc66cae0233 %}

###Takeaways 
We saw some useful examples for using git hooks, and how easy you can use Husky and NodeJS to apply policies and prevent bad commits.  

Now you can customize these hooks in the best way that suits your project 🥳


---

###Have You Tried Transloco Yet? 🌐
`ng-neat` introduces **Transloco**, the internationalization (i18n) library for Angular. It allows you to define translations for your content in different languages and switch between them easily in runtime.  

It exposes a rich API to manage translations efficiently and cleanly. It provides multiple plugins that will improve your development experience.
We/I highly recommend you read more about it and [check it out](https://github.com/ngneat/transloco)!

---

##Introducing Transloco: Angular Internationalization Done Right
[![🚀 Introducing Transloco: Angular Internationalization Done Right](https://thepracticaldev.s3.amazonaws.com/i/0byy9oicu3el19il9bx2.png)](https://netbasal.com/introducing-transloco-angular-internationalization-done-right-54710337630c)

##Translation Files Validation in Angular with Transloco And Husky
[![Translation Files Validation in Angular with Transloco And Husky](https://thepracticaldev.s3.amazonaws.com/i/fijkr5zgi3dx7u0rldry.png)](https://dev.to/shaharkazaz/translation-files-validation-in-angular-with-transloco-and-husky-4oc5)

##Lazy Load Translation Files In Angular Using Transloco
[![Lazy Load Translation Files In Angular Using Transloco](https://thepracticaldev.s3.amazonaws.com/i/qrbtvmni72l4jx39rymo.jpg)](https://medium.com/@shahar.kazaz/lazy-load-translation-files-in-angular-using-transloco-2d3afed116ce)