Skip to content

Commit a968994

Browse files
committed
fix(scripts): improve env loading in bun wrapper and add --help to dev.sh
- Unset env vars before sourcing to ensure file values take precedence - Add quotes around APP_URL assignment for consistency - Add --help flag showing usage, services, logs, and examples
1 parent dc730f4 commit a968994

File tree

2 files changed

+69
-20
lines changed

2 files changed

+69
-20
lines changed

.bin/bun

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -202,34 +202,40 @@ check_env_setup() {
202202
echo "" >&2
203203
}
204204

205-
# Load project env files to ensure correct precedence
206-
# .env.development.local must be sourced last to override any inherited env vars
207-
load_project_env() {
208-
# Source env files with auto-export
209-
# Order matters: .env.local first, then .env.development.local overrides
210-
set -a
211-
212-
# Only source .env.local when running from subdirectory
213-
# (Bun loads it natively when at project root)
214-
if [ "$PROJECT_ROOT" != "$CALLING_DIR" ] && [ -f "$ENV_LOCAL_FILE" ]; then
215-
# shellcheck disable=SC1090
216-
source "$ENV_LOCAL_FILE"
205+
# Clear inherited env vars that would conflict with .env file values
206+
# Bun loads .env files natively, but inherited shell variables take precedence.
207+
# We unset variables defined in our env files so Bun can set fresh values.
208+
clear_inherited_env_vars() {
209+
# Unset variables from .env.local so Bun can load fresh values
210+
if [ -f "$ENV_LOCAL_FILE" ]; then
211+
while IFS='=' read -r key _; do
212+
# Skip comments and empty lines
213+
case "$key" in
214+
'#'*|'') continue ;;
215+
esac
216+
# Remove any 'export ' prefix
217+
key="${key#export }"
218+
unset "$key" 2>/dev/null || true
219+
done < "$ENV_LOCAL_FILE"
217220
fi
218221

219-
# Always source .env.development.local to ensure worktree overrides
220-
# take precedence over any inherited shell environment variables
222+
# Unset variables from .env.development.local
221223
if [ -f "$ENV_DEVELOPMENT_LOCAL_FILE" ]; then
222-
# shellcheck disable=SC1090
223-
source "$ENV_DEVELOPMENT_LOCAL_FILE"
224+
while IFS='=' read -r key _; do
225+
case "$key" in
226+
'#'*|'') continue ;;
227+
esac
228+
key="${key#export }"
229+
unset "$key" 2>/dev/null || true
230+
done < "$ENV_DEVELOPMENT_LOCAL_FILE"
224231
fi
225-
226-
set +a
227232
}
228233

229234
run_bun() {
230235
create_env_symlinks
231236
check_env_setup
232-
load_project_env
237+
clear_inherited_env_vars
238+
# Bun natively loads .env files in the correct precedence order
233239
exec "$REAL_BUN" "$@"
234240
}
235241

scripts/dev.sh

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,48 @@
33

44
set -e
55

6+
# =============================================================================
7+
# Help
8+
# =============================================================================
9+
10+
show_help() {
11+
cat << EOF
12+
Usage: $(basename "$0") [OPTIONS] [-- CLI_ARGS...]
13+
14+
Starts the Codebuff development environment including database, web server,
15+
and CLI.
16+
17+
Options:
18+
-h, --help Show this help message and exit
19+
20+
Services Started:
21+
db PostgreSQL database (via Docker)
22+
studio Drizzle Studio for database inspection
23+
web Next.js web server (in tmux session 'codebuff-web')
24+
cli Codebuff CLI (foreground, interactive)
25+
26+
Logs:
27+
All service logs are written to: debug/console/
28+
- db.log Database startup logs
29+
- studio.log Drizzle Studio logs
30+
- sdk.log SDK build logs
31+
- web.log Web server logs
32+
33+
Examples:
34+
$(basename "$0") # Start dev environment
35+
$(basename "$0") --help # Show this help
36+
$(basename "$0") -- --debug # Pass --debug flag to CLI
37+
38+
To attach to web server logs:
39+
tmux attach -t codebuff-web
40+
EOF
41+
}
42+
43+
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
44+
show_help
45+
exit 0
46+
fi
47+
648
# =============================================================================
749
# Configuration
850
# =============================================================================
@@ -11,6 +53,7 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1153
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
1254
LOG_DIR="$PROJECT_ROOT/debug/console"
1355
BUN="$PROJECT_ROOT/.bin/bun"
56+
APP_URL="$NEXT_PUBLIC_CODEBUFF_APP_URL"
1457

1558
export PATH="$PROJECT_ROOT/.bin:$PATH"
1659
mkdir -p "$LOG_DIR"
@@ -92,7 +135,7 @@ else
92135
tmux new-session -d -s codebuff-web "cd $PROJECT_ROOT && $BUN --cwd web dev 2>&1 | sed -l 's/\x1b\[[0-9;]*m//g' | tee $LOG_DIR/web.log"
93136
fi
94137

95-
wait_for "web" "curl -sf ${NEXT_PUBLIC_CODEBUFF_APP_URL}/api/healthz"
138+
wait_for "web" "curl -sf ${APP_URL}/api/healthz"
96139

97140
# 4. CLI (foreground - user interaction)
98141
echo ""

0 commit comments

Comments
 (0)