Skip to content

Instantly share code, notes, and snippets.

@ottob
Forked from magnetikonline/precompresscssjswebfont.sh
Last active August 29, 2015 13:56
Show Gist options
  • Save ottob/8931327 to your computer and use it in GitHub Desktop.
Save ottob/8931327 to your computer and use it in GitHub Desktop.
#!/bin/bash
# note: this won't work too well with filenames/directories with spaces in them
function compressresource() {
gzip -c9 "$1" > "$1.gz"
touch -c -r "$1" "$1.gz"
echo "Compressed: $1 > $1.gz"
}
# fetch list of websites
for APPDIR in `find /var/www/* -maxdepth 0`
do
# fetch all existing gzipped CSS/JS/webfont files and remove files that do not have a base uncompressed file
for COMPRESSFILE in `find "$APPDIR" -type f \( -name "*.css.gz" -or -name "*.js.gz" -or -name "*.eot.gz" -or -name "*.svg.gz" -or -name "*.ttf.gz" \)`
do
if [ ! -f "`echo $COMPRESSFILE | sed -e 's/.gz$//'`" ]; then
# remove orphan gzipped file
rm "$COMPRESSFILE"
echo "Removed: $COMPRESSFILE"
fi
done
# fetch all source CSS/JS/webfont files
# gzip each file and give filestamp identical to that of the uncompressed file
for COMPRESSFILE in `find "$APPDIR" -type f \( -name "*.css" -or -name "*.js" -or -name "*.eot" -or -name "*.svg" -or -name "*.ttf" \)`
do
if [ -f "$COMPRESSFILE.gz" ]; then
# only re-gzip if base file is different in timestamp to the existing gzip file
if [ `stat -f %m -t %Y "$COMPRESSFILE"` != `stat -f %m -t %Y "$COMPRESSFILE.gz"` ]; then
compressresource "$COMPRESSFILE"
fi
else
compressresource "$COMPRESSFILE"
fi
done
done
@ottob
Copy link
Author

ottob commented Feb 11, 2014

fixed touch and stat commands to be compatible with osx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment