Skip to content

Instantly share code, notes, and snippets.

@plxity
Last active May 17, 2022 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plxity/db387ae653d8e4c9e7b77a65a67982d5 to your computer and use it in GitHub Desktop.
Save plxity/db387ae653d8e4c9e7b77a65a67982d5 to your computer and use it in GitHub Desktop.
Compress image using pre-commit hook
#!/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