Skip to content

Instantly share code, notes, and snippets.

@HelloThisIsFlo
Created November 16, 2017 19:56
Show Gist options
  • Save HelloThisIsFlo/720c78d59149839fcbdfa802072ca190 to your computer and use it in GitHub Desktop.
Save HelloThisIsFlo/720c78d59149839fcbdfa802072ca190 to your computer and use it in GitHub Desktop.
Livereload - Javascript / Brunch - w/ Docker
#!/bin/bash
## Config #############
HTML_DIR=$(pwd)/public
DOCKER_CONTAINER=livereload
DOCKER_PORT=1234
TMPDIR=/tmp/livereload-script
#########################
function tmp_file {
echo "$TMPDIR/pid-$1"
}
function run_and_store_pid {
cmd=$1
echo "Running \`$cmd\`"
$cmd &
echo "$!" > $(tmp_file $cmd)
}
function kill_running_cmd {
cmd=$1
echo "Stopping \`$cmd\`"
kill $(cat $(tmp_file $cmd))
}
function start {
mkdir $TMPDIR
# Start Javascript watchers
run_and_store_pid "brunch watch"
# Serve `public` directory
docker run --rm --name $DOCKER_CONTAINER -v $HTML_DIR:/usr/share/nginx/html:ro -d -p"$DOCKER_PORT:80" nginx
}
function stop {
# Stop Javascript watchers
kill_running_cmd "brunch watch"
# Stop serving `public` directory
docker kill $DOCKER_CONTAINER
rm -rf $TMPDIR
}
if [ "$1" == "start" ]; then
echo "Starting livereload"
echo
start
echo
echo ".------------------------------------------------------------."
echo "| LIVERELOAD STARTED !! |"
echo "| |"
echo "| Visit your browser at: |"
echo "| |"
echo "| http://localhost:$DOCKER_PORT/index.html |"
echo "| |"
echo "|____________________________________________________________|"
echo
elif [ "$1" == "stop" ]; then
echo "Stopping livereload"
echo
stop
echo
echo ".------------------------------------------------------------."
echo "| LIVERELOAD STOPPED !! |"
echo "|____________________________________________________________|"
echo
else
echo "/!\ Provide a argument when running this script: \`start\` | \`stop\`"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment