integratedhub/site-build.sh
2026-01-20 09:02:26 -06:00

149 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PUBLISH_DIR="$ROOT_DIR/publish"
log() { echo "[site-build] $*"; }
fail() { echo "[site-build][ERROR] $*" >&2; exit 1; }
case "${1-}" in
build)
shift
;;
test)
shift
command -v deno >/dev/null 2>&1 || fail "deno is required but not found in PATH"
[[ -d "$PUBLISH_DIR" ]] || fail "publish directory not found: $PUBLISH_DIR"
cd "$PUBLISH_DIR"
bash -c 'set -a && source ../.env 2>/dev/null || true && set +a && exec ./server -A'
;;
publish)
shift
command -v zip >/dev/null 2>&1 || fail "zip is required but not found in PATH"
command -v az >/dev/null 2>&1 || fail "az is required but not found in PATH"
[[ -d "$PUBLISH_DIR" ]] || fail "publish directory not found: $PUBLISH_DIR"
rm -f "$PUBLISH_DIR/app.zip"
(cd "$PUBLISH_DIR" && zip -rq "./app.zip" .)
# az login
az webapp deploy --resource-group FrontB2CResources --name cyberhybridhub --src-path "$PUBLISH_DIR/app.zip" --type zip --clean true --restart
exit 0
;;
*)
# Default to build if no command specified
;;
esac
# Simple flag parsing
print_help() {
cat <<EOF
Usage: $(basename "$0") [command] [options]
Commands:
build Build the project (default)
test Test the built application
publish Deploy to Azure Web App
Options:
-t, --target <system> Target system for deno compile: linux (default), macos, windows
--help Show this help and exit
EOF
}
# Default target label and mapping to Deno target triple
TARGET_LABEL="linux"
DENO_TARGET="x86_64-unknown-linux-gnu"
while [[ $# -gt 0 ]]; do
case "$1" in
-t|--target)
[[ $# -lt 2 ]] && fail "--target requires a value: linux|macos|windows"
TARGET_LABEL="$(echo "$2" | tr '[:upper:]' '[:lower:]')"
shift 2
;;
--help)
print_help; exit 0 ;;
--)
shift; break ;;
*)
fail "Unknown option: $1 (use --help)" ;;
esac
done
# Map TARGET_LABEL to Deno compile target triple
case "$TARGET_LABEL" in
linux)
DENO_TARGET="x86_64-unknown-linux-gnu" ;;
macos|maxos|darwin|osx)
DENO_TARGET="x86_64-apple-darwin" ;;
windows|win)
DENO_TARGET="x86_64-pc-windows-msvc" ;;
*)
fail "Unsupported target '$TARGET_LABEL'. Use linux, macos, or windows" ;;
esac
command -v deno >/dev/null 2>&1 || fail "deno is required but not found in PATH"
log "Building Deno (Vite) client..."
pushd "$ROOT_DIR" >/dev/null
if deno task build; then
log "Deno Vite build completed"
else
fail "Deno Vite build failed"
fi
if [[ ! -d "_fresh/client" ]]; then
fail "_fresh/client not found. Ensure 'deno task build' completed successfully."
fi
[[ -f "_fresh/compiled-entry.js" ]] || fail "_fresh/compiled-entry.js not found in $ROOT_DIR"
rm -rf "$PUBLISH_DIR"
mkdir -p "$PUBLISH_DIR"
log "Compiling Deno server binary..."
if deno compile --allow-all --no-check --unstable-kv --unstable \
--target "$DENO_TARGET" \
--include ./_fresh/client \
--output $PUBLISH_DIR/server \
./_fresh/compiled-entry.js; then
log "Deno server compiled at $PUBLISH_DIR/server"
else
fail "deno compile failed"
fi
log "Copying artifacts into publish output..."
rm -rf "$PUBLISH_DIR/_fresh"
mkdir -p "$PUBLISH_DIR/_fresh"
cp -r "$ROOT_DIR/_fresh/client" "$PUBLISH_DIR/_fresh/"
cp -r "$ROOT_DIR/certs" "$PUBLISH_DIR/"
cp -r "$ROOT_DIR/data" "$PUBLISH_DIR/"
cp -r "$ROOT_DIR/static" "$PUBLISH_DIR/"
# Copy .env file if it exists
if [[ -f "$ROOT_DIR/azure_env" ]]; then
cp "$ROOT_DIR/azure_env" "$PUBLISH_DIR/.env"
log "Copied .env file to publish directory"
fi
# log "Copying JSON configs into publish output..."
# while IFS= read -r -d '' f; do
# rel="${f#"$ROOT_DIR/"}"
# dest_dir="$PUBLISH_DIR/$(dirname "$rel")"
# mkdir -p "$dest_dir"
# cp -f "$f" "$dest_dir/"
# done < <(
# find "$ROOT_DIR" \
# \( -type d \( -name node_modules -o -name _fresh \) -prune \) -o \
# \( -type f -name '*.json' -print0 \)
# )
# Make server executable
chmod +x "$PUBLISH_DIR/server" || true
log "Build complete. Outputs:"
log "- Deno server binary: $PUBLISH_DIR/server"
log "- Deno assets: $PUBLISH_DIR/_fresh/client"
log "- Publish directory: $PUBLISH_DIR"