This repository has been archived on 2022-05-01. You can view files and clone it, but cannot push or open issues or pull requests.
www/scripts_nohugo/outils/minify

91 lines
2.5 KiB
Bash

#!/bin/sh
#set -x
clear
########################################################################
###
##
#
# Author: Stéphane HUC
# mail: devs@stephane-huc.net
# gpg:fingerprint: CE2C CF7C AB68 0329 0D20 5F49 6135 D440 4D44 BD58
#
# License: Public Domain
#
# Git:
#
# Date: 2020/04/15
#
##
###
########################################################################
ROOT="$(dirname "$(readlink -f -- "$0")")"
minifier="$ROOT/yuicompressor-2.4.8.jar"
java="/usr/bin/java"
SH="k"
set -A exts 'css' 'js'
########################################################################
#printf '%s\n' "Write a root folder (where css/js folders exists)!:"
#read -r dir
dir="$1"
if [ -d "$dir" ]; then
for ext in "${exts[@]}"; do
while read -r file; do
#echo "$(basename $file)"
search=".min.$ext"; #echo "search: $search";
if [[ "$(basename $file)" != *"$search"* ]]; then
printf "file: ${file}\n";
case "$SH" in
"b") mini="${file/.${ext}/.min.${ext}}" ;;
"k") mini="${file%.$ext}.min.$ext" ;;
esac
printf "minified: ${mini}\n"
if [ -x $(which stat) ]; then
case "$SH" in
"b")
[ -f "$file" ] && file_seconds=$(stat -c "%Y" "$file")
[ -f "$mini" ] && mini_seconds=$(stat -c "%Y" "$mini")
;;
"k")
[ -f "$file" ] && file_seconds=$(stat -f "%m" "$file")
[ -f "$mini" ] && mini_seconds=$(stat -f "%m" "$mini")
;;
esac
else
[ -f "$file" ] && file_seconds=$(date -r "$file" +%s)
[ -f "$mini" ] && mini_seconds=$(date -r "$mini" +%s)
fi
if [ "${file_seconds}" -gt "${mini_seconds}" ]; then
#printf "file: $file_seconds sec | name: $mini_seconds sec\n";
if $java -jar $minifier "${file}" -o "${mini}"; then
printf "[ %s ] %s\n" "OK" "file $file minified to $mini"
else
printf "[ %s ] %s\n" "KO" "Can not minify file $file to $mini!"
fi
fi
unset file_seconds mini mini_seconds
fi
done <<EOF
$(find $dir/${ext}/ -type f -maxdepth 1 -name "*.${ext}")
EOF
done
else
printf "%s \n" "This folder '$dir' seems not exists!"
fi
unset ROOT minifier dirs