Replaces the plain python3 http.server + one-shot make blog with a watcher pipeline: chokidar regenerates HTML on MD/template changes, browser-sync serves the site and reloads the browser on rendered-asset changes. First run downloads both via npx; subsequent runs are instant. Preflight checks for npx and pandoc. Port arg parsing is tolerant of legacy --drafts flag ordering (drafts are always included now, since that's what the dev loop actually wants). Cleanup trap kills the watcher on exit so re-runs don't leave orphans.
42 lines
1.3 KiB
Bash
Executable File
42 lines
1.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Dev server for site/: regenerates drafts on each MD change, reloads the
|
|
# browser on each rendered HTML/CSS/JS change. Port is the first numeric arg
|
|
# (default 9000); any other args are ignored for back-compat.
|
|
#
|
|
# First run downloads chokidar-cli + browser-sync into the npm cache — slow
|
|
# once, instant after that.
|
|
|
|
set -euo pipefail
|
|
|
|
PORT=9000
|
|
for arg in "$@"; do
|
|
if [[ "$arg" =~ ^[0-9]+$ ]]; then
|
|
PORT="$arg"
|
|
break
|
|
fi
|
|
done
|
|
|
|
command -v npx >/dev/null || { echo "npx not found. Install Node.js: https://nodejs.org" >&2; exit 1; }
|
|
command -v pandoc >/dev/null || { echo "pandoc not found (required by 'make blog-drafts')." >&2; exit 1; }
|
|
|
|
# Initial render so the first page load has everything.
|
|
make blog-drafts
|
|
|
|
echo "Serving site at http://localhost:$PORT (drafts included, live reload)"
|
|
|
|
# Kill child processes on exit so re-runs don't leave orphaned watchers.
|
|
trap 'kill $(jobs -p) 2>/dev/null' EXIT INT TERM
|
|
|
|
# Regenerate HTML when MD sources or the blog template change.
|
|
npx --yes chokidar-cli \
|
|
"drafts/*.md" "blog/*.md" "site/blog-template.html" \
|
|
-c "make blog-drafts" &
|
|
|
|
# Serve + reload on rendered-asset changes.
|
|
cd site && exec npx --yes browser-sync start \
|
|
--server . \
|
|
--port "$PORT" \
|
|
--files "**/*.html,**/*.css,**/*.js" \
|
|
--no-open \
|
|
--no-notify
|