feat: Add optimizer command

This commit is contained in:
Simon 2021-04-29 13:57:02 +02:00
parent e7237cd7b9
commit a9891a17d4
3 changed files with 8702 additions and 9 deletions

8676
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -3,9 +3,11 @@
"version": "0.1.0", "version": "0.1.0",
"author": "Simon <simon@lamelio.fr>", "author": "Simon <simon@lamelio.fr>",
"license": "GPL-3.0", "license": "GPL-3.0",
"devDependencies": { "dependencies": {
"imagemin-cli": "^6.0.0",
"richtypo": "4.0.7", "richtypo": "4.0.7",
"richtypo-rules-common": "4.0.2", "richtypo-rules-common": "4.0.2",
"richtypo-rules-fr": "4.0.2" "richtypo-rules-fr": "4.0.2",
"svgo": "^2.3.0"
} }
} }

View File

@ -7,6 +7,7 @@ set -eu
START=`date +%s` START=`date +%s`
BACKUP_DATE=`date +%Y%m%d` BACKUP_DATE=`date +%Y%m%d`
BACKUP_LOGS_FILENAME=${BACKUP_DATE}_${URL}_logs.tar BACKUP_LOGS_FILENAME=${BACKUP_DATE}_${URL}_logs.tar
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" # https://stackoverflow.com/a/246128/6429616
logs_backup() { logs_backup() {
echo "📥 Sauvegarde les logs" echo "📥 Sauvegarde les logs"
@ -78,6 +79,33 @@ favicons() {
convert static/logo.png -resize 32x32 -colors 16 \( -clone 0 -resize 16x16 -extent 16x16 \) \( -clone 0 -resize 32x32 -extent 32x32 \) -delete 0 static/favicon.ico convert static/logo.png -resize 32x32 -colors 16 \( -clone 0 -resize 16x16 -extent 16x16 \) \( -clone 0 -resize 32x32 -extent 32x32 \) -delete 0 static/favicon.ico
} }
optimizer() {
echo
echo "🖼️ Optimisation des images"
echo
# $SCRIPT_DIR/../node_modules/.bin/imagemin $PWD/static -o $PWD/static
find ./static ./content -type f | xargs -I % bash -c '$0/../node_modules/.bin/imagemin % -o $(dirname %)' $SCRIPT_DIR
}
optimizer_git_image() {
for file in $(git diff --name-only --diff-filter=M --ignore-submodules); do
last_commit=`git log -n 1 --pretty=format:%H -- $file`
current_size=`cat $file | wc -c`
old_size=`git cat-file -p $last_commit:$file | wc -c`
# https://stackoverflow.com/a/48999882/6429616
check_git_command=`git config --global --list | grep amend-to | wc -c`
if [ "$check_git_command" -ne 187 ]; then
git config --global alias.amend-to '!f() { SHA=`git rev-parse "$1"`; git stash -k && git commit --fixup "$SHA" && GIT_SEQUENCE_EDITOR=true git rebase --interactive --autosquash "$SHA^" && git stash pop; }; f'
fi
if [ "$current_size" -lt "$old_size" ]; then
git add $file
git amend-to $last_commit
fi
done
}
help_generic() { help_generic() {
echo echo
echo "💡 Aide 💡" echo "💡 Aide 💡"
@ -93,6 +121,7 @@ help_generic() {
echo "- ./run logs_restore 📤 Restaure les logs" echo "- ./run logs_restore 📤 Restaure les logs"
echo "- ./run stats 🌠 Mise à jour de GoAccess" echo "- ./run stats 🌠 Mise à jour de GoAccess"
echo "- ./run favicons 🔍 Génération des favicons à partir d'une image 512px" echo "- ./run favicons 🔍 Génération des favicons à partir d'une image 512px"
echo "- ./run optimizer 🖼️ Optimisation des images"
} }
end() { end() {