#!/usr/bin/env bash RED='\033[1;31m' GREEN='\033[1;32m' YELLOW='\033[1;33m' BLUE='\033[1;34m' RESET='\033[0m' minjs="dist/script.min.js" mincss="dist/style.min.css" minhtml="dist/index.html" minjs_name="script.min.js" mincss_name="style.min.css" minhtml_name="index.html" title() { echo echo -e "${BLUE}[*]${RESET} $1" } success() { echo echo -e "${GREEN}[V]${RESET} Done" echo } check_deps() { which "$1" >/dev/null 2>/dev/null if [[ $? == 1 ]]; then echo -e "${RED}[!]${RESET} $1 is required" echo exit 1 fi } js_minify() { order=('js/anim.js' 'js/script.js' 'js/init.js' 'js/menu.js' 'js/control.js' 'js/handlers.js') terser -o "$minjs" ${order[*]} } html_minify () { html-minifier \ --collapse-boolean-attributes \ --collapse-inline-tag-whitespace \ --collapse-whitespace \ --remove-attribute-quotes \ --remove-comments \ --remove-optional-tags \ --remove-script-type-attributes \ --remove-style-link-type-attributes \ --use-short-doctype \ -o "$minhtml" \ "$1" } copy_files() { exclude=('\.$' '\.git' 'index.html' 'styles.less' 'js' 'dist' 'build.sh' 'html_conv' 'README.md') items="${exclude[@]}" regex="${items//[[:space:]]/|}" find -maxdepth 1 -exec bash -c "if [[ {} =~ $regex ]]; then :; else cp -r {} dist/; fi" \; } title "Checking dependencies" check_deps terser check_deps lessc check_deps cleancss check_deps html-minifier check_deps python3 mkdir dist/ title "Minifying JS" js_minify title "Converting LESS" lessc "styles.less" "$mincss" title "Minifying CSS" cleancss -o "$mincss" "$mincss" title "Minifying HTML" cat index.html | python3 html_conv.py "$minjs_name" "$mincss_name" >"$minhtml" html_minify "$minhtml" title "Copying other files" copy_files success