Git Hooks Made Easy – Husky

Git hooks are custom scripts that you can drop inside your .git directory which are executed at a specified point in time. These scripts are usually executed after a certain event is occurred or about to occur. For example, we can have a script i.e. executed before commit or after the commit has happened. These scripts can be used to perform automated tasks such as executing all the test cases before pushing the changes to the git or sending an email notification to all the members when a user pushes the changes to git.

The Problem!

The git hooks are located inside the .git/hooks directory. The problem with hooks is you cannot commit and push your hooks and share it with your team members since the .git folder is not versioned.

Husky to the rescue!

To install husky in your project all you need to do is open your terminal and type following npm command.

npm install --save-dev husky

You could also use yarn to install this package.

yarn add husky --dev

Now, to add new hooks to you’re project open your package.json file.

{
  "husky": {
    "hooks": {
      "pre-commit": "npm run lint",
      "pre-push": "npm test",
      "...": "..."
    }
  }
}

In the above example, we’re using to hooks first executes linting before committing the changes and second runs all your tests before pushing the changes to the repository. If any of the npm scripts fails the git action following the script won’t be executed. You are free to replace npm run lint or npm test with your own npm scrips.

You can also run more then one script in your hook using the &&. For example npm run lint && npm test.

For more Information:

  1. https://git-scm.com/docs/githooks
  2. https://github.com/typicode/husky

Must Read: Angular Meta Service for Meta Tags

Set default node version with NVM

NVM or the Node version manager is a tiny command line application which enables you to efficiently manage multiple versions of node on your computer.

In this quick post, we’ll learn to set the default version of node using NVM.

To set the default node version use the following command. Make sure you replace 10.15.0 with your desired version.

nvm alias default 10.15.0

If you encounter an error similar to something as shown below, the version you’re trying to use might not be installed on your computer.

! WARNING: Version '10.15.0' does not exist.
default -> 10.15.0 (-> N/A)

Use the following command to install the missing node version and then try to set it as the default version.

nvm install 10.15.0

To make sure if NVM has set the desired node version as default use the following command.

node -v

If everything went well you’ll see the following output in your terminal window.

v10.15.0

Must Read: LocalStorage in JavaScript