diff options
author | Evan Read <eread@gitlab.com> | 2023-03-28 03:23:34 +0300 |
---|---|---|
committer | Evan Read <eread@gitlab.com> | 2023-03-29 04:46:49 +0300 |
commit | 826a22430d373bbfd834cdf544e0d462a8cc7125 (patch) | |
tree | eda3d8ac3aedb00c0f9733f5f3bdbe6d6d194a8b /scripts | |
parent | 7300a945653d8afc686d070fd81cdd5701a74cdb (diff) |
Refactor minify-assets.sh script
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/minify-assets.sh | 47 |
1 files changed, 38 insertions, 9 deletions
diff --git a/scripts/minify-assets.sh b/scripts/minify-assets.sh index 3cca8789..aa27512e 100755 --- a/scripts/minify-assets.sh +++ b/scripts/minify-assets.sh @@ -1,17 +1,22 @@ #!/usr/bin/env bash +# shellcheck disable=SC2059 TARGET="$1" VER="$2" MINIFY_FLAGS=("--html-keep-document-tags" "--html-keep-whitespace" "--recursive") +COLOR_RED="\e[31m" +COLOR_GREEN="\e[32m" +COLOR_RESET="\e[39m" + if [ -z "$TARGET" ] || [ -z "$VER" ]; then echo "Usage: $0 <target> <ver>" - echo "Either <target> or <ver> is missing. Exiting." + printf "${COLOR_RED}ERROR: Either <target> or <ver> is missing.${COLOR_RESET}\n" exit 1 fi if ! [ -d "$TARGET" ]; then - echo "Target directory $TARGET does not exist. Exiting." + printf "${COLOR_RED}ERROR: Target directory $TARGET does not exist.${COLOR_RESET}\n" exit 1 fi @@ -25,17 +30,41 @@ else then MINIFY_BIN=/scripts/minify else - echo "minify binary not found in PATH. Exiting." + printf "${COLOR_RED}ERROR: minify not found in PATH. Run 'make setup'.${COLOR_RESET}\n" exit 1 fi fi # Minify assets -printf "Optimizing assets..." +printf "${COLOR_GREEN}INFO: Minifying HTML...${COLOR_RESET}\n" +if $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=html --match="\.html$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}"; then + printf "${COLOR_GREEN}INFO: HTML minified!${COLOR_RESET}\n" +else + printf "${COLOR_RED}ERROR: Couldn't minify HTML${COLOR_RESET}\n" + exit 1 +fi + +printf "${COLOR_GREEN}INFO: Minifying CSS...${COLOR_RESET}\n" +if $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=css --match="\.css$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}"; then + printf "${COLOR_GREEN}INFO: CSS minified!${COLOR_RESET}\n" +else + printf "${COLOR_RED}ERROR: Couldn't minify CSS${COLOR_RESET}\n" + exit 1 +fi -printf "HTML..."; $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=html --match="\.html$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}" || true -printf "CSS..." ; $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=css --match="\.css$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}" || true -printf "JSON..."; $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=json --match="\.json$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}" || true -printf "SVG..." ; $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=svg --match="\.svg$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}" || true -echo "Done" +printf "${COLOR_GREEN}INFO: Minifying JSON...${COLOR_RESET}\n" +if $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=json --match="\.json$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}"; then + printf "${COLOR_GREEN}INFO: JSON minified!${COLOR_RESET}\n" +else + printf "${COLOR_RED}ERROR: Couldn't minify JSON${COLOR_RESET}\n" + exit 1 +fi + +printf "${COLOR_GREEN}INFO: Minifying SVGs...${COLOR_RESET}\n" +if $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=svg --match="\.svg$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}"; then + printf "${COLOR_GREEN}INFO: SVGs minified!${COLOR_RESET}\n" +else + printf "${COLOR_RED}ERROR: Couldn't minify SVGs${COLOR_RESET}\n" + exit 1 +fi |