north-star / commit
ci pasta!
scripts/clean-old-firebase.sh | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/scripts/clean-old-firebase.sh b/scripts/clean-old-firebase.sh index 41dc0e8..31ced34 100755 --- a/scripts/clean-old-firebase.sh +++ b/scripts/clean-old-firebase.sh @@ -34,12 +34,40 @@ fi run_firebase() { "$FIREBASE_BIN" --config "$CONFIG" --project "$PROJECT" "$@"; } -ROOMS_JSON=$(run_firebase database:get /rooms --instance "$INSTANCE") || { +sanitize_firebase_json_output() { + local raw="$1" + + # Drop ANSI color codes/noise and trim blank lines. + local clean + clean=$(printf '%s\n' "$raw" | sed -E 's/\x1B\[[0-9;]*[A-Za-z]//g' | sed '/^[[:space:]]*$/d') + + # Some firebase-tools builds append a trailing {"status":"success"} object. + # Keep only the payload emitted before that footer. + clean=$(printf '%s\n' "$clean" | awk ' + BEGIN { stop = 0 } + /^\{[[:space:]]*"status"[[:space:]]*:[[:space:]]*"success"/ { stop = 1 } + stop == 0 { print } + ') + + printf '%s' "$clean" +} + +ROOMS_RAW=$(run_firebase database:get /rooms --instance "$INSTANCE") || { echo "ERROR: Failed to reach Firebase. Is the CLI authenticated?" >&2 echo " Run: npm exec firebase -- login" >&2 exit 1 } +ROOMS_JSON=$(sanitize_firebase_json_output "$ROOMS_RAW") + +if ! printf '%s' "$ROOMS_JSON" | jq -e . >/dev/null 2>&1; then + echo "ERROR: Firebase returned a non-JSON /rooms payload in CI." >&2 + echo "Raw output (first 400 chars):" >&2 + printf '%s' "$ROOMS_RAW" | head -c 400 >&2 + echo >&2 + exit 1 +fi + if [[ "$ROOMS_JSON" == "null" ]] || [[ -z "$ROOMS_JSON" ]]; then echo "No rooms data found at /rooms in instance $INSTANCE." exit 0