optimize-svg: Add an optional argument to optimize single files.

Do not call `generate_integration_bots_avatars.py` for single files.
This commit is contained in:
Niloth P 2024-11-18 13:11:42 +05:30
parent ce62d89f2b
commit 96153f4c9d
1 changed files with 19 additions and 6 deletions

View File

@ -4,13 +4,16 @@ set -e
usage() { usage() {
cat <<'EOF' cat <<'EOF'
Usage: Usage:
optimize-svg --check optimize-svg [--check] [filename]
optimize-svg --help optimize-svg --help
Options: Options:
--check --check
This will check for unoptimized SVG files rather than automatically optimizing them. This will check for unoptimized SVG files rather than automatically optimizing them.
This allows us to run the script in CI. This allows us to run the script in CI.
[filename]
The filename of the SVG file to optimize, located in static/images/integrations/logos.
If not provided, all files in the directory will be optimized.
EOF EOF
} }
@ -36,18 +39,26 @@ while true; do
esac esac
done done
if [ "$#" -gt 0 ]; then if [ "$#" -gt 1 ]; then
usage >&2 usage >&2
exit 1 exit 1
fi fi
ZULIP_PATH="$(readlink -f "$(dirname "$0")"/../..)" ZULIP_PATH="$(readlink -f "$(dirname "$0")"/../..)"
PNPM="/usr/local/bin/pnpm" PNPM="/usr/local/bin/pnpm"
FILE_PATH="static/images/integrations/logos" BASE_PATH="static/images/integrations/logos"
if [ "$#" -eq 0 ]; then
FILE_PATH="$BASE_PATH"
SVGO_FLAG="-f"
else
FILE_PATH="$BASE_PATH/$1"
SVGO_FLAG=""
fi
if [ -n "$CHECK_UNOPTIMIZED" ]; then if [ -n "$CHECK_UNOPTIMIZED" ]; then
TEMP_PATH=$(mktemp -d svgo_temp_XXXXXX) TEMP_PATH=$(mktemp -d svgo_temp_XXXXXX)
RESULT=$("$PNPM" exec svgo -o "$TEMP_PATH" -f "$FILE_PATH" | grep -o '\.[0-9]% = ' | wc -l) RESULT=$("$PNPM" exec svgo -o "$TEMP_PATH" $SVGO_FLAG "$FILE_PATH" | grep -o '\.[0-9]% = ' | wc -l)
rm -rf "$TEMP_PATH" rm -rf "$TEMP_PATH"
if [ "$RESULT" -ge 1 ]; then if [ "$RESULT" -ge 1 ]; then
echo "ERROR: svgo detected unoptimized SVG file(s)." 1>&2 echo "ERROR: svgo detected unoptimized SVG file(s)." 1>&2
@ -57,6 +68,8 @@ if [ -n "$CHECK_UNOPTIMIZED" ]; then
echo "SUCCESS: SVG file(s) are optimized!" echo "SUCCESS: SVG file(s) are optimized!"
fi fi
else else
"$PNPM" exec svgo -q -f "$FILE_PATH" "$PNPM" exec svgo -q $SVGO_FLAG "$FILE_PATH"
if [ "$#" -eq 0 ]; then
"$ZULIP_PATH"/tools/setup/generate_integration_bots_avatars.py "$ZULIP_PATH"/tools/setup/generate_integration_bots_avatars.py
fi fi
fi