build.sh 1.08 KB
#!/usr/bin/env bash

set -u
set -o pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$SCRIPT_DIR"
DIST_DIR="$PROJECT_DIR/dist"
DEPLOY_DIR="$PROJECT_DIR/../runing/html/dist"

echo "[1/3] Building frontend..."
if ! yarn build:ci; then
  echo "Build failed."
  exit 1
fi

if [[ ! -d "$DIST_DIR" ]]; then
  echo "Build output not found: \"$DIST_DIR\""
  exit 1
fi

echo "[2/3] Preparing deploy directory..."
if [[ -d "$DEPLOY_DIR" ]]; then
  rm -rf "$DEPLOY_DIR"
  if [[ -d "$DEPLOY_DIR" ]]; then
    echo "Failed to remove old deploy directory: \"$DEPLOY_DIR\""
    exit 1
  fi
fi

if ! mkdir -p "$DEPLOY_DIR"; then
  echo "Failed to create deploy directory: \"$DEPLOY_DIR\""
  exit 1
fi

echo "[3/3] Copying dist to deploy directory..."
if command -v rsync >/dev/null 2>&1; then
  if ! rsync -a "$DIST_DIR"/ "$DEPLOY_DIR"/; then
    echo "Deploy copy failed."
    exit 1
  fi
else
  if ! cp -a "$DIST_DIR"/. "$DEPLOY_DIR"/; then
    echo "Deploy copy failed."
    exit 1
  fi
fi

echo "Deploy completed."
echo "Source: \"$DIST_DIR\""
echo "Target: \"$DEPLOY_DIR\""
exit 0