Sometimes I just need to follow the changes to files and act upon that. There are plenty of “watchers” out there, like Guard and Watchr, but if I really don’t want to drag the bag of gems, here’s what I do…

#!/bin/zsh

typeset -a pids

tidy()
{
    for pid in $pids
    do
        kill $pid

    done
}

trap tidy INT

(fswatch -e '#' lib test web | xargs -I anything -R 1 -L 1 -t mix test)&
pids=($!)

(fswatch -0 src/coffee | xargs -0 -n 1 -I _ sh -c "cat src/coffee/* | coffee --compile --stdio > priv/static/js/application.js")&
pids+=$!

wait

The original is by Dave Thomas and was posted in an Elixir user group thread.

We define a function that will terminate our background tasks on INT signal, then start two watchers and add their PIDs to the list. Finally, we wait.

Now to the fswatch command syntax. (Mac OS X users can install this tool with brew install fswatch.) The tool is pretty handy. It watches for changes in the given file / directory and sends notifications. In the first example, we just watch for notifications in code directories and run mix test for each noticed change. In the second, we watch for changes in src/coffee and then run the CoffeeScript compiler along with concatenation of the files.

Easy, bare bones and plenty of opportunities. Explore.