Created
November 8, 2024 15:48
-
-
Save markshust/6b93884c59f74a904ab7aa29fd668c37 to your computer and use it in GitHub Desktop.
Helper script to start Laravel with Sail, npm and ngrok.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to cleanup processes on script exit | |
cleanup() { | |
echo "Cleaning up..." | |
./vendor/bin/sail down | |
kill "$(jobs -p)" 2>/dev/null | |
exit | |
} | |
# Function to wait for sail to be ready | |
wait_for_sail() { | |
echo "Waiting for Laravel Sail to be ready..." | |
local max_attempts=30 | |
local attempt=1 | |
while ! ./vendor/bin/sail exec laravel.test curl -s http://localhost > /dev/null; do | |
if [ $attempt -eq $max_attempts ]; then | |
echo "Timeout waiting for Sail to be ready" | |
exit 1 | |
fi | |
echo "Waiting for Sail... attempt $attempt/$max_attempts" | |
attempt=$((attempt + 1)) | |
sleep 1 | |
done | |
echo "Sail is ready!" | |
} | |
# Function to wait for Vite to be ready | |
wait_for_vite() { | |
echo "Waiting for Vite to be ready..." | |
local max_attempts=30 | |
local attempt=1 | |
while ! curl -s http://localhost:5173 > /dev/null; do | |
if [ $attempt -eq $max_attempts ]; then | |
echo "Timeout waiting for Vite to be ready" | |
exit 1 | |
fi | |
echo "Waiting for Vite... attempt $attempt/$max_attempts" | |
attempt=$((attempt + 1)) | |
sleep 1 | |
done | |
echo "Vite is ready!" | |
} | |
# Set up trap for cleanup on script termination | |
trap cleanup EXIT INT TERM | |
# Start Laravel Sail in detached mode | |
echo "Starting Laravel Sail..." | |
./vendor/bin/sail up -d | |
# Wait for Sail to be ready | |
wait_for_sail | |
# Start npm development server by executing it inside the container | |
echo "Starting npm dev server..." | |
./vendor/bin/sail exec -T laravel.test npm run dev || exit 1 & | |
# Wait for Vite to be ready | |
wait_for_vite | |
# Start ngrok on all ports (this will run in foreground) | |
echo "Starting ngrok..." | |
ngrok start --all |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment