diff --git a/web/cashtab/scripts/addGenerated.sh b/web/cashtab/scripts/addGenerated.sh index c48db964b..5dae05384 100755 --- a/web/cashtab/scripts/addGenerated.sh +++ b/web/cashtab/scripts/addGenerated.sh @@ -1,19 +1,28 @@ #!/usr/bin/env bash export LC_ALL=C -auto_add_generated_to_snapshots(){ -snapshot_file_array=() -snapshot_file_array+=$(find -name "*.test.js.snap") -for i in $snapshot_file_array -do -if ! grep -q @generated $i -then -sed -i '1 s/$/ @generated/' $i -fi -done -} +set -euo pipefail + +# Build the generated marker without having this script being marked as +# generated itself. +AT=@ +AT_GENERATED="${AT}generated" -auto_add_generated_to_snapshots +TOPLEVEL=$(git rev-parse --show-toplevel) -exit 0 \ No newline at end of file +# Note that this won't work if the with files containing a space in their name. +# This can be solved by using a null char delimiter (-d '\0') for read in +# combination with find's option -print0 to get a compatible output, but this is +# not supported by all find variants so we'll do without it for now. +find "${TOPLEVEL}/web/cashtab/src" -name "*.test.js.snap" | while read i; do + # It is possible to avoid the grep and have sed do it all, but: + # 1. This is a more complex sed usage + # 2. It will add the generated mark in files that have it already on another + # line. + if ! grep -q "${AT_GENERATED}" "${i}"; then + # The -i.bak option tells sed to do in-place replacement and create a backup + # file with a .bak suffix. This is mandatory for BSD/OSX compatibility. + sed -i.bak "1 s/$/ ${AT_GENERATED}/" "${i}" && rm -f "${i}.bak" + fi +done