Agnostic livereload with Entr

Pierre-Yves Aillet - Jul 5 '20 - - Dev Community

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
}
Enter fullscreen mode Exit fullscreen mode

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)
    }
}
Enter fullscreen mode Exit fullscreen mode

I would launch my tests continuously with the following command:

find *.go | entr -c -s "go test ."
Enter fullscreen mode Exit fullscreen mode

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

Run 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
Enter fullscreen mode Exit fullscreen mode

Centos, ... :

$ yum install entr
Enter fullscreen mode Exit fullscreen mode

MacOS :

$ brew install entr
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .