tagged: inotifywait

Elm-lang: Auto refresh Elm sources when you embed in an index.html file

elm-react works wonderfully when your entire project is in elm. However, you often embed Elm into a larger application.
Your index.html will look something like this:

<div id="main"></div>
<script src="main.js"></script>
    var node = document.getElementById('main');
    var app = Elm.Main.embed(node);

And you use elm-make --output main.js to generate the javascript.
But you need to return the elm-make command on every modification of your elm sources. And that's shi... less that optimal for productivity.
But Linux/Unix/whatever can help you there with its inotifywait command. With it, you can run a command on file modifications.
Go to the directory where your elm sources are and run this:

inotifywait -e modify -m . | while read file; do elm-make Main.elm --output SOME_DIRECTORY_SOMEWHERE/main.js; done

You're telling it to wait for modify events in the current directory. And when it gets them it'll run the elm-make command.
Voila. Edit your elm code and refresh your browser. All is well in the world. Or at least your edit -> compile -> use cycle is shorter. Whatever.

elm inotifywait

Using inotifywait with a while loop in bash

Install inotify-tools and then you can wait for a file to change using:

inotifywait -m thefile.txt

The -m flag monitors the file, instead of exiting on the first event. You can use -e to wait for a particular event--see the man page.
But you probably want to do something when that happens. You can pipe the output of the command to a bash while loop:

inotifywait -m thefile.txt | while read file; do echo $file; done

file in this case will have the file name and event name, but you don't need to use it obviously.

bash bash-while inotifywait unix

Page 1 of 1