Simple watcher for `elm make`

Given some context (me complaining about not being able to use incremental builds with 0.19 and Evan telling me how to hotfix it for now) I want to share my setup for an elm make watcher that I use for development of our Elm app.

#!/usr/bin/env bash

# nice colors
COLOR_OFF="\e[0m";
DIM="\e[2m";

# random filename for the lock; see below
LOCKNAME=$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 16);

function run {
  (
  flock 200; # don't let multiple `elm make` scripts run at once.

  # reset the terminal scrollback history
  # --> all the errors you see are the current ones, not stale
  clear;
  tput reset;

  echo -en "${DIM}";
  date -R;
  echo -en "${COLOR_OFF}";

  elm make src/Main.elm --output /dev/null
  # on Linux optionally prepend for better performance: sysconfcpus -n 1

  ) 200>"/var/lock/${LOCKNAME}"
}

# run the compiler when running the script...
run;

# ... and when you save files in these directories
inotifywait -mqr -e close_write --format '%w %e %f' ./src | while read DIR EVENT FILE; do
  run;
done;

I then have my tmux windows like this: vim on the left, errors on the right.

It’s a bit minimalistic but serves me well.

So, the base of it is essentially an inotifywait ... | while ...; do elm make ...; done but there are some bells and whistles: resetting the terminal scrollback history, making sure we only run elm make once to not trigger the not enough bytes 0.19 compiler crash, etc.

Hope it is of some use to somebody!

14 Likes

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