This repository was archived by the owner on Jun 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathmac.sh
More file actions
200 lines (170 loc) · 6.38 KB
/
Copy pathmac.sh
File metadata and controls
200 lines (170 loc) · 6.38 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
#!/usr/bin/env bash
#
# Portable-Diffusion - macOS Launcher
# Double-click or run: ./mac.sh
#
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
APP_DIR="$SCRIPT_DIR/app"
PLATFORM="$(uname -s)"
if [[ "$PLATFORM" != "Darwin" ]]; then
echo "[ERROR] This script is for macOS only. Please run ./linux.sh on Linux." >&2
exit 1
fi
NODE_DIR="$APP_DIR/tools/node-mac"
NODE_BIN="$NODE_DIR/bin/node"
BACKEND_PATH="$APP_DIR/backend/mac/sd"
PLATFORM_LABEL="macOS"
DIST_INDEX="$APP_DIR/dist/index.html"
SETUP_SCRIPT="$SCRIPT_DIR/scripts/setup.sh"
SERVE_SCRIPT="$SCRIPT_DIR/scripts/serve.cjs"
FRONTEND_PORT="${FRONTEND_PORT:-1420}"
SETUP_REASON=""
SETUP_MODE="Repair"
# ── Setup node_modules to avoid OS conflicts ────────────────────────────────
FRONTEND_NODE_MODULES="$APP_DIR/frontend/node_modules"
MAC_NODE_MODULES="$APP_DIR/frontend/node_modules_mac"
ACTIVE_OS_FILE="$APP_DIR/frontend/.active_modules_os"
# Attempt to create a test symlink to check if filesystem supports symlinks
USE_SYMLINKS=true
TEST_LINK="$APP_DIR/frontend/.test_symlink"
rm -f "$TEST_LINK"
if ln -s "node_modules_mac" "$TEST_LINK" 2>/dev/null; then
rm -f "$TEST_LINK"
else
USE_SYMLINKS=false
fi
if [ "$USE_SYMLINKS" = true ]; then
if [[ -d "$FRONTEND_NODE_MODULES" && ! -L "$FRONTEND_NODE_MODULES" ]]; then
echo " >> Migrating existing node_modules to node_modules_mac..."
mv "$FRONTEND_NODE_MODULES" "$MAC_NODE_MODULES"
fi
rm -f "$FRONTEND_NODE_MODULES"
mkdir -p "$MAC_NODE_MODULES"
ln -sf "node_modules_mac" "$FRONTEND_NODE_MODULES"
else
# Fallback: Filesystem does not support symlinks (e.g. FAT32/exFAT)
echo " >> Filesystem does not support symlinks. Using directory swapping fallback..."
if [[ -L "$FRONTEND_NODE_MODULES" || -f "$FRONTEND_NODE_MODULES" ]]; then
rm -f "$FRONTEND_NODE_MODULES"
fi
PREV_OS=""
if [[ -f "$ACTIVE_OS_FILE" ]]; then
PREV_OS=$(cat "$ACTIVE_OS_FILE")
fi
if [[ -d "$FRONTEND_NODE_MODULES" && "$PREV_OS" != "mac" ]]; then
if [[ -n "$PREV_OS" ]]; then
echo " >> Swapping out node_modules to node_modules_$PREV_OS..."
rm -rf "$APP_DIR/frontend/node_modules_$PREV_OS"
mv "$FRONTEND_NODE_MODULES" "$APP_DIR/frontend/node_modules_$PREV_OS"
else
echo " >> Saving node_modules as node_modules_windows..."
rm -rf "$APP_DIR/frontend/node_modules_windows"
mv "$FRONTEND_NODE_MODULES" "$APP_DIR/frontend/node_modules_windows"
fi
fi
if [[ -d "$MAC_NODE_MODULES" && ! -d "$FRONTEND_NODE_MODULES" ]]; then
echo " >> Swapping in node_modules_mac..."
mv "$MAC_NODE_MODULES" "$FRONTEND_NODE_MODULES"
elif [[ ! -d "$FRONTEND_NODE_MODULES" ]]; then
mkdir -p "$FRONTEND_NODE_MODULES"
fi
echo "mac" > "$ACTIVE_OS_FILE"
fi
# ── First-time setup check ─────────────────────────────────────────────────
if [[ ! -d "$NODE_DIR" ]]; then
SETUP_MODE="First-Time Setup"
fi
if [[ ! -x "$NODE_BIN" ]]; then
SETUP_REASON="Portable Node.js for macOS is missing."
fi
if [[ ! -f "$DIST_INDEX" ]]; then
SETUP_REASON="Frontend build is missing."
fi
if [[ ! -x "$BACKEND_PATH" ]]; then
SETUP_REASON="No macOS Metal backend binary is installed."
fi
if [[ -n "$SETUP_REASON" ]]; then
echo ""
echo " ============================================================"
echo " PORTABLE DIFFUSION | $PLATFORM_LABEL $SETUP_MODE"
echo " ============================================================"
echo ""
if [[ "$SETUP_MODE" == "First-Time Setup" ]]; then
echo " This looks like your first run on macOS. Setting up automatically..."
else
echo " Portable Diffusion needs a quick repair before launch."
fi
echo " Reason: $SETUP_REASON"
echo " Models are not downloaded during setup. Download or import them in the app."
echo ""
read -rp " Press Enter to continue, or Ctrl+C to cancel."
# Clear any existing frontend and backend server processes
if command -v lsof >/dev/null 2>&1; then
lsof -t -i:"${FRONTEND_PORT}" -i:8080 | xargs kill -9 >/dev/null 2>&1 || true
elif command -v fuser >/dev/null 2>&1; then
fuser -k "${FRONTEND_PORT}/tcp" >/dev/null 2>&1 || true
fuser -k "8080/tcp" >/dev/null 2>&1 || true
fi
if ! bash "$SETUP_SCRIPT"; then
echo ""
echo " [ERROR] Setup failed. Please check the output above."
read -rp " Press Enter to close..."
exit 1
fi
fi
# ── Launch ─────────────────────────────────────────────────────────────────
clear 2>/dev/null || true
echo ""
echo " ============================================================"
echo " PORTABLE DIFFUSION | Launching..."
echo " ============================================================"
echo ""
# Clear frontend and backend ports
if command -v lsof >/dev/null 2>&1; then
lsof -t -i:"${FRONTEND_PORT}" -i:8080 | xargs kill -9 >/dev/null 2>&1 || true
elif command -v fuser >/dev/null 2>&1; then
fuser -k "${FRONTEND_PORT}/tcp" >/dev/null 2>&1 || true
fuser -k "8080/tcp" >/dev/null 2>&1 || true
fi
# Start the server
echo " Starting Portable Diffusion..."
export PATH="$NODE_DIR/bin:$PATH"
export FRONTEND_PORT="$FRONTEND_PORT"
# Run server in background and capture PID
"$NODE_BIN" "$SERVE_SCRIPT" &
SERVER_PID=$!
# Wait for server to be ready
sleep 2
# Open browser
if command -v open >/dev/null 2>&1; then
echo " Opening browser at http://localhost:${FRONTEND_PORT}"
open "http://localhost:${FRONTEND_PORT}" >/dev/null 2>&1 &
else
echo " Open your browser to: http://localhost:${FRONTEND_PORT}"
fi
echo ""
echo " ============================================================"
echo " Running!"
echo " Web UI: http://localhost:${FRONTEND_PORT}"
echo " GPU API: Auto-selected by the app (starts at 8080)"
echo ""
echo " Press Ctrl+C in this window to stop all services."
echo " ============================================================"
echo ""
# Cleanup on exit
cleanup() {
echo ""
echo " Shutting down..."
if kill -0 "$SERVER_PID" >/dev/null 2>&1; then
kill -TERM "$SERVER_PID" >/dev/null 2>&1 || true
sleep 1
kill -KILL "$SERVER_PID" >/dev/null 2>&1 || true
fi
echo " Done. Goodbye!"
exit 0
}
trap cleanup SIGINT SIGTERM
# Keep script alive
wait "$SERVER_PID" || true
cleanup