dc09.ru-old/build.sh

100 lines
2 KiB
Bash
Executable file

#!/usr/bin/env bash
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
BOLD='\033[1m'
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"
}
start() {
echo
echo -e "${YELLOW}[+]${RESET} ${BOLD}Building web site in:${RESET}"
echo -e "${BOLD}$PWD${RESET}"
}
success() {
echo
echo -e "${GREEN}[V]${RESET} ${BOLD}Done${RESET}"
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-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" \;
}
start
title "Creating dist/"
mkdir -p dist/
title "Checking dependencies"
check_deps terser
check_deps lessc
check_deps cleancss
check_deps html-minifier
check_deps python3
title "Minifying JS"
js_minify
title "Converting LESS"
lessc "styles.less" "$mincss"
title "Minifying CSS"
cleancss -o "$mincss" "$mincss"
title "Editing HTML"
cat index.html | python3 html_conv.py "$minjs_name" "$mincss_name" >"$minhtml"
title "Minifying HTML"
html_minify "$minhtml"
title "Copying other files"
copy_files
success