-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuddy
More file actions
executable file
·122 lines (102 loc) · 3.62 KB
/
buddy
File metadata and controls
executable file
·122 lines (102 loc) · 3.62 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
#!/bin/sh
set -e
ROOT_DIR=$(CDPATH= cd -- "$(dirname "$0")" && pwd)
PANTRY_LOCAL_BIN="${ROOT_DIR}/pantry/.bin"
PANTRY_DIR="${ROOT_DIR}/pantry"
USER_LOCAL_BIN="${HOME}/.local/bin"
PANTRY_INSTALLER="${ROOT_DIR}/storage/framework/scripts/pantry-install"
PANTRY_BOOTSTRAP_TIMEOUT_SECONDS="${PANTRY_BOOTSTRAP_TIMEOUT_SECONDS:-600}"
PANTRY_DEPENDENCIES_TIMEOUT_SECONDS="${PANTRY_DEPENDENCIES_TIMEOUT_SECONDS:-1200}"
# Resolve which CLI entrypoint to invoke. Resolution order matches the
# action-runner fallback chain in `@stacksjs/actions`:
#
# 1. `storage/framework/core/buddy/src/cli.ts` — userland override (legacy
# "framework lives in the project" layout). Kept FIRST so projects that
# have published buddy via `buddy publish:core buddy` still win.
# 2. `node_modules/@stacksjs/buddy/src/cli.ts` — installed package, TS source
# (workspace symlink or a real npm install with `src/` shipped).
# 3. `node_modules/@stacksjs/buddy/dist/cli.js` — published JS-only build.
#
# If none of these are present the script bails with a clear message instead
# of `bun` exploding on a missing file.
resolve_buddy_entry() {
for candidate in \
"${ROOT_DIR}/storage/framework/core/buddy/src/cli.ts" \
"${ROOT_DIR}/node_modules/@stacksjs/buddy/src/cli.ts" \
"${ROOT_DIR}/node_modules/@stacksjs/buddy/dist/cli.js" \
; do
if [ -f "${candidate}" ]; then
echo "${candidate}"
return 0
fi
done
return 1
}
if ! BUDDY_ENTRY=$(resolve_buddy_entry); then
echo "buddy: could not locate CLI entrypoint." >&2
echo " Tried: storage/framework/core/buddy/src/cli.ts, node_modules/@stacksjs/buddy/{src/cli.ts,dist/cli.js}." >&2
echo " Run \`bun install\` or \`buddy publish:core buddy\` to make the CLI available." >&2
exit 1
fi
run_with_timeout() {
timeout_seconds="$1"
shift
timeout_flag="${TMPDIR:-/tmp}/buddy-timeout-$$"
rm -f "${timeout_flag}"
"$@" &
cmd_pid=$!
(
sleep "${timeout_seconds}"
if kill -0 "${cmd_pid}" 2>/dev/null; then
echo "Command timed out after ${timeout_seconds}s: $*" >&2
: > "${timeout_flag}"
kill "${cmd_pid}" 2>/dev/null || true
sleep 2
kill -9 "${cmd_pid}" 2>/dev/null || true
fi
) &
watchdog_pid=$!
wait "${cmd_pid}"
status=$?
kill "${watchdog_pid}" 2>/dev/null || true
wait "${watchdog_pid}" 2>/dev/null || true
if [ -f "${timeout_flag}" ]; then
rm -f "${timeout_flag}"
return 124
fi
rm -f "${timeout_flag}"
return "${status}"
}
# Run buddy with the correct PATH and NODE_PATH for pantry resolution
run_buddy() {
BUN_CMD="$1"
shift
exec env PATH="${PANTRY_LOCAL_BIN}:${USER_LOCAL_BIN}:${PATH}" NODE_PATH="${PANTRY_DIR}:${NODE_PATH:-}" "${BUN_CMD}" "${BUDDY_ENTRY}" "$@"
}
# Fast path: if pantry is already set up, run immediately
# Check for pantry/.bin/bun first (managed runtime), then system bun
if [ -x "${PANTRY_LOCAL_BIN}/bun" ]; then
run_buddy "${PANTRY_LOCAL_BIN}/bun" "$@"
fi
if [ -d "${PANTRY_DIR}" ] && command -v bun >/dev/null 2>&1; then
run_buddy bun "$@"
fi
# Pantry not set up — bootstrap it
if ! command -v pantry >/dev/null 2>&1; then
if [ ! -x "${PANTRY_INSTALLER}" ]; then
echo "Pantry installer not found at ${PANTRY_INSTALLER}" >&2
exit 1
fi
run_with_timeout "${PANTRY_BOOTSTRAP_TIMEOUT_SECONDS}" "${PANTRY_INSTALLER}"
export PATH="${USER_LOCAL_BIN}:${PATH}"
fi
run_with_timeout "${PANTRY_DEPENDENCIES_TIMEOUT_SECONDS}" pantry install
# After install, try again
if [ -x "${PANTRY_LOCAL_BIN}/bun" ]; then
run_buddy "${PANTRY_LOCAL_BIN}/bun" "$@"
fi
if command -v bun >/dev/null 2>&1; then
run_buddy bun "$@"
fi
echo "Pantry did not provide a local bun runtime." >&2
exit 1