diff --git a/scripts/brotli-wrapper.sh b/scripts/brotli-wrapper.sh new file mode 100644 index 0000000..ca3d163 --- /dev/null +++ b/scripts/brotli-wrapper.sh @@ -0,0 +1,8 @@ +#!/bin/sh +# compress a file with brotli if it isn't already compressed. +set -e -u + +if [ ! -f "$1.br" ]; then + brotli -Z -- "$1" +fi + diff --git a/scripts/compress.sh b/scripts/compress.sh index 6b68660..817a754 100644 --- a/scripts/compress.sh +++ b/scripts/compress.sh @@ -5,18 +5,19 @@ # 2. "gzip" or "brotli" # 3. ECT zopfli compression level -# no pipefail here since there are no pipes. +#shellcheck disable=SC3040 # This only sets pipefail if it's available and otherwise does nothing +set -o pipefail 2>/dev/null || true set -e -u +dirname="$(dirname "$0")" output_dir="$1" format="$2" alias find_compressible='find "$output_dir" -type f \( -name "*.html" -o -name "*.txt" -o -name "*.xml" -o -name "*.webmanifest" -o -name "*.*.svg" -o -name "*.json" \)' if [ "$format" = "gzip" ]; then - compress_level="$3" - find_compressible -exec ect -quiet -"$compress_level" -gzip {} \; - find_compressible -exec touch -r {} {}.gz \; -elif [ "$2" = "brotli" ]; then - find_compressible -exec brotli -Z -- {} \; + # compression level should be the third argument + find_compressible | xargs -n1 sh "$dirname/ect-wrapper.sh" "${3-6}" +elif [ "$format" = "brotli" ]; then + find_compressible | xargs -n1 sh "$dirname/brotli-wrapper.sh" fi diff --git a/scripts/ect-wrapper.sh b/scripts/ect-wrapper.sh new file mode 100644 index 0000000..d6dc945 --- /dev/null +++ b/scripts/ect-wrapper.sh @@ -0,0 +1,9 @@ +#!/bin/sh +# compress a file with ect and preserve the mtime +# args: compression level and filename. +set -e -u + +if [ ! -f "$2.gz" ]; then + ect -quiet -"$1" -gzip "$2" + touch -r "$2" "$2.gz" +fi