#!/usr/bin/env bash # Builds the embeddable Scratch 3 editor/player into frontend/public/scratch/. # Source of truth for that (git-ignored) folder. Needs node + npm and network access. # Usage: scripts/build-scratch.sh (SCRATCH_BUILD_DIR= keeps the npm cache between runs, # SCRATCH_OUT= changes the output folder) set -euo pipefail SCRATCH_GUI_VERSION="15.3.0" # @scratch/scratch-gui, AGPL-3.0-only (see docs/scratch-editor.md) SOURCE_URL="https://git.zenvairo.es/dasemu/futuredev-scratch-editor" # public mirror of our wrapper (AGPL ยง13) ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" SRC="$ROOT/scripts/scratch" OUT="${SCRATCH_OUT:-$ROOT/frontend/public/scratch}" WORK="${SCRATCH_BUILD_DIR:-$(mktemp -d)}" [ -n "${SCRATCH_BUILD_DIR:-}" ] || trap 'rm -rf "$WORK"' EXIT mkdir -p "$WORK" cd "$WORK" [ -f package.json ] || npm init -y >/dev/null npm install --no-audit --no-fund --save-exact --ignore-scripts "@scratch/scratch-gui@$SCRATCH_GUI_VERSION" PKG="$WORK/node_modules/@scratch/scratch-gui" DIST="$PKG/dist" [ -f "$DIST/scratch-gui-standalone.js" ] || { echo "standalone bundle missing in package" >&2; exit 1; } # Assemble in a temp dir, then swap, so a failed build never leaves a half-written output. mkdir -p "$ROOT/frontend/public" STAGE="$(mktemp -d "$ROOT/frontend/public/.scratch-stage.XXXXXX")" trap 'rm -rf "$STAGE"; [ -n "${SCRATCH_BUILD_DIR:-}" ] || rm -rf "$WORK"' EXIT cp "$DIST/scratch-gui-standalone.js" "$DIST/scratch-gui-standalone.js.LICENSE.txt" "$STAGE/" cp "$DIST/extension-worker.js" "$DIST/extension-worker.js.LICENSE.txt" "$STAGE/" cp "$DIST"/*.hex "$STAGE/" cp -r "$DIST/static" "$STAGE/static" mkdir -p "$STAGE/chunks" # Drop face-sensing model files (tens of MB, fetched from the network anyway) and source maps. find "$DIST/chunks" -maxdepth 1 -type f ! -name '*.map' -exec cp {} "$STAGE/chunks/" \; cp "$PKG/LICENSE" "$STAGE/LICENSE-scratch-gui.txt" cp "$PKG/TRADEMARK" "$STAGE/TRADEMARK-scratch.txt" cp "$SRC/index.html" "$SRC/storage-shim.js" "$SRC/bridge.js" "$STAGE/" # The bundle is built with webpack publicPath "/" (main bundle and the nested fetch-worker # bundle); it is served from /scratch/, so rewrite every occurrence and fail if none/unknown. BUNDLE="$STAGE/scratch-gui-standalone.js" [ "$(grep -oE '__(nested_)?webpack_require(_[0-9]+)?__\.p="/"' "$BUNDLE" | wc -l)" -ge 1 ] \ || { echo "publicPath marker not found; bundle layout changed" >&2; exit 1; } sed -i -E 's#(__(nested_)?webpack_require(_[0-9]+)?__\.p)="/"#\1="/scratch/"#g' "$BUNDLE" ! grep -qE '__(nested_)?webpack_require(_[0-9]+)?__\.p="/"' "$BUNDLE" \ || { echo "publicPath rewrite incomplete" >&2; exit 1; } cat > "$STAGE/SOURCE.txt" <