-
Notifications
You must be signed in to change notification settings - Fork 578
Expand file tree
/
Copy pathstart-automaker.sh
More file actions
executable file
·1464 lines (1283 loc) · 53.3 KB
/
start-automaker.sh
File metadata and controls
executable file
·1464 lines (1283 loc) · 53.3 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Automaker TUI Launcher - Interactive menu for launching Automaker in different modes
# Supports: Web Browser, Desktop (Electron), Docker Dev, Electron + Docker API
# Platforms: Linux, macOS, Windows (Git Bash, WSL, MSYS2, Cygwin)
# Features: Terminal responsiveness, history, pre-flight checks, port management
set -e
# ============================================================================
# CONFIGURATION & CONSTANTS
# ============================================================================
if [ -f .env ]; then
set -a
. ./.env
set +a
fi
APP_NAME="Automaker"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HISTORY_FILE="${HOME}/.automaker_launcher_history"
MIN_TERM_WIDTH=70
MIN_TERM_HEIGHT=20
MENU_BOX_WIDTH=66
MENU_INNER_WIDTH=64
LOGO_WIDTH=52
INPUT_TIMEOUT=30
SELECTED_OPTION=1
MAX_OPTIONS=4
# Platform detection (set early for cross-platform compatibility)
IS_WINDOWS=false
IS_MACOS=false
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" || "$OSTYPE" == "mingw"* ]]; then
IS_WINDOWS=true
elif [[ "$OSTYPE" == "darwin"* ]]; then
IS_MACOS=true
fi
# Port configuration
# Defaults can be overridden via AUTOMAKER_WEB_PORT and AUTOMAKER_SERVER_PORT env vars
# Validate env-provided ports early (before colors are available)
if [ -n "$AUTOMAKER_WEB_PORT" ]; then
if ! [[ "$AUTOMAKER_WEB_PORT" =~ ^[0-9]+$ ]] || [ "$AUTOMAKER_WEB_PORT" -lt 1 ] || [ "$AUTOMAKER_WEB_PORT" -gt 65535 ]; then
echo "Error: AUTOMAKER_WEB_PORT must be a number between 1-65535, got '$AUTOMAKER_WEB_PORT'"
exit 1
fi
fi
if [ -n "$AUTOMAKER_SERVER_PORT" ]; then
if ! [[ "$AUTOMAKER_SERVER_PORT" =~ ^[0-9]+$ ]] || [ "$AUTOMAKER_SERVER_PORT" -lt 1 ] || [ "$AUTOMAKER_SERVER_PORT" -gt 65535 ]; then
echo "Error: AUTOMAKER_SERVER_PORT must be a number between 1-65535, got '$AUTOMAKER_SERVER_PORT'"
exit 1
fi
fi
DEFAULT_WEB_PORT=${AUTOMAKER_WEB_PORT:-3007}
DEFAULT_SERVER_PORT=${AUTOMAKER_SERVER_PORT:-3008}
PORT_SEARCH_MAX_ATTEMPTS=100
WEB_PORT=$DEFAULT_WEB_PORT
SERVER_PORT=$DEFAULT_SERVER_PORT
TEST_WEB_PORT=${TEST_PORT:-3107}
TEST_SERVER_PORT=${TEST_SERVER_PORT:-3108}
# Port validation function
# Returns 0 if valid, 1 if invalid (with error message printed)
validate_port() {
local port="$1"
local port_name="${2:-port}"
# Check if port is a number
if ! [[ "$port" =~ ^[0-9]+$ ]]; then
echo "${C_RED}Error:${RESET} $port_name must be a number, got '$port'"
return 1
fi
# Check if port is in valid range (1-65535)
if [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then
echo "${C_RED}Error:${RESET} $port_name must be between 1-65535, got '$port'"
return 1
fi
# Check if port is in privileged range (warning only)
if [ "$port" -lt 1024 ]; then
echo "${C_YELLOW}Warning:${RESET} $port_name $port is in privileged range (requires root/admin)"
fi
return 0
}
# Hostname configuration
# Use VITE_HOSTNAME if explicitly set, otherwise default to localhost
# Note: Don't use $HOSTNAME as it's a bash built-in containing the machine's hostname
APP_HOST="${VITE_HOSTNAME:-localhost}"
# Extract VERSION from apps/ui/package.json (the actual app version, not monorepo version)
if command -v node &> /dev/null; then
VERSION="v$(node -p "require('$SCRIPT_DIR/apps/ui/package.json').version" 2>/dev/null || echo "0.11.0")"
else
VERSION=$(grep '"version"' "$SCRIPT_DIR/apps/ui/package.json" 2>/dev/null | head -1 | sed 's/.*"version"[^"]*"\([^"]*\)".*/v\1/' || echo "v0.11.0")
fi
# ANSI Color codes (256-color palette)
ESC=$(printf '\033')
RESET="${ESC}[0m"
BOLD="${ESC}[1m"
DIM="${ESC}[2m"
C_PRI="${ESC}[38;5;51m" # Primary cyan
C_SEC="${ESC}[38;5;39m" # Secondary blue
C_ACC="${ESC}[38;5;33m" # Accent darker blue
C_GREEN="${ESC}[38;5;118m" # Green
C_RED="${ESC}[38;5;196m" # Red
C_YELLOW="${ESC}[38;5;226m" # Yellow
C_GRAY="${ESC}[38;5;240m" # Dark gray
C_WHITE="${ESC}[38;5;255m" # White
C_MUTE="${ESC}[38;5;248m" # Muted gray
# ============================================================================
# ARGUMENT PARSING
# ============================================================================
MODE=""
USE_COLORS=true
CHECK_DEPS=false
NO_HISTORY=false
PRODUCTION_MODE=false
show_help() {
cat << 'EOF'
Automaker TUI Launcher - Interactive development environment starter
USAGE:
start-automaker.sh [MODE] [OPTIONS]
MODES:
web Launch in web browser (localhost:3007)
electron Launch as desktop app (Electron)
docker Launch in Docker container (dev with live reload)
docker-electron Launch Electron with Docker API backend
OPTIONS:
--help Show this help message
--version Show version information
--no-colors Disable colored output
--check-deps Check dependencies before launching
--no-history Don't remember last choice
--production Run in production mode (builds first, faster React)
EXAMPLES:
start-automaker.sh # Interactive menu (development)
start-automaker.sh --production # Interactive menu (production)
start-automaker.sh web # Launch web mode directly (dev)
start-automaker.sh web --production # Launch web mode (production)
start-automaker.sh electron # Launch desktop app directly
start-automaker.sh docker # Launch Docker dev container
start-automaker.sh --version # Show version
AUTOMAKER_WEB_PORT=4000 AUTOMAKER_SERVER_PORT=4001 start-automaker.sh web
# Launch web mode on custom ports
KEYBOARD SHORTCUTS (in menu):
Up/Down arrows Navigate between options
Enter Select highlighted option
1-4 Jump to and select mode
Q Exit
HISTORY:
Your last selected mode is remembered in: ~/.automaker_launcher_history
Use --no-history to disable this feature
ENVIRONMENT VARIABLES:
AUTOMAKER_WEB_PORT Override default web/UI port (default: 3007)
AUTOMAKER_SERVER_PORT Override default API server port (default: 3008)
PLATFORMS:
Linux, macOS, Windows (Git Bash, WSL, MSYS2, Cygwin)
EOF
}
show_version() {
echo "Automaker Launcher $VERSION"
echo "Node.js: $(node -v 2>/dev/null || echo 'not installed')"
echo "Bash: ${BASH_VERSION%.*}"
}
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--help)
show_help
exit 0
;;
--version)
show_version
exit 0
;;
--no-colors)
USE_COLORS=false
RESET=""
C_PRI="" C_SEC="" C_ACC="" C_GREEN="" C_RED="" C_YELLOW="" C_GRAY="" C_WHITE="" C_MUTE=""
;;
--check-deps)
CHECK_DEPS=true
;;
--no-history)
NO_HISTORY=true
;;
--production)
PRODUCTION_MODE=true
;;
web|electron|docker|docker-electron)
MODE="$1"
;;
*)
echo "Unknown option: $1" >&2
echo "Use --help for usage information" >&2
exit 1
;;
esac
shift
done
}
# ============================================================================
# PRE-FLIGHT CHECKS
# ============================================================================
check_platform() {
# Platform already detected at script start
# This function is kept for any additional platform-specific checks
if [ "$IS_WINDOWS" = true ]; then
# Check if running in a proper terminal
if [ -z "$TERM" ]; then
echo "${C_YELLOW}Warning:${RESET} Running on Windows without proper terminal."
echo "For best experience, use Git Bash, WSL, or Windows Terminal."
fi
fi
}
check_required_commands() {
local missing=()
# Check for required commands
for cmd in node npm tput; do
if ! command -v "$cmd" &> /dev/null; then
missing+=("$cmd")
fi
done
if [ ${#missing[@]} -gt 0 ]; then
echo "${C_RED}Error:${RESET} Missing required commands: ${missing[*]}"
echo ""
echo "Please install:"
for cmd in "${missing[@]}"; do
case "$cmd" in
node|npm) echo " - Node.js (includes npm) from https://nodejs.org/" ;;
tput) echo " - ncurses package (usually pre-installed on Unix systems)" ;;
esac
done
exit 1
fi
}
DOCKER_CMD="docker"
check_docker() {
if ! command -v docker &> /dev/null; then
echo "${C_RED}Error:${RESET} Docker is not installed or not in PATH"
echo "Please install Docker from https://docs.docker.com/get-docker/"
return 1
fi
if ! docker info &> /dev/null 2>&1; then
if sg docker -c "docker info" &> /dev/null 2>&1; then
DOCKER_CMD="sg docker -c"
else
echo "${C_RED}Error:${RESET} Docker daemon is not running or not accessible"
echo ""
echo "To fix, run:"
echo " sudo usermod -aG docker \$USER"
echo ""
echo "Then either log out and back in, or run:"
echo " newgrp docker"
return 1
fi
fi
export DOCKER_CMD
return 0
}
check_running_electron() {
local electron_pids=""
if [ "$IS_WINDOWS" = true ]; then
# Windows: look for electron.exe or Automaker.exe
electron_pids=$(tasklist 2>/dev/null | grep -iE "electron|automaker" | awk '{print $2}' | tr '\n' ' ' || true)
else
# Unix: look for electron or Automaker processes
electron_pids=$(pgrep -f "electron.*automaker|Automaker" 2>/dev/null | tr '\n' ' ' || true)
fi
if [ -n "$electron_pids" ] && [ "$electron_pids" != " " ]; then
get_term_size
echo ""
center_print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" "$C_GRAY"
center_print "Running Electron App Detected" "$C_YELLOW"
center_print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" "$C_GRAY"
echo ""
center_print "Electron process(es): $electron_pids" "$C_MUTE"
echo ""
center_print "What would you like to do?" "$C_WHITE"
echo ""
center_print "[K] Kill Electron and continue" "$C_GREEN"
center_print "[I] Ignore and continue anyway" "$C_MUTE"
center_print "[C] Cancel" "$C_RED"
echo ""
while true; do
local choice_pad=$(( (TERM_COLS - 20) / 2 ))
printf "%${choice_pad}s" ""
read -r -p "Choice: " choice
case "$choice" in
[kK]|[kK][iI][lL][lL])
echo ""
center_print "Killing Electron processes..." "$C_YELLOW"
if [ "$IS_WINDOWS" = true ]; then
taskkill //F //IM "electron.exe" 2>/dev/null || true
taskkill //F //IM "Automaker.exe" 2>/dev/null || true
else
pkill -f "electron.*automaker" 2>/dev/null || true
pkill -f "Automaker" 2>/dev/null || true
fi
sleep 1
center_print "✓ Electron stopped" "$C_GREEN"
echo ""
return 0
;;
[iI]|[iI][gG][nN][oO][rR][eE])
echo ""
center_print "Continuing without stopping Electron..." "$C_MUTE"
echo ""
return 0
;;
[cC]|[cC][aA][nN][cC][eE][lL])
echo ""
center_print "Cancelled." "$C_MUTE"
echo ""
exit 0
;;
*)
center_print "Invalid choice. Please enter K, I, or C." "$C_RED"
;;
esac
done
fi
return 0
}
check_running_containers() {
local compose_file="$1"
local running_containers=""
# Get list of running automaker containers
if [ "$DOCKER_CMD" = "sg docker -c" ]; then
running_containers=$(sg docker -c "docker ps --filter 'name=automaker-dev' --format '{{{{Names}}}}'" 2>/dev/null | tr '\n' ' ' || true)
else
running_containers=$($DOCKER_CMD ps --filter "name=automaker-dev" --format "{{.Names}}" 2>/dev/null | tr '\n' ' ' || true)
fi
if [ -n "$running_containers" ] && [ "$running_containers" != " " ]; then
get_term_size
echo ""
center_print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" "$C_GRAY"
center_print "Existing Containers Detected" "$C_YELLOW"
center_print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" "$C_GRAY"
echo ""
center_print "Running containers: $running_containers" "$C_MUTE"
echo ""
center_print "What would you like to do?" "$C_WHITE"
echo ""
center_print "[S] Stop containers and start fresh" "$C_GREEN"
center_print "[R] Restart containers (rebuild)" "$C_MUTE"
center_print "[A] Attach to existing containers" "$C_MUTE"
center_print "[C] Cancel" "$C_RED"
echo ""
while true; do
local choice_pad=$(( (TERM_COLS - 20) / 2 ))
printf "%${choice_pad}s" ""
read -r -p "Choice: " choice
case "$choice" in
[sS]|[sS][tT][oO][pP])
echo ""
center_print "Stopping existing containers..." "$C_YELLOW"
if [ "$DOCKER_CMD" = "sg docker -c" ]; then
sg docker -c "docker compose -f '$compose_file' down" 2>/dev/null || true
sg docker -c "docker ps --filter 'name=automaker-dev' -q" 2>/dev/null | xargs -r sg docker -c "docker stop" 2>/dev/null || true
else
$DOCKER_CMD compose -f "$compose_file" down 2>/dev/null || true
$DOCKER_CMD ps --filter "name=automaker-dev" -q 2>/dev/null | xargs -r $DOCKER_CMD stop 2>/dev/null || true
fi
center_print "✓ Containers stopped" "$C_GREEN"
echo ""
return 0 # Continue with fresh start
;;
[rR]|[rR][eE][sS][tT][aA][rR][tT])
echo ""
center_print "Stopping and rebuilding containers..." "$C_YELLOW"
if [ "$DOCKER_CMD" = "sg docker -c" ]; then
sg docker -c "docker compose -f '$compose_file' down" 2>/dev/null || true
else
$DOCKER_CMD compose -f "$compose_file" down 2>/dev/null || true
fi
center_print "✓ Ready to rebuild" "$C_GREEN"
echo ""
return 0 # Continue with rebuild
;;
[aA]|[aA][tT][tT][aA][cC][hH])
echo ""
center_print "Attaching to existing containers..." "$C_GREEN"
echo ""
return 2 # Special code for attach
;;
[cC]|[cC][aA][nN][cC][eE][lL])
echo ""
center_print "Cancelled." "$C_MUTE"
echo ""
exit 0
;;
*)
center_print "Invalid choice. Please enter S, R, A, or C." "$C_RED"
;;
esac
done
fi
return 0 # No containers running, continue normally
}
check_dependencies() {
if [ "$CHECK_DEPS" = false ]; then
return 0
fi
echo "${C_MUTE}Checking project dependencies...${RESET}"
if [ ! -d "node_modules" ]; then
echo "${C_YELLOW}⚠${RESET} node_modules not found. Run 'npm install' before launching."
return 1
fi
if [ ! -f "package-lock.json" ]; then
echo "${C_YELLOW}⚠${RESET} package-lock.json not found."
fi
return 0
}
# ============================================================================
# PORT MANAGEMENT (Cross-platform)
# ============================================================================
get_pids_on_port() {
local port=$1
if [ "$IS_WINDOWS" = true ]; then
# Windows: use netstat
netstat -ano 2>/dev/null | grep ":$port " | grep "LISTENING" | awk '{print $5}' | sort -u | tr '\n' ' ' || true
else
# Unix: use lsof
lsof -ti:"$port" 2>/dev/null || true
fi
}
is_port_in_use() {
local port=$1
local pids
pids=$(get_pids_on_port "$port")
[ -n "$pids" ] && [ "$pids" != " " ]
}
# Find the next available port starting from a given port
# Returns the port on stdout if found, nothing if all ports in range are busy
# Exit code: 0 if found, 1 if no available port in range
find_next_available_port() {
local start_port=$1
local port=$start_port
for ((i=0; i<PORT_SEARCH_MAX_ATTEMPTS; i++)); do
if ! is_port_in_use "$port"; then
echo "$port"
return 0
fi
port=$((port + 1))
done
# No free port found in the scan range
return 1
}
kill_port() {
local port=$1
local pids
pids=$(get_pids_on_port "$port")
if [ -z "$pids" ] || [ "$pids" = " " ]; then
echo "${C_GREEN}✓${RESET} Port $port is available"
return 0
fi
echo "${C_YELLOW}Killing process(es) on port $port: $pids${RESET}"
if [ "$IS_WINDOWS" = true ]; then
# Windows: use taskkill
for pid in $pids; do
taskkill //F //PID "$pid" 2>/dev/null || true
done
else
# Unix: use kill
echo "$pids" | xargs kill -9 2>/dev/null || true
fi
# Wait for port to be freed
local i=0
while [ $i -lt 10 ]; do
sleep 0.5 2>/dev/null || sleep 1
if ! is_port_in_use "$port"; then
echo "${C_GREEN}✓${RESET} Port $port is now free"
return 0
fi
i=$((i + 1))
done
echo "${C_RED}Warning:${RESET} Port $port may still be in use"
return 1
}
check_ports() {
# Auto-discover available ports (no user interaction required)
local web_in_use=false
local server_in_use=false
if is_port_in_use "$DEFAULT_WEB_PORT"; then
web_in_use=true
fi
if is_port_in_use "$DEFAULT_SERVER_PORT"; then
server_in_use=true
fi
if [ "$web_in_use" = true ] || [ "$server_in_use" = true ]; then
echo ""
local max_port
if [ "$web_in_use" = true ]; then
local pids
# Get PIDs and convert newlines to spaces for display
pids=$(get_pids_on_port "$DEFAULT_WEB_PORT" | xargs)
echo "${C_YELLOW}Port $DEFAULT_WEB_PORT in use (PID: $pids), finding alternative...${RESET}"
max_port=$((DEFAULT_WEB_PORT + PORT_SEARCH_MAX_ATTEMPTS - 1))
if ! WEB_PORT=$(find_next_available_port "$DEFAULT_WEB_PORT"); then
echo "${C_RED}Error: No free web port in range ${DEFAULT_WEB_PORT}-${max_port}${RESET}"
exit 1
fi
fi
if [ "$server_in_use" = true ]; then
local pids
# Get PIDs and convert newlines to spaces for display
pids=$(get_pids_on_port "$DEFAULT_SERVER_PORT" | xargs)
echo "${C_YELLOW}Port $DEFAULT_SERVER_PORT in use (PID: $pids), finding alternative...${RESET}"
max_port=$((DEFAULT_SERVER_PORT + PORT_SEARCH_MAX_ATTEMPTS - 1))
if ! SERVER_PORT=$(find_next_available_port "$DEFAULT_SERVER_PORT"); then
echo "${C_RED}Error: No free server port in range ${DEFAULT_SERVER_PORT}-${max_port}${RESET}"
exit 1
fi
fi
# Ensure web and server ports don't conflict with each other
if [ "$WEB_PORT" -eq "$SERVER_PORT" ]; then
local conflict_start=$((SERVER_PORT + 1))
max_port=$((conflict_start + PORT_SEARCH_MAX_ATTEMPTS - 1))
if ! SERVER_PORT=$(find_next_available_port "$conflict_start"); then
echo "${C_RED}Error: No free server port in range ${conflict_start}-${max_port}${RESET}"
exit 1
fi
fi
echo ""
echo "${C_GREEN}✓ Auto-selected available ports: Web=$WEB_PORT, Server=$SERVER_PORT${RESET}"
else
echo "${C_GREEN}✓${RESET} Port $DEFAULT_WEB_PORT is available"
echo "${C_GREEN}✓${RESET} Port $DEFAULT_SERVER_PORT is available"
fi
}
validate_terminal_size() {
if [ "$USE_COLORS" = false ]; then
return 0
fi
local term_width term_height
term_width=$(tput cols 2>/dev/null || echo 80)
term_height=$(tput lines 2>/dev/null || echo 24)
if [ "$term_width" -lt "$MIN_TERM_WIDTH" ] || [ "$term_height" -lt "$MIN_TERM_HEIGHT" ]; then
echo "${C_YELLOW}⚠${RESET} Terminal size ${term_width}x${term_height} is smaller than recommended ${MIN_TERM_WIDTH}x${MIN_TERM_HEIGHT}"
echo " Some elements may not display correctly."
echo ""
return 0
fi
}
# ============================================================================
# CURSOR & CLEANUP
# ============================================================================
hide_cursor() {
[ "$USE_COLORS" = true ] && printf "${ESC}[?25l"
}
show_cursor() {
[ "$USE_COLORS" = true ] && printf "${ESC}[?25h"
}
cleanup() {
show_cursor
# Restore terminal settings (echo and canonical mode)
stty echo icanon 2>/dev/null || true
# Kill server process if running in production mode
if [ -n "${SERVER_PID:-}" ]; then
kill $SERVER_PID 2>/dev/null || true
fi
printf "${RESET}\n"
}
trap cleanup EXIT INT TERM
# ============================================================================
# TERMINAL SIZE & UI UTILITIES
# ============================================================================
get_term_size() {
TERM_COLS=$(tput cols 2>/dev/null || echo 80)
TERM_LINES=$(tput lines 2>/dev/null || echo 24)
}
center_text() {
local text="$1"
local len=${#text}
local pad=$(( (TERM_COLS - len) / 2 ))
printf "%${pad}s%s\n" "" "$text"
}
draw_line() {
local char="${1:-─}"
local color="${2:-$C_GRAY}"
local width="${3:-58}"
printf "${color}"
for ((i=0; i<width; i++)); do printf "%s" "$char"; done
printf "${RESET}"
}
# ============================================================================
# UI DISPLAY FUNCTIONS
# ============================================================================
show_header() {
clear
get_term_size
# Top padding
local top_pad=$(( TERM_LINES / 6 ))
for ((i=0; i<top_pad; i++)); do echo ""; done
# Automaker ASCII art logo
local l1=" █▀▀█ █ █ ▀▀█▀▀ █▀▀█ █▀▄▀█ █▀▀█ █ █ █▀▀ █▀▀█ "
local l2=" █▄▄█ █ █ █ █ █ █ ▀ █ █▄▄█ █▀▄ █▀▀ █▄▄▀ "
local l3=" ▀ ▀ ▀▀▀ ▀ ▀▀▀▀ ▀ ▀ ▀ ▀ ▀ ▀ ▀▀▀ ▀ ▀▀ "
local pad_left=$(( (TERM_COLS - LOGO_WIDTH) / 2 ))
local pad=$(printf "%${pad_left}s" "")
echo -e "${pad}${C_PRI}${l1}${RESET}"
echo -e "${pad}${C_SEC}${l2}${RESET}"
echo -e "${pad}${C_ACC}${l3}${RESET}"
echo ""
local mode_indicator=""
if [ "$PRODUCTION_MODE" = true ]; then
mode_indicator="${C_GREEN}[PRODUCTION]${RESET}"
else
mode_indicator="${C_YELLOW}[DEVELOPMENT]${RESET}"
fi
local sub_display_len=60
local sub_pad=$(( (TERM_COLS - sub_display_len) / 2 ))
printf "%${sub_pad}s" ""
echo -e "${C_MUTE}Autonomous AI Development Studio${RESET} ${C_GRAY}│${RESET} ${C_GREEN}${VERSION}${RESET} ${mode_indicator}"
echo ""
echo ""
}
show_menu() {
local pad_left=$(( (TERM_COLS - MENU_BOX_WIDTH) / 2 ))
local pad=$(printf "%${pad_left}s" "")
local border="${C_GRAY}│${RESET}"
printf "%s${C_GRAY}╭" "$pad"
draw_line "─" "$C_GRAY" "$MENU_INNER_WIDTH"
printf "╮${RESET}\n"
# Menu items with selection indicator
local sel1="" sel2="" sel3="" sel4=""
local txt1="${C_MUTE}" txt2="${C_MUTE}" txt3="${C_MUTE}" txt4="${C_MUTE}"
case $SELECTED_OPTION in
1) sel1="${C_ACC}▸${RESET} ${C_PRI}"; txt1="${C_WHITE}" ;;
2) sel2="${C_ACC}▸${RESET} ${C_PRI}"; txt2="${C_WHITE}" ;;
3) sel3="${C_ACC}▸${RESET} ${C_PRI}"; txt3="${C_WHITE}" ;;
4) sel4="${C_ACC}▸${RESET} ${C_PRI}"; txt4="${C_WHITE}" ;;
esac
# Default non-selected prefix
[[ -z "$sel1" ]] && sel1=" ${C_MUTE}"
[[ -z "$sel2" ]] && sel2=" ${C_MUTE}"
[[ -z "$sel3" ]] && sel3=" ${C_MUTE}"
[[ -z "$sel4" ]] && sel4=" ${C_MUTE}"
printf "%s${border}${sel1}[1]${RESET} 🌐 ${txt1}Web App${RESET} ${C_MUTE}Server + Browser (localhost:$WEB_PORT)${RESET} ${border}\n" "$pad"
printf "%s${border}${sel2}[2]${RESET} 🖥 ${txt2}Electron${RESET} ${DIM}Desktop App (embedded server)${RESET} ${border}\n" "$pad"
printf "%s${border}${sel3}[3]${RESET} 🐳 ${txt3}Docker${RESET} ${DIM}Full Stack (live reload)${RESET} ${border}\n" "$pad"
printf "%s${border}${sel4}[4]${RESET} 🔗 ${txt4}Electron & Docker${RESET} ${DIM}Desktop + Docker Server${RESET} ${border}\n" "$pad"
printf "%s${C_GRAY}├" "$pad"
draw_line "─" "$C_GRAY" "$MENU_INNER_WIDTH"
printf "┤${RESET}\n"
printf "%s${border} ${C_RED}[Q]${RESET} ⏻ ${C_MUTE}Exit${RESET} ${border}\n" "$pad"
printf "%s${C_GRAY}╰" "$pad"
draw_line "─" "$C_GRAY" "$MENU_INNER_WIDTH"
printf "╯${RESET}\n"
echo ""
local footer_text="[↑↓] Navigate [Enter] Select [1-4] Quick Select [Q] Exit"
local f_pad=$(( (TERM_COLS - ${#footer_text}) / 2 ))
printf "%${f_pad}s" ""
echo -e "${DIM}${footer_text}${RESET}"
if [ -f "$HISTORY_FILE" ]; then
local last_mode
last_mode=$(cat "$HISTORY_FILE" 2>/dev/null || echo "")
if [ -n "$last_mode" ]; then
local hint_text="(Last: $last_mode)"
local h_pad=$(( (TERM_COLS - ${#hint_text}) / 2 ))
printf "%${h_pad}s" ""
echo -e "${DIM}${hint_text}${RESET}"
fi
fi
}
# ============================================================================
# SPINNER & INITIALIZATION
# ============================================================================
spinner() {
local text="$1"
local -a frames=('⠋' '⠙' '⠹' '⠸' '⠼' '⠴' '⠦' '⠧' '⠇' '⠏')
local i=0
local count=0
local max_frames=20 # Max 2 seconds
# Ensure TERM_COLS is set
[ -z "$TERM_COLS" ] && TERM_COLS=80
while [ $count -lt $max_frames ]; do
local len=${#text}
local pad_left=$(( (TERM_COLS - len - 4) / 2 ))
[ $pad_left -lt 0 ] && pad_left=0
printf "\r%${pad_left}s${C_PRI}${frames[$i]}${RESET} ${C_WHITE}%s${RESET}" "" "$text"
i=$(( (i + 1) % ${#frames[@]} ))
count=$((count + 1))
sleep 0.1 2>/dev/null || sleep 1
done
local len=${#text}
local pad_left=$(( (TERM_COLS - len - 4) / 2 ))
[ $pad_left -lt 0 ] && pad_left=0
printf "\r%${pad_left}s${C_GREEN}✓${RESET} ${C_WHITE}%s${RESET} \n" "" "$text"
}
center_print() {
local text="$1"
local color="${2:-}"
local len=${#text}
local pad=$(( (TERM_COLS - len) / 2 ))
[ $pad -lt 0 ] && pad=0
printf "%${pad}s${color}%s${RESET}\n" "" "$text"
}
resolve_port_conflicts() {
# Ensure terminal is in proper state for input
show_cursor
stty echo icanon 2>/dev/null || true
local web_in_use=false
local server_in_use=false
local web_pids=""
local server_pids=""
if is_port_in_use "$DEFAULT_WEB_PORT"; then
web_in_use=true
# Get PIDs and convert newlines to spaces for display
web_pids=$(get_pids_on_port "$DEFAULT_WEB_PORT" | xargs)
fi
if is_port_in_use "$DEFAULT_SERVER_PORT"; then
server_in_use=true
# Get PIDs and convert newlines to spaces for display
server_pids=$(get_pids_on_port "$DEFAULT_SERVER_PORT" | xargs)
fi
if [ "$web_in_use" = true ] || [ "$server_in_use" = true ]; then
echo ""
if [ "$web_in_use" = true ]; then
center_print "Port $DEFAULT_WEB_PORT in use (PID: $web_pids)" "$C_YELLOW"
fi
if [ "$server_in_use" = true ]; then
center_print "Port $DEFAULT_SERVER_PORT in use (PID: $server_pids)" "$C_YELLOW"
fi
echo ""
# Show options
center_print "What would you like to do?" "$C_WHITE"
echo ""
center_print "[Enter] Auto-select available ports (Recommended)" "$C_GREEN"
center_print "[K] Kill processes and use default ports" "$C_MUTE"
center_print "[C] Choose custom ports" "$C_MUTE"
center_print "[X] Cancel" "$C_RED"
echo ""
while true; do
local choice_pad=$(( (TERM_COLS - 20) / 2 ))
printf "%${choice_pad}s" ""
read -r -p "Choice [Enter]: " choice
case "$choice" in
""|[aA]|[aA][uU][tT][oO])
# Auto-select: find next available ports
echo ""
local max_port=$((DEFAULT_WEB_PORT + PORT_SEARCH_MAX_ATTEMPTS - 1))
if [ "$web_in_use" = true ]; then
if ! WEB_PORT=$(find_next_available_port "$DEFAULT_WEB_PORT"); then
center_print "No free web port in range ${DEFAULT_WEB_PORT}-${max_port}" "$C_RED"
exit 1
fi
fi
max_port=$((DEFAULT_SERVER_PORT + PORT_SEARCH_MAX_ATTEMPTS - 1))
if [ "$server_in_use" = true ]; then
if ! SERVER_PORT=$(find_next_available_port "$DEFAULT_SERVER_PORT"); then
center_print "No free server port in range ${DEFAULT_SERVER_PORT}-${max_port}" "$C_RED"
exit 1
fi
fi
# Ensure web and server ports don't conflict with each other
if [ "$WEB_PORT" -eq "$SERVER_PORT" ]; then
local conflict_start=$((SERVER_PORT + 1))
max_port=$((conflict_start + PORT_SEARCH_MAX_ATTEMPTS - 1))
if ! SERVER_PORT=$(find_next_available_port "$conflict_start"); then
center_print "No free server port in range ${conflict_start}-${max_port}" "$C_RED"
exit 1
fi
fi
center_print "✓ Auto-selected available ports:" "$C_GREEN"
center_print " Web: $WEB_PORT | Server: $SERVER_PORT" "$C_PRI"
break
;;
[kK]|[kK][iI][lL][lL])
echo ""
if [ "$web_in_use" = true ]; then
center_print "Killing process(es) on port $DEFAULT_WEB_PORT..." "$C_YELLOW"
kill_port "$DEFAULT_WEB_PORT" > /dev/null 2>&1 || true
center_print "✓ Port $DEFAULT_WEB_PORT is now free" "$C_GREEN"
fi
if [ "$server_in_use" = true ]; then
center_print "Killing process(es) on port $DEFAULT_SERVER_PORT..." "$C_YELLOW"
kill_port "$DEFAULT_SERVER_PORT" > /dev/null 2>&1 || true
center_print "✓ Port $DEFAULT_SERVER_PORT is now free" "$C_GREEN"
fi
break
;;
[cC]|[cC][hH][oO][oO][sS][eE])
echo ""
local input_pad=$(( (TERM_COLS - 40) / 2 ))
# Collect both ports first
printf "%${input_pad}s" ""
read -r -p "Enter web port (default $DEFAULT_WEB_PORT): " input_web
input_web=${input_web:-$DEFAULT_WEB_PORT}
printf "%${input_pad}s" ""
read -r -p "Enter server port (default $DEFAULT_SERVER_PORT): " input_server
input_server=${input_server:-$DEFAULT_SERVER_PORT}
# Validate both before assigning either
if ! validate_port "$input_web" "Web port"; then
continue
fi
if ! validate_port "$input_server" "Server port"; then
continue
fi
# Assign atomically after both validated
WEB_PORT=$input_web
SERVER_PORT=$input_server
center_print "Using ports: Web=$WEB_PORT, Server=$SERVER_PORT" "$C_GREEN"
break
;;
[xX]|[xX][cC][aA][nN][cC][eE][lL])
echo ""
center_print "Cancelled." "$C_MUTE"
echo ""
exit 0
;;
*)
center_print "Invalid choice. Press Enter for auto-select, or K/C/X." "$C_RED"
;;
esac
done
else
center_print "✓ Port $DEFAULT_WEB_PORT is available" "$C_GREEN"
center_print "✓ Port $DEFAULT_SERVER_PORT is available" "$C_GREEN"
fi
# Restore terminal state
hide_cursor
stty -echo -icanon 2>/dev/null || true
}
launch_sequence() {
local mode_name="$1"
# Ensure terminal size is available
get_term_size
echo ""
# Show port checking for modes that use local ports
if [[ "$MODE" == "web" || "$MODE" == "electron" ]]; then
center_print "Checking ports ${DEFAULT_WEB_PORT} and ${DEFAULT_SERVER_PORT}..." "$C_MUTE"
resolve_port_conflicts
echo ""
fi
spinner "Initializing environment..."
spinner "Starting $mode_name..."
echo ""
local msg="Automaker is ready!"
local pad=$(( (TERM_COLS - ${#msg}) / 2 ))
printf "%${pad}s${C_GREEN}${BOLD}%s${RESET}\n" "" "$msg"
case "$MODE" in
web)
local url="http://${APP_HOST}:$WEB_PORT"
local upad=$(( (TERM_COLS - ${#url} - 10) / 2 ))
echo ""
printf "%${upad}s${DIM}Opening ${C_SEC}%s${RESET}\n" "" "$url"
;;
docker|docker-electron)
echo ""
local ui_msg="UI: http://localhost:$DEFAULT_WEB_PORT"
local api_msg="API: http://localhost:$DEFAULT_SERVER_PORT"
center_text "${DIM}${ui_msg}${RESET}"
center_text "${DIM}${api_msg}${RESET}"
;;
esac
echo ""
}
# ============================================================================
# HISTORY MANAGEMENT
# ============================================================================
save_mode_to_history() {
if [ "$NO_HISTORY" = false ]; then
echo "$1" > "$HISTORY_FILE"
fi
}
get_last_mode_from_history() {
if [ -f "$HISTORY_FILE" ] && [ "$NO_HISTORY" = false ]; then
cat "$HISTORY_FILE"
fi
}
# ============================================================================
# PRODUCTION BUILD
# ============================================================================
build_for_production() {