aboutsummaryrefslogtreecommitdiffstats
path: root/start-dev.sh
blob: 0f5f9d2ee2bcb7ea3db69c8e0a8787f454dcbec3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash

# Function to check if a command exists
command_exists() {
    command -v "$1" >/dev/null 2>&1
}

# Function to check if a port is in use
port_in_use() {
    lsof -i :"$1" >/dev/null 2>&1
}

# Check if Docker is installed
if ! command_exists docker; then
    echo "Error: Docker is not installed. Please install Docker first."
    exit 1
fi

# Check if pnpm is installed
if ! command_exists pnpm; then
    echo "Error: pnpm is not installed. Please install pnpm first."
    exit 1
fi

# Start Meilisearch if not already running
if ! port_in_use 7700; then
    echo "Starting Meilisearch..."
    docker run -d -p 7700:7700 --name karakeep-meilisearch getmeili/meilisearch:v1.13.3
else
    echo "Meilisearch is already running on port 7700"
fi

# Start Chrome if not already running
if ! port_in_use 9222; then
    echo "Starting headless Chrome..."
    docker run -d -p 9222:9222 --name karakeep-chrome gcr.io/zenika-hub/alpine-chrome:123 \
        --no-sandbox \
        --disable-gpu \
        --disable-dev-shm-usage \
        --remote-debugging-address=0.0.0.0 \
        --remote-debugging-port=9222 \
        --hide-scrollbars
else
    echo "Chrome is already running on port 9222"
fi

# Install dependencies if node_modules doesn't exist
if [ ! -d "node_modules" ]; then
    echo "Installing dependencies..."
    pnpm install
fi

# Get DATA_DIR from environment or .env file
if [ -z "$DATA_DIR" ] && [ -f ".env" ]; then
    DATA_DIR=$(grep "^DATA_DIR=" .env | cut -d'=' -f2)
fi

# Create DATA_DIR if it doesn't exist
if [ -n "$DATA_DIR" ] && [ ! -d "$DATA_DIR" ]; then
    echo "Creating DATA_DIR at $DATA_DIR..."
    mkdir -p "$DATA_DIR"
fi

# Start the web app
echo "Starting web app..."
pnpm web & WEB_PID=$!

# Wait for web app to be ready
echo "Waiting for web app to start..."
until curl -s http://localhost:3000 > /dev/null 2>&1; do
    sleep 1
done

# Run database migrations
echo "Running database migrations..."
pnpm run db:migrate

# Start workers
echo "Starting workers..."
pnpm workers & WORKERS_PID=$!

# Function to handle script termination
cleanup() {
    echo "Shutting down services..."
    kill $WEB_PID $WORKERS_PID 2>/dev/null
    docker stop karakeep-meilisearch karakeep-chrome 2>/dev/null
    docker rm karakeep-meilisearch karakeep-chrome 2>/dev/null
    exit 0
}

# Set up trap to catch termination signals
trap cleanup SIGINT SIGTERM

echo "Development environment is running!"
echo "Web app: http://localhost:3000"
echo "Meilisearch: http://localhost:7700"
echo "Chrome debugger: http://localhost:9222"
echo "Press Ctrl+C to stop all services"

# Wait for user interrupt
wait