avatarkrishankant singhal

Summary

The provided web content discusses the use of Husky and Git hooks to ensure code quality by automating tasks such as linting and testing before committing and pushing code to a repository.

Abstract

The web content explains how developers can maintain clean, formatted, and error-free code in JavaScript or TypeScript applications by utilizing Husky and Git hooks. It emphasizes the importance of running certain commands and scripts during the git workflow, specifically at the time of committing and pushing code to a repository. Git hooks are described as built-in features that execute scripts before or after key Git events like commit and push. The article introduces Husky as a tool that simplifies the management of these Git hooks within a project. It provides instructions on how to install Husky as a development dependency and configure it in the package.json file to run specific scripts, such as linting and testing, during the pre-commit and pre-push stages. The content also mentions that Husky can be configured using .huskyrc files and supports various Git hooks, except for server-side hooks.

Opinions

  • The author suggests that maintaining code quality is crucial when working individually or in a team of developers.
  • The use of Husky and Git hooks is presented as a solution to automate and enforce code standards and checks before code is committed or pushed.
  • The article implies that integrating these tools into the development workflow can lead to a more streamlined and reliable codebase by catching issues early in the development process.
  • It is implied that Husky is a preferred tool for managing Git hooks due to its ease of integration with Node.js projects and its ability to run custom scripts at designated points in the Git workflow.
  • The author's choice to include installation and configuration instructions indicates a belief in the practicality and immediate applicability of Husky for developers.

Husky and Git Hooks to make sure your code is clean, formatted and no-eslint error.

Whenever we writing any javascript, typescript app or working in a team of developers , We want to keep our code clean, formatted , buildable or any other action we want to happen before code is committed and pushed, husky and Git Hooks are there for help you.

With the help of Husky and Git hooks we can run certain command and scripts at time of committing, pushing source in repo.

What are Git hooks?

Git hooks are scripts that Git executes before or after events such as: commit, push, and receive. Git hooks are a built-in feature — no need to download anything. Git hooks are run locally.

There are various hooks provided by git . some of them are

So we can use these triggers and use husky to perform some task in our project.

To Install husky in your project, you have to run below npm command

npm install husky --save-dev
  Or
yarn add husky

it will add husky to devDependencies section of package.json and then we have to add husky section and specify what action we want to perform.

// package.json
"scripts": {
"lint": "run-p --print-label lint-eslint lint-markdown",
"lint-eslint": "eslint --cache --ext .js,.jsx,.ts,.tsx --format=pretty ./",
"lint-markdown": "markdownlint --ignore coverage --ignore dist --ignore examples --ignore node_modules **/*.md .**/**/*.md",

"test": "react-scripts test",
"test--coverage": "yarn run test -- --coverage",
"typecheck": "tsc --noEmit",
"validate": "run-p --print-label lint typecheck test build",
},
{
  "husky": {
    "hooks": {
      "pre-commit": "lint",
      "pre-push": "test",
      "...": "..."
      }
   }
}

Existing hooks are kept. Requires Node >= 10 and Git >= 2.13.0.

git commit -m 'Keep calm and commit'

When git commit is executed ,lint script will execute and es-lint and lint-markdown will run on your source code.

When git push is executed , test will execute .

Starting with 1.0.0, husky can be configured using .huskyrc, .huskyrc.json or .huskyrc.js file.

// .huskyrc
{
  "hooks": {
    "pre-commit": "npm test"
  }
}

Supported hooks

Husky supports all Git hooks defined here. Server-side hooks (pre-receive, update and post-receive) aren't supported.

So using husky and git hooks , We can achieve prettier, error free , buildable code.

JavaScript
Git Hooks
Husky
Eslint
Prettier
Recommended from ReadMedium