Skip to content

Instantly share code, notes, and snippets.

@Llbe
Last active May 1, 2019 15:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Llbe/3eed046d7737654887dc7870de81de03 to your computer and use it in GitHub Desktop.
Naive Sass watcher in Bash
#!/bin/bash
# Set target dir
DIR=foo/bar
# Configure sass
SASS="sass --no-source-map --style=compressed"
function removeBOM ()
{
sed -i -e '1s/^\xEF\xBB\xBF//' $@
}
function post_compile ()
{
if [ ! -e $1 ]; then
# Sass removes the files if an error occurred, so we create it again
# in order to re-attempt compilation
touch $1
fi
removeBOM $1
}
function compile ()
{
SCSS=$1
CSS=$(echo $SCSS | sed -e 's/\.scss/.css/g')
# SCSS files with no CSS sibling file are "import only" SCSS files
if [ -f $CSS ]; then
echo "Writing $CSS"
( $SASS $SCSS $CSS ; post_compile $CSS ) &
fi
}
function compile_dependants ()
{
BASENAME=$(basename $path | sed -e 's/\.scss//g')
# Compile any file that imports this file (recursively)
find $DIR -name '*.scss' -exec grep -l -z -P -o '@import[^;]*'"'$BASENAME'" {} \+ | while read path; do
compile $path
compile_dependants $path
done
}
inotifywait -q --format "%w%f" -e "close_write,create" -m -r $DIR \
| grep --line-buffered '\.scss$' \
| while read path; do
if [ -e "$path" ] ; then
compile $path
compile_dependants $path
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment