-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAdKit.command
More file actions
116 lines (103 loc) · 3.92 KB
/
Copy pathOpenAdKit.command
File metadata and controls
116 lines (103 loc) · 3.92 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
#!/usr/bin/env bash
# OpenAdKit desktop launcher (macOS / Linux).
#
# Same behavior as OpenAdKit.bat: detects port conflicts with other OpenAdKit
# installs (or unrelated processes) and auto-shifts to a free port pair.
# First run installs deps + writes default .env.local + creates Desktop
# shortcut. Subsequent runs just open the launcher.
set -e
cd "$(dirname "$0")"
# 1. Sanity: Node installed?
if ! command -v node >/dev/null 2>&1; then
echo
echo "[ERROR] Node.js is not installed."
echo " Install Node 20+ from https://nodejs.org/en/download then run OpenAdKit again."
echo
read -p "Press enter to exit…" _ || true
exit 1
fi
# 2. npm install on first run
if [ ! -d node_modules ]; then
echo
echo "=================================================="
echo " First run · installing dependencies"
echo "=================================================="
npm install --no-audit --no-fund
fi
# .env.local is NOT seeded here — the port resolver below detects first-run
# (no .env.local) and asks the OS for ports the kernel just confirmed are
# free. That skips the contested 3000-range entirely so we never collide
# with Next.js / Vite / Express / Rails / Flask running locally.
mkdir -p data
# 4. Desktop shortcut on first run
if [ -d "$HOME/Desktop" ]; then
if [ "$(uname)" = "Darwin" ]; then
if [ ! -e "$HOME/Desktop/OpenAdKit.command" ]; then
cp -f OpenAdKit.command "$HOME/Desktop/OpenAdKit.command" 2>/dev/null && \
chmod +x "$HOME/Desktop/OpenAdKit.command" 2>/dev/null
fi
else
if [ ! -e "$HOME/Desktop/OpenAdKit.desktop" ]; then
cat > "$HOME/Desktop/OpenAdKit.desktop" <<DESK
[Desktop Entry]
Type=Application
Name=OpenAdKit
Comment=Local AI ad operations cockpit
Exec=bash $(pwd)/OpenAdKit.command
Terminal=true
Categories=Office;Development;
DESK
chmod +x "$HOME/Desktop/OpenAdKit.desktop" 2>/dev/null
fi
fi
fi
# 5. Resolve ports (multi-install conflict detection)
echo "Checking for port conflicts..."
RESOLVED=$(node scripts/resolve-ports.cjs 2>/dev/null)
ACTION=$(echo "$RESOLVED" | grep -oE 'ACTION=[a-z_]+' | head -1 | cut -d= -f2)
SYNC_PORT=$(echo "$RESOLVED" | grep -oE 'SYNC=[0-9]+' | head -1 | cut -d= -f2)
WEB_PORT=$(echo "$RESOLVED" | grep -oE 'PORT=[0-9]+' | head -1 | cut -d= -f2)
if [ -z "$ACTION" ]; then
echo "[ERROR] Port resolver failed to run. Falling back to defaults."
ACTION="start"
SYNC_PORT="${SYNC_PORT:-41574}"
WEB_PORT="${WEB_PORT:-41573}"
fi
if [ "$ACTION" = "error" ]; then
REASON=$(echo "$RESOLVED" | grep -oE 'REASON=[^ ]+' | head -1 | cut -d= -f2)
echo "[ERROR] Port resolver: $REASON"
read -p "Press enter to exit…" _ || true
exit 1
fi
if [ "$ACTION" = "reuse" ]; then
echo "Sidecar already running on :$SYNC_PORT for this install. Opening browser..."
if command -v open >/dev/null 2>&1; then open "http://127.0.0.1:$SYNC_PORT/" &
elif command -v xdg-open >/dev/null 2>&1; then xdg-open "http://127.0.0.1:$SYNC_PORT/" &
fi
exit 0
fi
if [ "$ACTION" = "restart_stale" ]; then
echo "Stale sidecar on :$SYNC_PORT — asking it to quit before starting fresh..."
curl -fsS -X POST --max-time 3 "http://127.0.0.1:$SYNC_PORT/quit" >/dev/null 2>&1 || true
# Poll until the port frees. sleep 1 is not enough on macOS with open file
# handles; the new sidecar would otherwise fail to bind. (Audit finding #41.)
DEADLINE=$(( $(date +%s) + 10 ))
while [ $(date +%s) -lt $DEADLINE ]; do
# nc -z returns 0 if port is OPEN. We want it CLOSED (free).
if ! nc -z 127.0.0.1 "$SYNC_PORT" >/dev/null 2>&1; then
break
fi
sleep 0.3
done
fi
if [ "$ACTION" = "shifted" ]; then
echo
echo " Default ports were taken by another OpenAdKit install or process."
echo " This install will use: web=$WEB_PORT sync=$SYNC_PORT"
echo " Saved to .env.local so future launches reuse these."
echo
fi
# 6. Hand off to the sidecar (visible log, Ctrl+C exits)
export ADFORGE_SYNC_PORT="$SYNC_PORT"
export PORT="$WEB_PORT"
exec bash scripts/start.sh