Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit a92fa64

Browse files
committed
Fix optipng-linter.sh to handle multiple pngs
Also clean up the script and make it pass shellcheck.
1 parent 79c9f5c commit a92fa64

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

scripts/custom_linters/optipng-linter.sh

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
function get_size {
16-
LS_OUTPUT=($(ls -n $1));
17-
echo ${LS_OUTPUT[4]};
16+
LS_OUTPUT=($(ls -n "$1"))
17+
echo ${LS_OUTPUT[4]}
1818
}
1919

20-
FILENAME=$1;
21-
OUTFILE='/tmp/git-lint/optipng.png';
22-
origsize=$(get_size $FILENAME);
23-
rm $OUTFILE 2> /dev/null;
24-
optipng -out $OUTFILE -o9 $FILENAME > /dev/null 2> /dev/null;
25-
newsize=$(get_size $OUTFILE);
20+
FILENAME=$1
21+
OUTFILE="$(mktemp)"
22+
ORIGSIZE=$(get_size "$FILENAME")
23+
rm "$OUTFILE" 2> /dev/null
24+
optipng -out "$OUTFILE" -o9 "$FILENAME" > /dev/null 2> /dev/null
25+
NEWSIZE=$(get_size "$OUTFILE")
26+
rm "$OUTFILE" 2> /dev/null
2627

27-
if [ $newsize -gt 0 ] && [ $newsize -lt $origsize ]; then
28-
reduction=`bc <<< "scale = 2; (100*($origsize - $newsize) / $origsize)"`;
29-
echo "The file size can be losslessly reduced from $origsize to $newsize bytes. ($reduction% filesize reduction)";
30-
echo "Use: optipng -o9 $FILENAME";
31-
exit 1;
28+
if [ "$NEWSIZE" != "" ] && [ "$NEWSIZE" -gt 0 ] && [ "$NEWSIZE" -lt "$ORIGSIZE" ]; then
29+
REDUCTION=$(bc <<< "scale = 2; (100*($ORIGSIZE - $NEWSIZE) / $ORIGSIZE)")
30+
echo "The file size can be losslessly reduced from $ORIGSIZE to $NEWSIZE bytes. ($REDUCTION% filesize reduction)"
31+
echo "Use: optipng -o9 $FILENAME"
32+
exit 1
3233
fi

0 commit comments

Comments
 (0)