Skip to content

Commit 221b062

Browse files
Check faster
1 parent 73b690f commit 221b062

File tree

1 file changed

+52
-89
lines changed

1 file changed

+52
-89
lines changed

.device_scripts/start.sh

Lines changed: 52 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -27,110 +27,73 @@ cd "$SCRIPT_DIR/codequill" || {
2727
}
2828
print_step "Changed to directory: $(pwd)"
2929

30-
# Check if settings.json exists
31-
if [ -f "settings.json" ]; then
32-
# Extract port from settings.json
33-
PORT=$(grep -oP '"port":\s*\K\d+' settings.json)
34-
if [ -z "$PORT" ]; then
35-
PORT=1291
36-
print_step "No port specified in settings.json. Using default port: $PORT"
37-
else
38-
print_step "Using port from settings.json: $PORT"
39-
fi
40-
else
41-
PORT=1291
42-
print_step "settings.json not found. Using default port: $PORT"
43-
fi
30+
PORT=1291
4431

45-
# Function to check if a port is in use
46-
check_port() {
47-
nc -z localhost $1 >/dev/null 2>&1
32+
# Function to get local IP address
33+
get_local_ip() {
34+
hostname -I | awk '{print $1}'
4835
}
4936

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
37+
# Function to check if CodeQuill is running on another device (parallelized)
38+
check_remote_codequill() {
39+
local network_prefix=$(echo $1 | cut -d. -f1-3)
40+
local ip_list=($(seq 1 254 | xargs -I {} echo "${network_prefix}.{}"))
41+
local -a pids=()
42+
43+
for ip in "${ip_list[@]}"; do
44+
if [ "$ip" != "$1" ]; then
45+
(nc -z -w 1 $ip $PORT && echo $ip) &
46+
pids+=($!)
5847
fi
5948
done
60-
return 1
49+
50+
for pid in "${pids[@]}"; do
51+
wait $pid
52+
done | head -n 1
6153
}
6254

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
55+
LOCAL_IP=$(get_local_ip)
56+
REMOTE_IP=$(check_remote_codequill $LOCAL_IP)
6957

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
58+
if [ -n "$REMOTE_IP" ]; then
59+
print_step "CodeQuill is already running on $REMOTE_IP:$PORT"
60+
ELECTRON_START_URL=http://$REMOTE_IP:$PORT npm run electron -- --no-sandbox
61+
else
62+
# Check if .next directory exists
63+
if [ ! -d ".next" ]; then
64+
print_step "The .next directory is missing. Running 'next build'..."
65+
npm run build
66+
if [ $? -ne 0 ]; then
67+
print_color "31" "❌ Build failed. Please check for errors and try again."
68+
exit 1
8169
fi
82-
) &
83-
done
84-
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)
88-
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
93-
fi
94-
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
70+
print_color "32" "✅ Build completed successfully."
71+
else
72+
print_step ".next directory found. Skipping build."
10573
fi
106-
print_color "32" "✅ Build completed successfully."
107-
else
108-
print_step ".next directory found. Skipping build."
109-
fi
11074

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=$!
75+
# Start the Next.js server in the background
76+
print_step "Starting Next.js server on port $PORT..."
77+
npm run start -- -p $PORT &
11578

116-
# Function to check if the server is ready
117-
check_server() {
118-
curl -s http://localhost:$PORT >/dev/null
119-
return $?
120-
}
79+
# Function to check if the server is ready
80+
check_server() {
81+
curl -s http://localhost:$PORT >/dev/null
82+
return $?
83+
}
12184

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!"
85+
# Wait for the server to be ready
86+
print_step "Waiting for Next.js server to be ready..."
87+
while ! check_server; do
88+
sleep 1
89+
done
90+
print_color "32" "✅ Next.js server is ready!"
12891

129-
# Start Electron
130-
print_step "Starting Electron..."
131-
ELECTRON_START_URL=http://localhost:$PORT npm run electron -- --no-sandbox
92+
# Start Electron
93+
print_step "Starting Electron..."
94+
ELECTRON_START_URL=http://$LOCAL_IP:$PORT npm run electron -- --no-sandbox
95+
fi
13296

133-
# Electron has exited, perform any necessary cleanup
13497
print_color "35" "
13598
==========================
13699
👋 CodeQuill has stopped 👋

0 commit comments

Comments
 (0)