Created
September 9, 2017 04:46
-
-
Save frosit/fc95d25bc7cdd7aeb8e7ccc7255597c1 to your computer and use it in GitHub Desktop.
Small wrapper for JSMIN/CSSMIN and Convert (PNG)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# == Minify JS/CSS/PNG Files (requires dependency's) == | |
# =================== POC ============================= | |
# == Variables | |
BINDIR=$(dirname $0) | |
PROJECTDIR=$(realpath "${BINDIR}/..") | |
export Red='\033[0;31m' # Red | |
export Green='\033[0;32m' # Green | |
export Yellow='\033[0;33m' # Yellow | |
export Blue='\033[0;34m' # Blue | |
export NC="\033[0m" # Color Reset | |
JSMIN="${HOME}/node_modules/jsmin/bin/jsmin" | |
CSSMIN="${HOME}/node_modules/cssmin/bin/cssmin" | |
CONV=$(which convert) | |
VERSION="1.0.0" | |
SCRIPT=${0##*/} | |
USAGE="\ | |
${Blue}Minifier${NC} ${Green}(v${VERSION})${NC} | |
* ${Blue}Usage:${NC} ${Green}${SCRIPT}${NC} [target] --[opts] | |
Automatically detects folder/ file and type of file (css,js,png) | |
Then minifies it, them. | |
Requires node_modules in home directory with cssmin and jsmin (and convert binary) | |
${Blue}Commands:${NC} | |
${Yellow}minify${NC} minify | |
${Blue}Optional arguments:${NC} | |
${Yellow}-h, --help${NC} Show this help menu | |
${Yellow}-d, --dry-run${NC} Do a test run without saving | |
" | |
# Help function | |
help(){ | |
echo -e "$USAGE" | |
exit | |
} | |
# Shutdown | |
shutdown(){ | |
echo -e "${Green}${SCRIPT} Finished...${NC}" | |
exit | |
} | |
# Check dependencies | |
if [ ! -f "${JSMIN}" ]; then | |
echo -e "JSMin not found" | |
exit; | |
fi | |
if [ ! -f "${CSSMIN}" ]; then | |
echo -e "CSSMIN not found" | |
exit; | |
fi | |
## Call help if no arg | |
if [ $# -eq 0 ]; then | |
help | |
fi | |
### Set arg as action | |
arg=$1;shift | |
## Takes params from input | |
function fetchParams(){ | |
### Try to fetch params | |
while [[ $# -gt 0 ]] | |
do | |
param=${1} | |
# echo -e "Param = ${param}" | |
case ${param} in | |
-l|--list) | |
LIST=true | |
shift | |
;; | |
-d|--dry-run) | |
DRYRUN=true | |
shift | |
;; | |
-u|--update) | |
UPDATE=true | |
echo -e "* ${Yellow}Enabled:${NC} auto-update" | |
shift | |
;; | |
-v|--verbose) | |
VERBOSE=true | |
shift | |
;; | |
-h|--help) | |
HELP=true | |
shift | |
;; | |
--env=*) | |
ENV="${param#*=}" | |
shift | |
;; | |
*) | |
;; | |
esac | |
shift # past argument or value | |
done | |
} | |
# ===== Specify functions | |
# Make bytes human readable | |
function toHumanReadableSize(){ | |
local sizeBytes="${1}" | |
numfmt --to='iec-i' --suffix='B' --padding='7' "${sizeBytes}" | |
} | |
# Remove tmp file | |
function clearTmp(){ | |
local tmpFile=${1}; | |
if [ -f ${tmpFile} ]; then | |
rm ${tmpFile} | |
fi | |
} | |
# Create tmp file | |
function getTmpFile(){ | |
# Fetch target | |
if [ -z ${tmpFile} ]; then | |
tmpFile=$(tempfile) | |
fi | |
echo ${tmpFile} | |
} | |
# == Minify a file | |
function minifyFile(){ | |
local target=$1 | |
# Fetch target | |
if [ -z ${target} ]; then | |
echo -e "Specify a target" | |
exit; | |
fi | |
tmpFile=$(getTmpFile) | |
# if its a file | |
if [ -f ${target} ];then | |
FILESIZE=$(stat -c%s "$target") | |
readableSizeOriginal=$(toHumanReadableSize ${FILESIZE}) | |
if [[ ${target} == *".js"* ]]; then | |
${JSMIN} ${target} > "${tmpFile}" | |
fi | |
if [[ ${target} == *".css"* ]]; then | |
${CSSMIN} ${target} > "${tmpFile}" | |
fi | |
if [[ ${target} == *".png"* ]]; then | |
${CONV} ${target} -strip -alpha Remove "${tmpFile}" | |
fi | |
# Dry run, no save | |
if [[ -z ${DRYRUN+x} ]]; then | |
cat ${tmpFile} > ${target} | |
fi | |
MINSIZE=$(stat -c%s "${tmpFile}") | |
OPTIMAMOUNT=`expr $FILESIZE - $MINSIZE` | |
readableSize=$(toHumanReadableSize ${OPTIMAMOUNT}) | |
echo -e "${Yellow}Minified file: ${NC} ${target} ${Blue}Size: ${NC}${readableSizeOriginal} ${Green} Saved: ${readableSize} ${NC}" | |
fi | |
clearTmp ${tmpFile} | |
} | |
# Minify global command | |
function minify(){ | |
# Loop params | |
for elem in $@; do | |
# echo -e ${#@} # Gives count of array | |
# echo -e "- ${elem}" | |
# ~ If element contains a dash, mark it as parameter | |
# echo -e $elem | |
if [[ ${elem} == "-"* ]]; then | |
fetchParams ${elem}; shift | |
else | |
local target=$elem; shift | |
fi | |
done | |
# Fetch target | |
if [ -z ${target} ]; then | |
echo -e "Specify a target" | |
exit; | |
fi | |
# Create temp file | |
local tmpFile=$(tempfile) | |
# if its a file | |
if [ -f ${target} ];then | |
minifyFile $target | |
fi | |
if [ -d ${target} ];then | |
echo -e "Minifying directory: ${target}" | |
for toProcessFile in $(find ${target} -type f -iname "*.js*" -or -iname "*.css*" -or -iname "*.png"); do | |
minify ${toProcessFile} | |
done | |
echo -e "Finished minifying" | |
fi | |
clearTmp ${tmpFile} | |
} | |
# Function executor | |
${arg%%/} $@ | |
trap shutdown EXIT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment