Unlock the Power of Git Hooks with Lefthook

As developers, we know how crucial it is to maintain code quality across a large codebase with multiple contributors. With the advent of linting, code formatting, and type checking, the importance of Git Hooks has never been more pronounced. Among frontend developers, Husky is a popular choice for managing Git hooks. However, Husky has its limitations, including the lack of parallelization and excessive dependencies. This is where Lefthook comes in – a faster, simpler, and more powerful alternative to Husky.

What is Lefthook?

Lefthook is a polyglot Git Hooks manager developed using Go, making it compatible with Node.js and Ruby, as well as other stacks. Its single binary approach reduces the clutter in the node_modules directory. With Lefthook, you can execute tasks in parallel, choose which files to check, and separate local configurations.

Standout Features of Lefthook

Lefthook offers several features that set it apart from other Git Hook managers. These include:

  • Parallel Execution: Utilize parallelization to speed up task execution, making it ideal for large-scale applications.
  • File Selection: Execute commands on a limited set of files using prebuilt shorthands, glob and RegEx filters, and sub-directory options.
  • Separate Local Configs: Manage hooks differently on your local machine with separate configuration files.
  • Scripts and Runners: Create custom tasks with any programming language and select runners for these scripts.
  • Piped Execution: Ensure smooth task completion with piped execution of commands.
  • Inheritance of Configs: Extend configurations to ensure product or company-wide synchronization of commit messages, security audits, and other checks.

Implementing Lefthook in React and React Native

To demonstrate the power of Lefthook, let’s implement it in a React and React Native application. We’ll start by installing Lefthook and creating a lefthook.yml file to hold our jobs and hooks. Then, we’ll replace Husky with Lefthook and set up pre-commit, commit message, and pre-push hooks.

Pre-Commit Hook

Our pre-commit hook will run quickly and in parallel to ensure our app’s code is always fit to be merged. We’ll divide the hook into four tasks: type checking, ESLint, code spell checker, and Markdown link check.

Commit Message Hook

Next, we’ll set up a commit message hook to save us from the hassle of managing change logs and readability of commit messages. We’ll use commitlint, but you can always use another library or custom scripts with runners.

Pre-Push Hook

Finally, we’ll ensure that whenever code is pushed to our repository, it’s well tested for quality and security. Our pre-push hook will perform tasks in parallel, just like our pre-commit hook.

In Conclusion

Lefthook offers a more potent and flexible solution for managing Git Hooks compared to other libraries. With its parallelization capabilities, custom runners, and inheritance of configurations, Lefthook is an ideal choice for use in CI/CD environments. If you’re impressed with Lefthook and looking to migrate from Husky, check out our migration guide and gist to add hooks to your React and React Native apps.

Leave a Reply

Your email address will not be published. Required fields are marked *