Skip to content

Instantly share code, notes, and snippets.

@jelder
Created November 27, 2012 17:16
Show Gist options
  • Save jelder/4155612 to your computer and use it in GitHub Desktop.
Save jelder/4155612 to your computer and use it in GitHub Desktop.
coffeerun - Wait for changes to a CoffeeScript file, compile it, and run it under either Mongo or Mac OS X's native JavaScript runtime.
#!/bin/bash
# Wait for changes to a CoffeeScript file, compile it, and run it under either Mongo or Mac OS X's native JavaScript runtime.
set -o nounset
set -o errtrace
set -o errexit
set -o pipefail
usage() {
cat <<END
Usage: $(basename $0) OPTIONS
-h Display this help text
-m Run under mongo (as in -m "server/mydb -u user -p pass")
-j Run under JavaScriptCore (Mac OS)
END
echo ${1:-}
exit 1
}
default_runtime="/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Resources/jsc"
while getopts "j:m:f:" OPTION ; do
case $OPTION in
m)
# OPTARG should be any other arguments to mongo, like database name.
runtime="mongo $OPTARG"
;;
j)
runtime=$default_runtime
;;
f)
file=$OPTARG
;;
h)
usage
;;
esac
done
compiled=${file%.*}.js
function mtime() {
while [[ ! -f $file ]] ; do
sleep 0.025
echo "... $file is gone ..." >&2
done
# -s is a feature of BSD stat(1). Sorry, GNU.
eval $(/usr/bin/stat -s $file)
echo $st_mtime
}
runtime=${runtime:-$default_runtime}
old_mtime=$(mtime $file)
echo -e "Watching for changes in $file; will run:\n $runtime $compiled"
while :; do
mtime=$(mtime $file)
if [[ $mtime -gt $old_mtime ]] ; then
time coffee -c $file && time $runtime $compiled || cat -n $compiled
echo -e "Watching for changes in $file; will run:\n $runtime $compiled"
fi
old_mtime=$mtime
sleep 0.25
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment