#!/usr/bin/env bash # Build the Discord Activity: the bug.tv site + the watch-party overlay (docs/protocol.md "Build"). # # scripts/build-activity.sh [apiBase=/tv] # # - copies the site (bug-tv `web/` or its nix packages.web output) to # - index.html + 404.html: sets the bug-tv-api / -tmdb / -img / -force-proxy metas (inserted when # missing), drops the Google Fonts links (Activity CSP), appends overlay.css + discord.js # - bundles activity/js/discord.js with @discord/embedded-app-sdk → /discord.js # # esbuild: $ESBUILD (e.g. nixpkgs' esbuild) or `npx esbuild`. No network access needed; # @discord/embedded-app-sdk must be in node_modules. set -euo pipefail if [ $# -lt 2 ]; then echo "usage: $0 [apiBase=/tv]" >&2 exit 2 fi web=$1 out=$2 api=${3:-/tv} here=$(cd "$(dirname "$0")/.." && pwd) esbuild=${ESBUILD:-npx esbuild} [ -f "$web/index.html" ] || { echo "build-activity: $web/index.html not found" >&2; exit 1; } rm -rf "$out" mkdir -p "$out" cp -R "$web"/. "$out"/ chmod -R u+w "$out" rm -rf "$out/test" # bug-tv's web tests, if building from the source dir # The site's own img/ would sit under Discord's /img → image.tmdb.org mapping: move it to # site-img/ and rewrite the references in the pages and stylesheets (before the metas below). if [ -d "$out/img" ]; then mv "$out/img" "$out/site-img" find "$out" -path "$out/vendor" -prune -o -type f \( -name '*.html' -o -name '*.css' \) -print | while IFS= read -r f; do sed -i -E "s#([\"'(=])(\\.\\./|\\./|/)?img/#\\1\\2site-img/#g" "$f" done fi set_meta() { # file name content local f=$1 name=$2 content=$3 if grep -q "]*>||" "$f" else sed -i -E "0,/<\/head>/s|| \n|" "$f" fi } for page in index.html 404.html; do f=$out/$page [ -f "$f" ] || continue set_meta "$f" bug-tv-api "$api" set_meta "$f" bug-tv-tmdb /tmdb/3 set_meta "$f" bug-tv-img /img/t/p/ set_meta "$f" bug-tv-force-proxy 1 # Google Fonts (stylesheet + preconnects) are blocked by the Activity CSP sed -i -E '/]*fonts\.(googleapis|gstatic)\.com[^>]*>/d; /]*(image|api)\.(tmdb|themoviedb)\.org[^>]*>/d' "$f" grep -q 'href="/overlay.css"' "$f" || sed -i -E '0,/<\/head>/s|| \n|' "$f" grep -q 'src="/discord.js"' "$f" || sed -i -E '0,/<\/body>/s|| \n|' "$f" done cp "$here/activity/overlay.css" "$out/overlay.css" # shellcheck disable=SC2086 (cd "$here" && $esbuild activity/js/discord.js --bundle --format=esm --target=es2020 --minify --legal-comments=none \ --log-level=warning --outfile="$out/discord.js") echo "build-activity: $out (api $api)"