-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtunnel.sh
More file actions
executable file
Β·238 lines (207 loc) Β· 7.44 KB
/
tunnel.sh
File metadata and controls
executable file
Β·238 lines (207 loc) Β· 7.44 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
#!/bin/bash
# Tunnel Script
# Configuration loaded from .env (root, gitignored) and .env.tunnel (per-app, git-tracked)
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=tunnel-lib.sh
source "$SCRIPT_DIR/tunnel-lib.sh"
# --- Defaults ---
TUNNEL_DRY_RUN=false
TUNNEL_APP_ENV_ARG=""
tunnel_kill_vite() {
local pids
pids="$(pgrep -f 'vite/bin/vite\.js' 2>/dev/null)" || true
if [ -z "$pids" ]; then
echo "No vite processes found."
return
fi
echo "Killing vite process trees:"
local killed_pids=""
for pid in $pids; do
local root_pid="$pid"
while ppid="$(ps -o ppid= -p "$root_pid" 2>/dev/null | tr -d ' ')"; do
[ -z "$ppid" ] || [ "$ppid" -eq 1 ] && break
local pcmd
pcmd="$(ps -o args= -p "$ppid" 2>/dev/null)" || break
case "$pcmd" in
*node*|*pnpm*|*dotenv*) root_pid="$ppid" ;;
*) break ;;
esac
done
if echo "$killed_pids" | grep -qw "$root_pid"; then
continue
fi
killed_pids="$killed_pids $root_pid"
local cmd
cmd="$(ps -o args= -p "$root_pid" 2>/dev/null)" || true
echo " PID $root_pid $cmd"
kill -- -"$(ps -o pgid= -p "$root_pid" 2>/dev/null | tr -d ' ')" 2>/dev/null || kill "$root_pid" 2>/dev/null || true
done
echo "Done."
}
# --- CLI Parsing ---
usage() {
cat <<EOF
Usage: tunnel.sh [options]
Configuration Precedence:
1. CLI options (--host, --port, --plugins)
2. Root .env (user-specific, gitignored) - TUNNEL_PLUGINS
3. Per-app .env.tunnel (app config, git-tracked)
4. Default values
Options:
--config <path> Path to .env.tunnel file (default: ./.env.tunnel)
--host <name> Override TUNNEL_HOST
--port <number> Override TUNNEL_PORT / TUNNEL_MAIN_PORT
--env <path> Path to root .env (default: auto-detect, walk up from CWD)
--plugins <list> Override TUNNEL_PLUGINS (comma-separated, e.g. core,dashboard)
--dry-run Print resolved config and exit without starting anything
--kill Kill all node processes originating from vite and exit
--list Discover all .env.tunnel files in the project
-h, --help Show this help message
Examples:
# Start with all plugins from .env.tunnel:
tunnel.sh
# Start with specific plugins (CLI override):
tunnel.sh --plugins core,dashboard
# API docs mode (use root .env TUNNEL_PLUGINS=dashboard):
tunnel.sh --config apps/docs/.env.tunnel
tunnel.sh # uses .env.tunnel in CWD
tunnel.sh --config apps/docs # uses apps/docs/.env.tunnel
tunnel.sh --host custom --port 8080 # override host and port
tunnel.sh --list # show available configs
tunnel.sh --dry-run # validate config without starting
EOF
exit 0
}
tunnel_list_configs() {
echo "Available .env.tunnel configurations:"
echo
local files
files="$(find "$SCRIPT_DIR" -name ".env.tunnel" -not -path "*/node_modules/*" 2>/dev/null | sort)" || true
if [ -z "$files" ]; then
echo " No .env.tunnel files found."
echo
return
fi
while IFS= read -r file; do
local host
host="$(grep -E '^TUNNEL_HOST=' "$file" 2>/dev/null | cut -d'=' -f2)" || true
local mode
mode="$(grep -E '^TUNNEL_MODE=' "$file" 2>/dev/null | cut -d'=' -f2)" || true
mode="${mode:-single}"
if [ -z "$host" ]; then
host="$(grep -E '^TUNNEL_MAIN_HOST=' "$file" 2>/dev/null | cut -d'=' -f2)" || true
fi
printf " %-40s host=%-12s mode=%s\n" "$file" "${host:-?}" "$mode"
done <<< "$files"
echo
}
while [[ $# -gt 0 ]]; do
case "$1" in
--config)
TUNNEL_APP_ENV_ARG="$2"
shift 2
;;
--host)
TUNNEL_HOST_OVERRIDE="$2"
shift 2
;;
--port)
TUNNEL_PORT_OVERRIDE="$2"
shift 2
;;
--env)
TUNNEL_ROOT_ENV="$2"
export TUNNEL_ROOT_ENV
shift 2
;;
--plugins)
TUNNEL_PLUGINS_OVERRIDE="$2"
shift 2
;;
--dry-run)
TUNNEL_DRY_RUN=true
shift
;;
--list)
tunnel_list_configs
exit 0
;;
--kill)
tunnel_kill_vite
exit 0
;;
-h|--help)
usage
;;
*)
echo "Unknown option: $1" >&2
usage
;;
esac
done
# --- Load Config ---
# 1. Root .env (infrastructure secrets, gitignored)
tunnel_load_root_env
# 2. Per-app .env.tunnel (app config, git-tracked)
tunnel_load_app_env "$TUNNEL_APP_ENV_ARG"
# 3. CLI overrides for TUNNEL_PLUGINS take highest precedence
# shellcheck disable=SC2034
[ -n "${TUNNEL_PLUGINS_OVERRIDE:-}" ] && TUNNEL_PLUGINS="$TUNNEL_PLUGINS_OVERRIDE"
# 3. CLI overrides
[ -n "${TUNNEL_HOST_OVERRIDE:-}" ] && TUNNEL_HOST="$TUNNEL_HOST_OVERRIDE"
[ -n "${TUNNEL_PORT_OVERRIDE:-}" ] && TUNNEL_PORT="$TUNNEL_PORT_OVERRIDE"
[ -n "${TUNNEL_PORT_OVERRIDE:-}" ] && TUNNEL_MAIN_PORT="$TUNNEL_PORT_OVERRIDE"
# 4. Resolve paths relative to script dir
TUNNEL_APP_DIR="$(cd "$SCRIPT_DIR/$TUNNEL_APP_DIR" 2>/dev/null && pwd || echo "$SCRIPT_DIR/$TUNNEL_APP_DIR")"
if [ "${TUNNEL_MODE:-single}" = "multi" ]; then
TUNNEL_MAIN_APP_DIR="$(cd "$SCRIPT_DIR/$TUNNEL_MAIN_APP_DIR" 2>/dev/null && pwd || echo "$SCRIPT_DIR/$TUNNEL_MAIN_APP_DIR")"
if [ -n "${TUNNEL_PLUGIN_CONFIG:-}" ]; then
TUNNEL_PLUGIN_CONFIG="$SCRIPT_DIR/$TUNNEL_PLUGIN_CONFIG"
fi
if [ -n "${TUNNEL_PLUGINS_BASE_DIR:-}" ]; then
TUNNEL_PLUGINS_BASE_DIR="$SCRIPT_DIR/$TUNNEL_PLUGINS_BASE_DIR"
fi
fi
# 5. Validate
tunnel_validate_config
# --- Dry Run ---
if [ "$TUNNEL_DRY_RUN" = true ]; then
echo
echo "=== Resolved Tunnel Configuration ==="
echo " TUNNEL_MODE = ${TUNNEL_MODE:-single}"
echo " TUNNEL_DOMAIN = $TUNNEL_DOMAIN"
echo " PIKO_SERVICE = $PIKO_SERVICE"
echo " TUNNEL_APP_DIR = $TUNNEL_APP_DIR"
if [ "${TUNNEL_MODE:-single}" = "multi" ]; then
# shellcheck disable=SC2153
echo " TUNNEL_MAIN_HOST = $TUNNEL_MAIN_HOST"
echo " TUNNEL_MAIN_APP_DIR = $TUNNEL_MAIN_APP_DIR"
echo " TUNNEL_MAIN_PORT = ${TUNNEL_MAIN_PORT:-4173}"
echo " TUNNEL_SERVER_CMD = ${TUNNEL_SERVER_CMD:-pnpm serve}"
echo " TUNNEL_PLUGIN_CONFIG = ${TUNNEL_PLUGIN_CONFIG:-<none>}"
else
echo " TUNNEL_HOST = $TUNNEL_HOST"
echo " TUNNEL_PORT = ${TUNNEL_PORT:-4173}"
echo " TUNNEL_SERVER_CMD = ${TUNNEL_SERVER_CMD:-pnpm preview}"
echo " TUNNEL_SERVER_ARGS = ${TUNNEL_SERVER_ARGS:-}"
fi
echo
echo "Configuration valid. Exiting (dry-run)."
exit 0
fi
# --- Execute ---
if [ "${TUNNEL_MODE:-single}" = "multi" ]; then
tunnel_multi_app
else
# Build server command with optional args
local_port="${TUNNEL_PORT:-$(tunnel_find_open_port 4173)}"
server_cmd="${TUNNEL_SERVER_CMD:-pnpm preview}"
if [ -n "${TUNNEL_SERVER_ARGS:-}" ]; then
server_cmd="$server_cmd $TUNNEL_SERVER_ARGS"
fi
# Substitute {PORT} placeholder in server args if present
server_cmd="${server_cmd//\{PORT\}/$local_port}"
tunnel_single_app "$TUNNEL_APP_DIR" "$local_port" "$TUNNEL_HOST" \
"${TUNNEL_APP_NAME:-app}" "$server_cmd"
fi