Skip to content

Instantly share code, notes, and snippets.

@landys
Last active August 29, 2015 14:11
Show Gist options
  • Save landys/a850b9330783ed54c205 to your computer and use it in GitHub Desktop.
Save landys/a850b9330783ed54c205 to your computer and use it in GitHub Desktop.
Merge and minify css or js files with yuicompressor.
#!/bin/bash
# Wang Jinde
# Modify YUI_PATH with the path to the yuicompressor jar file
YUI_PATH="/opt/yui/yuicompressor-2.4.8.jar"
usage()
{
echo "usage: `basename "$0"` css/js outer_file source_files"
echo "I.E."
echo " ./`basename "$0"` css out.min.css a.css b.css c.css"
echo " ./`basename "$0"` js out.min.js a.js b.js c.js"
}
if [ $# -lt 3 ]; then
usage
exit 1
fi
type="$1"
out_file="$2"
temp_file="im_a_temp_file.$1"
# combine files
i=0
for file in "$@";
do
i=$(expr $i + 1)
if [ $i -lt 3 ]; then
continue
fi
if [ -f "$file" ]; then
cat "$file" >> "$temp_file"
fi
done
#java -jar "$YUI_PATH" -o "${file%%.*}-min.js" "$file"
java -jar "$YUI_PATH" --type $1 --charset utf-8 -o "$out_file" "$temp_file"
if (( $? )); then
echo "The files were not able to be minified after merged."
exit 1
fi
rm -f "$temp_file"
echo "Succeed!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment