Last active
May 17, 2022 18:47
-
-
Save plxity/db387ae653d8e4c9e7b77a65a67982d5 to your computer and use it in GitHub Desktop.
Compress image using pre-commit hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
. "$(dirname "$0")/_/husky.sh" | |
command -v ./node_modules/.bin/svgo >/dev/null 2>&1 || { | |
echo "\033[1mPlease install svgo to reduce images size before commit\033[0m" | |
echo "Install svgo as a devDependency using npm install -D svgo" | |
exit 1; | |
} | |
for file in `git diff --diff-filter=ACMRT --cached --name-only | grep ".svg\$"` | |
do | |
exec < /dev/tty | |
read -p "Do you want to compress $file? [yN]: " yn | |
case $yn in | |
[Yy]* ) | |
echo "Compressing $file" | |
./node_modules/.bin/svgo $file -o ${file%.svg}.new | |
mv -f ${file%.svg}.new $file | |
git add $file;; | |
* ) echo "Skipping $file" && continue;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment