-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview.sh
More file actions
executable file
·80 lines (68 loc) · 2.26 KB
/
Copy pathpreview.sh
File metadata and controls
executable file
·80 lines (68 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/zsh
# Renders all projects, serves docs/ locally, and auto-reloads on changes.
# Usage: ./preview.sh
set -e
ROOT="$(cd "$(dirname "$0")" && pwd)"
PORT="${1:-8000}"
render_all() {
echo "==> Rendering main site..."
quarto render "$ROOT" 2>&1 || true
echo "==> Rendering microbiome..."
quarto render "$ROOT/microbiome" 2>&1 || true
echo "==> Rendering mutual-funds..."
quarto render "$ROOT/mutual-funds" 2>&1 || true
echo "==> Render complete at $(date +%H:%M:%S)"
}
cleanup() {
echo "\n==> Shutting down..."
[[ -n "$SERVER_PID" ]] && kill "$SERVER_PID" 2>/dev/null
[[ -n "$WATCH_PID" ]] && kill "$WATCH_PID" 2>/dev/null
exit 0
}
trap cleanup INT TERM
# --- Initial render ---
render_all
# --- Start HTTP server in background ---
python3 -m http.server "$PORT" --directory "$ROOT/docs" &
SERVER_PID=$!
echo ""
echo "==> Serving at http://localhost:$PORT"
echo " Main site: http://localhost:$PORT"
echo " Vol 1 Microbiome: http://localhost:$PORT/microbiome/"
echo " Vol 2 Finance: http://localhost:$PORT/mutual-funds/"
echo ""
# --- Watch for changes and re-render ---
if command -v fswatch &>/dev/null; then
echo "==> Watching for changes (fswatch)... Press Ctrl+C to stop."
fswatch --one-per-batch --latency=1 \
--exclude='\.git' --exclude='_site' --exclude='_book' --exclude='docs' --exclude='\.quarto' \
--include='\.qmd$' --include='\.yml$' --include='\.css$' --include='\.bib$' --include='\.tex$' \
"$ROOT" | while read -r; do
echo ""
echo "==> Change detected, re-rendering..."
render_all
done &
WATCH_PID=$!
else
echo "==> fswatch not found; falling back to polling (every 3s)..."
(
get_fingerprint() {
find "$ROOT" \( -name '*.qmd' -o -name '*.yml' -o -name '*.css' -o -name '*.bib' -o -name '*.tex' \) \
-not -path '*/.git/*' -not -path '*/_site/*' -not -path '*/_book/*' -not -path '*/docs/*' -not -path '*/.quarto/*' \
-exec stat -f '%m %N' {} + 2>/dev/null | sort | md5
}
LAST=$(get_fingerprint)
while true; do
sleep 3
NOW=$(get_fingerprint)
if [[ "$NOW" != "$LAST" ]]; then
echo ""
echo "==> Change detected, re-rendering..."
render_all
LAST=$(get_fingerprint)
fi
done
) &
WATCH_PID=$!
fi
wait