Skip to content

Commit 4d87165

Browse files
Update start script
1 parent 76dcaba commit 4d87165

File tree

1 file changed

+81
-80
lines changed

1 file changed

+81
-80
lines changed

.device_scripts/start.sh

Lines changed: 81 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,6 @@ print_step() {
1212
print_color "36" "\n📌 $1"
1313
}
1414

15-
# Function to cleanup and exit
16-
cleanup_and_exit() {
17-
print_color "33" "\n\n🛑 Stopping CodeQuill..."
18-
19-
if [ -n "$NEXT_PID" ]; then
20-
if kill -0 $NEXT_PID 2>/dev/null; then
21-
kill $NEXT_PID
22-
wait $NEXT_PID 2>/dev/null
23-
print_color "32" "✅ Next.js server stopped"
24-
else
25-
print_color "33" "⚠️ Next.js server was not running"
26-
fi
27-
else
28-
print_color "33" "⚠️ Next.js server PID not set"
29-
fi
30-
31-
print_color "35" "
32-
==========================
33-
👋 CodeQuill has stopped 👋
34-
==========================
35-
"
36-
exit 0
37-
}
38-
39-
# Trap Ctrl+C and other termination signals
40-
trap cleanup_and_exit SIGINT SIGTERM
41-
4215
# Start message
4316
print_color "35" "
4417
==========================
@@ -69,69 +42,97 @@ else
6942
print_step "settings.json not found. Using default port: $PORT"
7043
fi
7144

72-
# Function to get local IP address
73-
get_local_ip() {
74-
local_ip=$(hostname -I | awk '{print $1}')
75-
echo $local_ip
45+
# Function to check if a port is in use
46+
check_port() {
47+
nc -z localhost $1 >/dev/null 2>&1
7648
}
7749

78-
# Function to check if CodeQuill is running on another device
79-
check_remote_codequill() {
80-
local network_prefix=$(echo $1 | cut -d. -f1-3)
81-
for i in {1..254}; do
82-
ip="${network_prefix}.${i}"
83-
if [ "$ip" != "$1" ]; then
84-
if nc -z -w 1 $ip $PORT 2>/dev/null; then
85-
echo $ip
86-
return 0
87-
fi
50+
# Function to find an available port
51+
find_available_port() {
52+
local start_port=$1
53+
local end_port=$2
54+
for port in $(seq $start_port $end_port); do
55+
if ! check_port $port; then
56+
echo $port
57+
return 0
8858
fi
8959
done
9060
return 1
9161
}
9262

93-
LOCAL_IP=$(get_local_ip)
94-
REMOTE_IP=$(check_remote_codequill $LOCAL_IP)
63+
# Check if the server is already running on this device
64+
if check_port $PORT; then
65+
print_step "CodeQuill is already running on localhost:$PORT"
66+
ELECTRON_START_URL=http://localhost:$PORT npm run electron -- --no-sandbox
67+
exit 0
68+
fi
9569

96-
if [ -n "$REMOTE_IP" ]; then
97-
print_step "CodeQuill is already running on $REMOTE_IP:$PORT"
98-
ELECTRON_START_URL=http://$REMOTE_IP:$PORT npm run electron -- --no-sandbox
99-
else
100-
# Check if .next directory exists
101-
if [ ! -d ".next" ]; then
102-
print_step "The .next directory is missing. Running 'next build'..."
103-
npm run build
104-
if [ $? -ne 0 ]; then
105-
print_color "31" "❌ Build failed. Please check for errors and try again."
106-
exit 1
70+
# Parallel port scanning
71+
print_step "Scanning for running CodeQuill instances..."
72+
FOUND_PORT=""
73+
PORT_RANGE_START=1291
74+
PORT_RANGE_END=1300
75+
76+
for port in $(seq $PORT_RANGE_START $PORT_RANGE_END); do
77+
(
78+
if check_port $port; then
79+
echo $port
80+
kill -SIGINT $$ # Send interrupt signal to the main script
10781
fi
108-
print_color "32" "✅ Build completed successfully."
109-
else
110-
print_step ".next directory found. Skipping build."
111-
fi
82+
) &
83+
done
11284

113-
# Start the Next.js server in the background
114-
print_step "Starting Next.js server on port $PORT..."
115-
npm run start -- -p $PORT &
116-
NEXT_PID=$!
117-
118-
# Function to check if the server is ready
119-
check_server() {
120-
curl -s http://localhost:$PORT >/dev/null
121-
return $?
122-
}
123-
124-
# Wait for the server to be ready
125-
print_step "Waiting for Next.js server to be ready..."
126-
while ! check_server; do
127-
sleep 1
128-
done
129-
print_color "32" "✅ Next.js server is ready!"
85+
# Wait for any child process to finish or timeout after 5 seconds
86+
timeout 5s wait
87+
FOUND_PORT=$(find_available_port $PORT_RANGE_START $PORT_RANGE_END)
13088

131-
# Start Electron
132-
print_step "Starting Electron..."
133-
ELECTRON_START_URL=http://$LOCAL_IP:$PORT npm run electron -- --no-sandbox
89+
if [ -n "$FOUND_PORT" ]; then
90+
print_step "CodeQuill is already running on localhost:$FOUND_PORT"
91+
ELECTRON_START_URL=http://localhost:$FOUND_PORT npm run electron -- --no-sandbox
92+
exit 0
13493
fi
13594

136-
# Electron has exited, so we can clean up
137-
cleanup_and_exit
95+
# If no running instance found, start the server locally
96+
print_step "No running CodeQuill instance found. Starting server locally..."
97+
98+
# Check if .next directory exists
99+
if [ ! -d ".next" ]; then
100+
print_step "The .next directory is missing. Running 'next build'..."
101+
npm run build
102+
if [ $? -ne 0 ]; then
103+
print_color "31" "❌ Build failed. Please check for errors and try again."
104+
exit 1
105+
fi
106+
print_color "32" "✅ Build completed successfully."
107+
else
108+
print_step ".next directory found. Skipping build."
109+
fi
110+
111+
# Start the Next.js server in the background
112+
print_step "Starting Next.js server on port $PORT..."
113+
npm run start -- -p $PORT &
114+
NEXT_PID=$!
115+
116+
# Function to check if the server is ready
117+
check_server() {
118+
curl -s http://localhost:$PORT >/dev/null
119+
return $?
120+
}
121+
122+
# Wait for the server to be ready
123+
print_step "Waiting for Next.js server to be ready..."
124+
while ! check_server; do
125+
sleep 1
126+
done
127+
print_color "32" "✅ Next.js server is ready!"
128+
129+
# Start Electron
130+
print_step "Starting Electron..."
131+
ELECTRON_START_URL=http://localhost:$PORT npm run electron -- --no-sandbox
132+
133+
# Electron has exited, perform any necessary cleanup
134+
print_color "35" "
135+
==========================
136+
👋 CodeQuill has stopped 👋
137+
==========================
138+
"

0 commit comments

Comments
 (0)