Files
medusa-store/start.sh

115 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env sh
set -eu
# Colors (disabled if not a terminal)
if [ -t 1 ]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
else
RED='' GREEN='' YELLOW='' BLUE='' NC=''
fi
timestamp() {
date -u +%Y-%m-%dT%H:%M:%SZ
}
log_info() {
printf "💙 ${BLUE}[%s] [INFO]${NC} %s\n" "$(timestamp)" "$1"
}
log_success() {
printf "💚 ${GREEN}[%s] [SUCCESS]${NC} %s\n" "$(timestamp)" "$1"
}
log_error() {
printf "${RED}[%s] [ERROR]${NC} %s\n" "$(timestamp)" "$1" >&2
}
log_warn() {
printf "💛 ${YELLOW}[%s] [WARN]${NC} %s\n" "$(timestamp)" "$1"
}
cleanup() {
log_warn "Received shutdown signal, cleaning up..."
# Kill child processes
jobs -p | xargs kill 2>/dev/null || true
exit 130
}
trap cleanup INT TERM
# Pre-flight checks
log_info "Starting Medusa development environment..."
if find . -maxdepth 1 -mindepth 1 ! -uid "$UID" -print -quit | grep -q .; then
echo "ERROR: files not owned by UID=$UID exist in current directory. If running in DEVELOPER_MODE, fill UID and GID of your user in .env file and rebuild" >&2
find . -maxdepth 1 -mindepth 1 ! -uid "$UID" -ls >&2
exit 1
fi
if find . -maxdepth 1 -mindepth 1 ! -gid "$GID" -print -quit | grep -q .; then
echo "ERROR: files not owned by GID=$GID exist in current directory. If running in DEVELOPER_MODE, fill UID and GID of your user in .env file and rebuild" >&2
find . -maxdepth 1 -mindepth 1 ! -gid "$GID" -ls >&2
exit 1
fi
if [ ! -f "medusa-config.js" ]; then
log_error "medusa-config.js not found. Are you in the Medusa project root?"
exit 1
fi
if [ ! -d "node_modules" ]; then
log_error "node_modules/ not found. Run 'yarn install' first."
exit 1
fi
if [ -z "${DATABASE_URL:-}" ]; then
log_error "DATABASE_URL environment variable is not set"
exit 1
fi
# Database connectivity check (optional, can be skipped with --skip-db-check)
if [ "${1:-}" != "--skip-db-check" ]; then
log_info "Checking database connectivity..."
# Simple check using npx medusa (will fail if DB is unreachable)
if ! npx medusa db:check 2>/dev/null; then
log_warn "Database connectivity check failed (this is OK if db:check doesn't exist)"
else
log_success "Database is reachable"
fi
fi
# Run migrations
log_info "Running database migrations..."
start_time=$(date +%s)
if npx medusa db:migrate; then
migration_time=$(($(date +%s) - start_time))
log_success "Migrations completed in ${migration_time}s"
else
log_error "Migration failed"
exit 1
fi
# Create admin user if credentials are provided
if [ -n "${ADMIN_LOGIN:-}" ] && [ -n "${ADMIN_PASSWORD:-}" ]; then
log_info "Creating admin user with email: ${ADMIN_LOGIN}..."
if npx medusa user -e "${ADMIN_LOGIN}" -p "${ADMIN_PASSWORD}"; then
log_success "Admin user created successfully"
else
log_warn "Admin user creation failed (user may already exist)"
fi
elif [ -n "${ADMIN_LOGIN:-}" ] || [ -n "${ADMIN_PASSWORD:-}" ]; then
log_warn "Both ADMIN_LOGIN and ADMIN_PASSWORD must be set to create admin user"
fi
# Start development server
log_info "Starting Medusa development server..."
log_info "Press Ctrl+C to stop"
npx medusa develop