Files
mtc-project-sync-script/watch-core-build.sh

87 lines
2.2 KiB
Bash
Raw Normal View History

# Watch and upload script for core assets
# Install: brew install fswatch
# This script monitors the public folder for changes, rebuilds assets, and uploads them to the server
set -e
# Local paths
LOCAL_PUBLIC="./public"
LOCAL_MANIFEST="./mix-manifest.json"
# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Trap to kill background processes on script exit
trap cleanup EXIT INT TERM
cleanup() {
echo "${YELLOW}Cleaning up...${NC}"
if [ ! -z "$BUN_PID" ]; then
echo "Killing bun watch process..."
kill $BUN_PID 2>/dev/null || true
wait $BUN_PID 2>/dev/null || true
fi
if [ ! -z "$RSYNC_PID" ]; then
kill $RSYNC_PID 2>/dev/null || true
fi
echo "${GREEN}Cleanup complete${NC}"
}
# Function to upload changes
upload_changes() {
echo "${BLUE}=== Starting upload cycle ===${NC}"
# Sync public folder (js, css, images, etc.)
echo "Syncing public folder..."
sshpass -p "$SERVER_PASSWORD" rsync -avz --progress \
--exclude 'storage' \
--exclude '.htaccess' \
"$LOCAL_PUBLIC/" "$SERVER_USER@$SERVER_HOST:$SERVER_PATH/"
# Upload mix-manifest.json specifically
echo "Uploading mix-manifest.json..."
sshpass -p "$SERVER_PASSWORD" scp "$LOCAL_MANIFEST" \
"$SERVER_USER@$SERVER_HOST:$SERVER_PATH/mix-manifest.json"
echo "${GREEN}Upload complete!${NC}"
}
# Clear screen
clear
# Start bun run watch in background
echo "${BLUE}Starting bun watch...${NC}"
bun run watch &
BUN_PID=$!
echo "${GREEN}Bun watch started (PID: $BUN_PID)${NC}"
echo ""
# Wait a moment for the initial build
sleep 2
# Initial upload
echo "${BLUE}Performing initial upload...${NC}"
upload_changes
echo ""
# Watch for changes in public folder
echo "${BLUE}Watching for changes in $LOCAL_PUBLIC...${NC}"
echo "Press Ctrl+C to stop watching and exit"
echo ""
fswatch -o "$LOCAL_PUBLIC" | while read num; do
# Kill previous pending upload
if [ ! -z "$UPLOAD_PID" ]; then
kill $UPLOAD_PID 2>/dev/null || true
wait $UPLOAD_PID 2>/dev/null || true
fi
# Schedule upload after 0.5 seconds of no changes
(sleep 0.5 && upload_changes) &
UPLOAD_PID=$!
done