Skip to content

Commit 12f1dd9

Browse files
committed
Perhaps a slightly better approach
Signed-off-by: FooterManDev <footerman.work@gmail.com>
1 parent 84b9c86 commit 12f1dd9

File tree

1 file changed

+28
-73
lines changed

1 file changed

+28
-73
lines changed

.github/workflows/build-docs.yml

Lines changed: 28 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -189,87 +189,42 @@ jobs:
189189
run: |
190190
set -euo pipefail
191191
cd algolia
192-
PRODUCT="${PRODUCT:-1}"
193-
echo "Setting product=\"$PRODUCT\" for Algolia JSON files (only when missing/empty)."
194-
195-
echo "Found JSON files:"
196-
ls -la *.json || true
197192
193+
processed=0
198194
for f in *.json; do
199-
echo
200-
echo "---- Processing $f ----"
201-
if [ ! -s "$f" ]; then
202-
echo "Skipping empty file: $f (size 0)"
203-
continue
204-
fi
205-
206-
echo "Size: $(wc -c < "$f") bytes"
207-
echo "Head (first 2 lines / 200 bytes):"
208-
head -c 200 "$f" || true
209-
echo
210-
echo "-----"
211-
212-
if jq -e '.' "$f" >/dev/null 2>&1; then
213-
echo "$f is valid JSON — attempting structural update with jq..."
214-
215-
jq --arg p "$PRODUCT" '
216-
if type == "array" then
217-
map(if (.product == null or .product == "") then . + {product:$p} else . end)
218-
elif (type == "object" and has("records") and (.records|type == "array")) then
219-
.records = (.records | map(if (.product == null or .product == "") then . + {product:$p} else . end))
220-
elif type == "object" then
221-
if (.product == null or .product == "") then . + {product:$p} else . end
222-
else
223-
.
224-
end
225-
' "$f" > "$f.tmp"
226-
227-
if [ ! -s "$f.tmp" ]; then
228-
echo "Warning: jq produced empty output for $f — falling back to NDJSON per-line update."
229-
rm -f "$f.tmp"
230-
else
231-
mv "$f.tmp" "$f"
232-
echo "Patched $f (whole-file JSON path)."
233-
continue
234-
fi
195+
[ -e "$f" ] || continue
196+
197+
if jq -e 'type == "array"' "$f" >/dev/null 2>&1; then
198+
jq 'map(if (.product == "") then . + {product:"-1"} else . end)' "$f" > "$f.tmp"
199+
mv "$f.tmp" "$f"
200+
elif jq -e 'has("records") and (.records | type == "array")' "$f" >/dev/null 2>&1; then
201+
jq '.records = (.records | map(if (.product == "") then . + {product:"-1"} else . end))' "$f" > "$f.tmp"
202+
mv "$f.tmp" "$f"
203+
elif jq -e 'type == "object"' "$f" >/dev/null 2>&1; then
204+
jq 'if (.product == "") then . + {product:"-1"} else . end' "$f" > "$f.tmp"
205+
mv "$f.tmp" "$f"
235206
else
236-
echo "$f is not parseable as a single JSON value — attempting NDJSON per-line update."
237-
fi
238-
239-
tmp="$(mktemp)"
240-
while IFS= read -r line || [ -n "$line" ]; do
241-
trimmed="$(printf '%s' "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
242-
if [ -z "$trimmed" ]; then
243-
printf '\n' >> "$tmp"
244-
continue
245-
fi
246-
247-
if printf '%s' "$trimmed" | jq -e '.' >/dev/null 2>&1; then
248-
printf '%s\n' "$trimmed" | jq -c --arg p "$PRODUCT" 'if (.product == null or .product == "") then . + {product:$p} else . end' >> "$tmp"
249-
else
250-
printf '%s\n' "$line" >> "$tmp"
251-
fi
252-
done < "$f"
253-
254-
if [ -s "$tmp" ]; then
207+
tmp="$(mktemp)"
208+
while IFS= read -r line || [ -n "$line" ]; do
209+
trimmed="$(echo "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
210+
if [ -z "$trimmed" ]; then
211+
echo "" >> "$tmp"
212+
continue
213+
fi
214+
if echo "$trimmed" | jq -e '.' >/dev/null 2>&1; then
215+
echo "$trimmed" | jq -c 'if (.product == "") then . + {product:"-1"} else . end' >> "$tmp"
216+
else
217+
echo "$line" >> "$tmp"
218+
fi
219+
done < "$f"
255220
mv "$tmp" "$f"
256-
echo "Patched $f (NDJSON path)."
257-
else
258-
rm -f "$tmp"
259-
echo "Error: NDJSON fallback produced empty file for $f — leaving original intact and aborting."
260-
exit 1
261221
fi
262-
done
263222
264-
echo
265-
echo "Final files summary:"
266-
for f in *.json; do
267-
echo "-> $f : $(wc -c < "$f") bytes"
268-
echo " head:"
269-
head -c 200 "$f" || true
270-
echo
223+
processed=$((processed + 1))
271224
done
272225
226+
echo "Files parsed: $processed"
227+
273228
- name: Setup Algolia CLI
274229
uses: algolia/setup-algolia-cli@master
275230

0 commit comments

Comments
 (0)