DEV Community

loading...
Cover image for Agnostic livereload with Entr

Agnostic livereload with Entr

pyaillet profile image Pierre-Yves Aillet Originally published at pyaillet.gitlab.io Updated on ・2 min read

Entr overview

Today, I would like to introduce you to the project entr.
I discovered this project randomly a while ago, but I had been searching for a tool
like this for a while.

The goal of this tool is to provide a simple command which can launch
a specified command when some files are modified.
Concretely, this tool can be useful if you are searching for an alternative to
livereload, but not related to a specific language and not related to a full
blown IDE

Example

I often use it to automatically launch tests for Golang projects I am
developping.

Imagine, I wish to test the following function in the
pkg.go file:

// Inc returns the op1 integer incremented by one unit
func Inc(op1 int) int {
    return op1 + 1
}

I wish to test it, with this test in pkg_test.go:

func Test_Inc(t *testing.T) {
    actual := Inc(1)
    if actual != 2 {
        t.Errorf("Expecting: <2>, got <%d>", actual)
    }
}

I would launch my tests continuously with the following command:

find *.go | entr -c -s "go test ."

Here is a live preview of the result (using asciinema):

At first, the test is not working, but as soon as the test file is updated, the
test is launched again.
The specificity here, is to use the -c flag, in order to clear the screen
before launching the new command.

You can find the full list of options here

How does it work ?

In order to avoid polling,
entr uses file system events
(kqueue with BSD,
inotify with Linux).

The full source code is available here:

GitHub logo eradman / entr

A utility for running arbitrary commands when files change

How to get it ?

If you wish to install it, it's really easy, just use your Linux distribution package:

Ubuntu, Debian, Mint, ... :

$ apt install entr

Centos, ... :

$ yum install entr

MacOS :

$ brew install entr

Discussion (0)

pic
Editor guide