# What is the preferred way to add a pre-commit hook to re-frame project?

**URL:** https://clojureverse.org/t/what-is-the-preferred-way-to-add-a-pre-commit-hook-to-re-frame-project/5305
**Category:** How to?
**Tags:** clojure, clojurescript
**Created:** [January 1, 2020, 10:22am UTC](https://clojureverse.org/t/what-is-the-preferred-way-to-add-a-pre-commit-hook-to-re-frame-project/5305 "2020-01-01T10:22:56Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![terbiy](https://clojureverse.org/user_avatar/clojureverse.org/terbiy/32/2606_2.png) [@terbiy](https://clojureverse.org/u/terbiy)
#### Post date: [January 1, 2020, 10:22am UTC](https://clojureverse.org/t/what-is-the-preferred-way-to-add-a-pre-commit-hook-to-re-frame-project/5305/1 "2020-01-01T10:22:56Z")

</div>

Hello!

I am new to Clojure, and mostly I am familiar with the JS ecosystem. In my projects, I find [Prettier](https://prettier.io/) very convenient. When there are several developers, it is convenient to introduce pre-commit hook with `Prettier`, linters, testing and else. I’ve discovered `zprint` as an alternative to `Prettier` in the Clojure world in the following thread: [How to best integrate zprint as pre-commit hook?](https://v2.clojureverse.org/t/how-to-best-integrate-zprint-as-pre-commit-hook/3045). Unfortunately, there is no answer to its central question: how to introduce a pre-commit hook?

There is always an option to introduce `.git/hooks/pre-commit`. Still, in projects, I use [lint-staged](https://github.com/okonet/lint-staged) combined with [husky](https://github.com/typicode/husky), it allows me to work only with staged files. But this is a JavaScript way, what is a Clojure way or if there is one, a `re-frame` way?

---

<div class="post-metadata">

### Author: ![didibus](https://clojureverse.org/user_avatar/clojureverse.org/didibus/32/951_2.png) [@didibus](https://clojureverse.org/u/didibus)
#### Post date: [January 1, 2020, 6:46pm UTC](https://clojureverse.org/t/what-is-the-preferred-way-to-add-a-pre-commit-hook-to-re-frame-project/5305/2 "2020-01-01T18:46:39Z")

</div>

You could just use these tools and set them up to run zprint or cljfmt.

But I don’t see what these tools help with to be honest. Git hooks are pretty easy. Just edit the pre-commit shell script sample to call zprint or do whatever you want. If you wanted to go all Clojure, you could make the script a joker or babashka script. Heck, even go for ClojureScript or Clojure, but those will both add some start time latency to the hook.

---

<div class="post-metadata">

### Author: ![terbiy](https://clojureverse.org/user_avatar/clojureverse.org/terbiy/32/2606_2.png) [@terbiy](https://clojureverse.org/u/terbiy)
#### Post date: [January 2, 2020, 5:48am UTC](https://clojureverse.org/t/what-is-the-preferred-way-to-add-a-pre-commit-hook-to-re-frame-project/5305/3 "2020-01-02T05:48:52Z")

</div>

Thank you @didibus! I’ll, therefore, use familiar tools. Yes, pre-commit hooks are not the most complicated topic that I’ve encountered, but additional tooling helps to act easily only on the changed files or on partially added ones.

---

<div class="post-metadata">

### Author: ![terbiy](https://clojureverse.org/user_avatar/clojureverse.org/terbiy/32/2606_2.png) [@terbiy](https://clojureverse.org/u/terbiy)
#### Post date: [January 11, 2020, 3:49pm UTC](https://clojureverse.org/t/what-is-the-preferred-way-to-add-a-pre-commit-hook-to-re-frame-project/5305/4 "2020-01-11T15:49:46Z")

</div>

After some time spent, I have a pre-commit configuration that works fine for me. I decided to use `husky` with `lint-staged`, which I configure in `package.json`.  
In `project.clj` I’ve added the following to install `zprint` and use it from `lein`:

```clojure
(defproject my-project "0.1.0-SNAPSHOT"
  ; Use zprint.
  :dependencies [
                 ; ...
                 [zprint "0.5.3"]]

  ; Make it run by CLI command: lein zprint.
  :plugins [
            ; ...
            [lein-zprint "0.5.3"]]

  ; Configure zprint.
  :zprint {:old? false :style :respect-nl}
  ; ...
)

```

Also, I’ve added some configuration to prevent `zprint` from creating new files and to keeping the additional linebreak.

Unfortunately `clj-kondo` is not available as `lein` plugin, but I can use it as Node.js package. Here is my `package.json` configuration:

```auto
{
  "devDependencies": {
    "husky": "^3.1.0",
    "lint-staged": "^10.0.0-beta.14",
    "clj-kondo": "^2019.11.23"
  },
  "scripts": {
    "format": "lein zprint",
    "lint": "clj-kondo --lint"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "*.{clj,cljs}": [
      "npm run format",
      "npm run lint"
    ]
  }
}

```

---

<div class="post-metadata">

### Author: ![didibus](https://clojureverse.org/user_avatar/clojureverse.org/didibus/32/951_2.png) [@didibus](https://clojureverse.org/u/didibus)
#### Post date: [January 11, 2020, 6:49pm UTC](https://clojureverse.org/t/what-is-the-preferred-way-to-add-a-pre-commit-hook-to-re-frame-project/5305/5 "2020-01-11T18:49:32Z")

</div>

Both zprint and clj-kondo are availaible as a native CLI executable as well. Did you try using those instead of their lein or Node counterpart? The benefit is that they’d run much faster, the downside is that if you have members developing on different platform (win, mac, linux), you need a way to select the correct binary.

---

<div class="post-metadata">

### Author: ![terbiy](https://clojureverse.org/user_avatar/clojureverse.org/terbiy/32/2606_2.png) [@terbiy](https://clojureverse.org/u/terbiy)
#### Post date: [January 12, 2020, 7:21am UTC](https://clojureverse.org/t/what-is-the-preferred-way-to-add-a-pre-commit-hook-to-re-frame-project/5305/6 "2020-01-12T07:21:06Z")

</div>

Do they imply pre-installation on developers’ machines or they can be installed like the project development dependencies on initial installation of the dependencies?

---

<div class="post-metadata">

### Author: ![teodorlu](https://clojureverse.org/user_avatar/clojureverse.org/teodorlu/32/2896_2.png) [@teodorlu](https://clojureverse.org/u/teodorlu)
#### Post date: [January 12, 2020, 9:37am UTC](https://clojureverse.org/t/what-is-the-preferred-way-to-add-a-pre-commit-hook-to-re-frame-project/5305/7 "2020-01-12T09:37:17Z")

</div>

From a quick look at clj-kondo’s docs, you should be able to run on any machine with Docker:

> <https://github.com/borkdude/clj-kondo/blob/master/doc/docker.md>

---

<div class="post-metadata">

### Author: ![didibus](https://clojureverse.org/user_avatar/clojureverse.org/didibus/32/951_2.png) [@didibus](https://clojureverse.org/u/didibus)
#### Post date: [January 12, 2020, 10:20am UTC](https://clojureverse.org/t/what-is-the-preferred-way-to-add-a-pre-commit-hook-to-re-frame-project/5305/8 "2020-01-12T10:20:10Z")

</div>

There’s no install needed, it’s a self contained binary. But the binary has to find its way on their machine somehow, either through a download, or a script. I guess you could consider that an install. Lein can’t pull it down for you.

You can find download links here: [https://github.com/borkdude/clj-kondo/releases/tag/v2020.01.10](https://github.com/borkdude/clj-kondo/releases/tag/v2020.01.10)  
And here: [https://github.com/kkinnear/zprint/releases/tag/0.5.3](https://github.com/kkinnear/zprint/releases/tag/0.5.3)

Though it looks like zprint doesn’t have a windows binary.

---

<div class="post-metadata">

### Author: ![system](https://clojureverse.org/uploads/default/original/2X/5/51079bf9e4b7d9466242c06cf1e43b9f8bd6da14.png) [@system](https://clojureverse.org/u/system)
#### Post date: [July 12, 2020, 10:20pm UTC](https://clojureverse.org/t/what-is-the-preferred-way-to-add-a-pre-commit-hook-to-re-frame-project/5305/9 "2020-07-12T22:20:11Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
