Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·536 lines (479 loc) · 19.9 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·536 lines (479 loc) · 19.9 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#!/usr/bin/env bash
# bun-node — transparent node/npm/npx/yarn → bun shims
# Installs into bun's own bin dir — no PATH changes needed.
#
# Install: curl -fsSL https://raw.githubusercontent.com/bethropolis/bun-node/main/install.sh | bash
# Uninstall: bun-node-uninstall
# Update: bun-node-update
set -euo pipefail
# ── Config ───────────────────────────────────────────────────────────────────
SPOOF_NODE_VERSION="v22.14.0"
SPOOF_NPM_VERSION="10.9.2"
SPOOF_YARN_VERSION="1.22.22"
# ── Colours ──────────────────────────────────────────────────────────────────
if [ -t 1 ]; then
BOLD='\033[1m'; DIM='\033[2m'; RED='\033[0;31m'
GREEN='\033[0;32m'; YELLOW='\033[1;33m'; CYAN='\033[0;36m'; RESET='\033[0m'
else
BOLD=''; DIM=''; RED=''; GREEN=''; YELLOW=''; CYAN=''; RESET=''
fi
step() { printf "${CYAN} →${RESET} %s\n" "$*"; }
ok() { printf "${GREEN} ✓${RESET} %s\n" "$*"; }
warn() { printf "${YELLOW} !${RESET} %s\n" "$*" >&2; }
fatal() { printf "${RED} ✗${RESET} %s\n" "$*" >&2; exit 1; }
# ── Preflight ────────────────────────────────────────────────────────────────
echo ""
printf "${BOLD}bun-node installer${RESET}${DIM} node/npm/npx/yarn → bun${RESET}\n"
echo "──────────────────────────────────────────"
echo ""
command -v bun &>/dev/null \
|| fatal "bun is not installed. Get it at https://bun.sh"
BUN_BIN_PATH="$(command -v bun)"
BUN_VERSION="$(bun --version 2>/dev/null)"
ok "Found bun ${BUN_VERSION} at ${BUN_BIN_PATH}"
# Derive BIN_DIR from where bun actually lives — not a hardcoded default.
# This handles non-standard installs (Homebrew, Nix, custom prefix, etc.)
BIN_DIR="$(dirname "$BUN_BIN_PATH")"
ok "Install target: ${BIN_DIR}"
# Warn if the derived dir isn't in PATH (shouldn't happen, but be safe).
if ! echo "$PATH" | tr ':' '\n' | grep -qxF "$BIN_DIR"; then
warn "$BIN_DIR is not in your PATH — shims will be written but may not be active."
fi
# Detect existing shims and offer to update rather than blindly overwrite.
EXISTING=()
for shim in node npm npx yarn; do
[ -f "$BIN_DIR/$shim" ] && EXISTING+=("$shim")
done
if [ "${#EXISTING[@]}" -gt 0 ]; then
printf "${YELLOW} !${RESET} Existing shims found: %s\n" "${EXISTING[*]}" >&2
printf " Re-running will update them in place. Continue? [Y/n] "
read -r reply </dev/tty
case "${reply:-Y}" in
[Yy]*|"") : ;;
*) echo "Aborted."; exit 0 ;;
esac
fi
# ── Shared helpers written into each shim ────────────────────────────────────
# BIN_DIR is baked in at install time so shims can exclude themselves from PATH
# without hardcoding a path that might differ from where bun actually lives.
SHIM_BIN_DIR="$BIN_DIR"
# ── Write shim: node ─────────────────────────────────────────────────────────
step "Writing node shim"
cat > "$BIN_DIR/node" << SHIM
#!/usr/bin/env bash
# bun-node shim: node → bun
# https://github.com/bethropolis/bun-node
[ "\${BUN_NODE_DEBUG:-}" = "1" ] && echo "[bun-node] node \$*" >&2
# Absolute path to bun, baked in at install time.
_bun="${BUN_BIN_PATH}"
[ -x "\$_bun" ] || { echo "bun-node: bun not found at \$_bun" >&2; exit 127; }
# Read .node-version or .nvmrc from cwd upward for per-project version spoofing.
_spoof_version() {
local dir="\$PWD"
while [ "\$dir" != "/" ]; do
if [ -f "\$dir/.node-version" ]; then
cat "\$dir/.node-version"; return
elif [ -f "\$dir/.nvmrc" ]; then
cat "\$dir/.nvmrc"; return
fi
dir="\$(dirname "\$dir")"
done
echo "\${BUN_NODE_SPOOF_VERSION:-${SPOOF_NODE_VERSION}}"
}
case "\${1-}" in
--version|-v)
ver="\$(_spoof_version)"
# Normalise: ensure it starts with v
[[ "\$ver" == v* ]] || ver="v\$ver"
echo "\$ver"
;;
--print|-p)
shift
exec "\$_bun" -e "console.log(\$1)"
;;
-e|--eval)
shift
exec "\$_bun" -e "\$@"
;;
*)
exec "\$_bun" "\$@"
;;
esac
SHIM
# ── Write shim: npm ──────────────────────────────────────────────────────────
step "Writing npm shim"
cat > "$BIN_DIR/npm" << SHIM
#!/usr/bin/env bash
# bun-node shim: npm → bun
# https://github.com/bethropolis/bun-node
[ "\${BUN_NODE_DEBUG:-}" = "1" ] && echo "[bun-node] npm \$*" >&2
_bun="${BUN_BIN_PATH}"
[ -x "\$_bun" ] || { echo "bun-node: bun not found at \$_bun" >&2; exit 127; }
# Find the real npm by excluding our shim dir from PATH.
_real_npm() {
PATH="\$(echo "\$PATH" | tr ':' '\n' | grep -vxF "${SHIM_BIN_DIR}" | tr '\n' ':')" command -v npm 2>/dev/null
}
# Translate npm install flags → bun add flags.
# Handles: --save-dev/-D, --save-optional/-O, --save-exact/-E, --global/-g
_translate_install_flags() {
local args=() pkg_args=()
local is_global=0
while [ "\$#" -gt 0 ]; do
case "\$1" in
--save-dev|-D) pkg_args+=("-d") ;;
--save-optional|-O) pkg_args+=("--optional") ;;
--save-exact|-E) pkg_args+=("--exact") ;;
--global|-g) is_global=1 ;;
--save|-S) : ;; # default in bun, no-op
--no-save) pkg_args+=("--no-save") ;;
*) args+=("\$1") ;;
esac
shift
done
if [ "\$is_global" = "1" ]; then
echo "g \${pkg_args[*]-} \${args[*]-}"
else
echo " \${pkg_args[*]-} \${args[*]-}"
fi
}
case "\${1-}" in
# ── Version / meta ──────────────────────────────────────────────────────
--version|-v)
echo "\${BUN_NODE_SPOOF_NPM_VERSION:-${SPOOF_NPM_VERSION}}"
;;
# ── Install ─────────────────────────────────────────────────────────────
install|i)
shift
if [ "\$#" -eq 0 ]; then
exec "\$_bun" install
else
translated="\$(_translate_install_flags "\$@")"
is_global="\${translated%% *}"
rest="\${translated#* }"
if [ "\$is_global" = "g" ]; then
# shellcheck disable=SC2086
exec "\$_bun" add --global \$rest
else
# shellcheck disable=SC2086
exec "\$_bun" add \$rest
fi
fi
;;
# ── ci (frozen install — critical for CI pipelines) ─────────────────────
ci)
exec "\$_bun" install --frozen-lockfile
;;
# ── Remove ──────────────────────────────────────────────────────────────
uninstall|remove|rm|un|r)
shift; exec "\$_bun" remove "\$@"
;;
# ── List ────────────────────────────────────────────────────────────────
ls|list|ll|la)
shift; exec "\$_bun" pm ls "\$@"
;;
# ── Link ────────────────────────────────────────────────────────────────
link)
shift; exec "\$_bun" link "\$@"
;;
unlink)
shift; exec "\$_bun" unlink "\$@"
;;
# ── Scripts ─────────────────────────────────────────────────────────────
run|run-script)
shift; exec "\$_bun" run "\$@"
;;
start)
exec "\$_bun" run start
;;
test|t)
shift; exec "\$_bun" test "\$@"
;;
# ── Update ──────────────────────────────────────────────────────────────
update|up|upgrade)
shift; exec "\$_bun" update "\$@"
;;
# ── Exec / dlx ──────────────────────────────────────────────────────────
exec|x)
shift; exec "\$_bun" x "\$@"
;;
# ── Init ────────────────────────────────────────────────────────────────
init)
shift; exec "\$_bun" init "\$@"
;;
# ── node-gyp: native addons can't run under bun — fail clearly ──────────
# (npm install of a gyp package triggers this internally)
node-gyp)
echo "bun-node: native addons (.node files / node-gyp) are not supported by bun." >&2
echo "bun-node: You'll need Node.js for this package." >&2
exit 1
;;
# ── Publish / Pack ──────────────────────────────────────────────────────
publish)
shift; exec "\$_bun" publish "\$@"
;;
pack)
shift; exec "\$_bun" pm pack "\$@"
;;
# ── Audit ───────────────────────────────────────────────────────────────
audit)
exec "\$_bun" audit
;;
# ── Whoami ──────────────────────────────────────────────────────────────
whoami)
exec "\$_bun" pm whoami
;;
# ── Commands bun doesn't support — fall back to real npm ────────────────
deprecate|owner|team|org|hook|fund|token|profile|stars|adduser|login|logout|ping|access|dist-tag|shrinkwrap|doctor)
_npm="\$(_real_npm)"
if [ -n "\$_npm" ]; then
echo "bun-node: '\$1' is unsupported by bun — delegating to real npm at \$_npm" >&2
exec "\$_npm" "\$@"
else
echo "bun-node: 'npm \$1' is not supported by bun and no real npm was found." >&2
echo "bun-node: Install npm (https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) to use this command." >&2
exit 1
fi
;;
# ── Prefix ──────────────────────────────────────────────────────
prefix)
shift
if [ "\${1-}" = "-g" ] || [ "\${1-}" = "--global" ]; then
dirname "\$(bun pm bin -g 2>/dev/null)"
else
dir="\$PWD"
while [ "\$dir" != "/" ]; do
if [ -f "\$dir/package.json" ] || [ -d "\$dir/node_modules" ]; then
echo "\$dir"
break
fi
dir="\$(dirname "\$dir")"
done
[ "\$dir" = "/" ] && echo "\$PWD"
fi
;;
# ── Passthrough ─────────────────────────────────────────────────────────
*)
exec "\$_bun" "\$@"
;;
esac
SHIM
# ── Write shim: npx ──────────────────────────────────────────────────────────
step "Writing npx shim"
cat > "$BIN_DIR/npx" << SHIM
#!/usr/bin/env bash
# bun-node shim: npx → bun x
# https://github.com/bethropolis/bun-node
[ "\${BUN_NODE_DEBUG:-}" = "1" ] && echo "[bun-node] npx \$*" >&2
_bun="${BUN_BIN_PATH}"
[ -x "\$_bun" ] || { echo "bun-node: bun not found at \$_bun" >&2; exit 127; }
case "\${1-}" in
--version|-v)
echo "\${BUN_NODE_SPOOF_NPM_VERSION:-${SPOOF_NPM_VERSION}}"
;;
# bunx never needs -y / --yes — strip silently.
-y|--yes)
shift; exec "\$_bun" x "\$@"
;;
# --no-install: honour intent with --prefer-offline.
--no-install)
shift; exec "\$_bun" x --prefer-offline "\$@"
;;
# node-gyp: fail clearly rather than letting bun give a confusing error.
node-gyp)
echo "bun-node: native addons (.node files / node-gyp) are not supported by bun." >&2
echo "bun-node: You'll need Node.js for this package." >&2
exit 1
;;
*)
exec "\$_bun" x "\$@"
;;
esac
SHIM
# ── Write shim: yarn ─────────────────────────────────────────────────────────
step "Writing yarn shim"
cat > "$BIN_DIR/yarn" << SHIM
#!/usr/bin/env bash
# bun-node shim: yarn → bun
# https://github.com/bethropolis/bun-node
[ "\${BUN_NODE_DEBUG:-}" = "1" ] && echo "[bun-node] yarn \$*" >&2
_bun="${BUN_BIN_PATH}"
[ -x "\$_bun" ] || { echo "bun-node: bun not found at \$_bun" >&2; exit 127; }
_real_yarn() {
PATH="\$(echo "\$PATH" | tr ':' '\n' | grep -vxF "${SHIM_BIN_DIR}" | tr '\n' ':')" command -v yarn 2>/dev/null
}
case "\${1-}" in
--version|-v)
echo "\${BUN_NODE_SPOOF_YARN_VERSION:-${SPOOF_YARN_VERSION}}"
;;
# bare "yarn" with no args = install
"")
exec "\$_bun" install
;;
add)
shift; exec "\$_bun" add "\$@"
;;
remove|rm)
shift; exec "\$_bun" remove "\$@"
;;
install)
shift; exec "\$_bun" install "\$@"
;;
run)
shift; exec "\$_bun" run "\$@"
;;
# bare "yarn <script>" — yarn allows calling scripts without "run"
# Try bun run; if the script doesn't exist bun will error naturally.
start|build|test|dev|lint|format|clean|typecheck|check)
exec "\$_bun" run "\$@"
;;
upgrade|up)
shift; exec "\$_bun" update "\$@"
;;
global)
# "yarn global add <pkg>" → "bun add --global <pkg>"
sub="\${2-}"
shift 2 || shift
case "\$sub" in
add) exec "\$_bun" add --global "\$@" ;;
remove) exec "\$_bun" remove --global "\$@" ;;
*) exec "\$_bun" "\$sub" "\$@" ;;
esac
;;
dlx)
shift; exec "\$_bun" x "\$@"
;;
exec)
shift; exec "\$_bun" x "\$@"
;;
init)
shift; exec "\$_bun" init "\$@"
;;
link)
shift; exec "\$_bun" link "\$@"
;;
unlink)
shift; exec "\$_bun" unlink "\$@"
;;
info)
shift; exec "\$_bun" pm "\$@"
;;
# Commands with no bun equivalent — fall back to real yarn if present.
publish|pack|login|logout|owner|team|workspace|workspaces|policies|why|outdated|licenses|audit|import|create)
_yarn="\$(_real_yarn)"
if [ -n "\$_yarn" ]; then
echo "bun-node: '\$1' unsupported by bun — delegating to real yarn at \$_yarn" >&2
exec "\$_yarn" "\$@"
else
echo "bun-node: 'yarn \$1' is not supported by bun and no real yarn was found." >&2
exit 1
fi
;;
# Unknown: pass straight through and let bun decide.
*)
exec "\$_bun" run "\$@"
;;
esac
SHIM
chmod +x "$BIN_DIR/node" "$BIN_DIR/npm" "$BIN_DIR/npx" "$BIN_DIR/yarn"
ok "Shims written and made executable"
# ── Write: bun-node status ───────────────────────────────────────────────────
step "Writing bun-node status"
cat > "$BIN_DIR/bun-node" << SHIM
#!/usr/bin/env bash
# bun-node — meta command for status, update, and help
# https://github.com/bethropolis/bun-node
BIN_DIR="${SHIM_BIN_DIR}"
if [ -t 1 ]; then
BOLD='\033[1m'; DIM='\033[2m'; GREEN='\033[0;32m'
YELLOW='\033[1;33m'; CYAN='\033[0;36m'; RED='\033[0;31m'; RESET='\033[0m'
else
BOLD=''; DIM=''; GREEN=''; YELLOW=''; CYAN=''; RED=''; RESET=''
fi
_shim_ok() {
[ -f "\$BIN_DIR/\$1" ] && echo "${GREEN}✓${RESET}" || echo "${RED}✗${RESET}"
}
case "\${1-status}" in
status)
echo ""
printf "\${BOLD}bun-node\${RESET}\${DIM} node/npm/npx/yarn → bun\${RESET}\n"
echo "──────────────────────────────────────────"
echo ""
printf " \${DIM}bun\${RESET} \$(command -v bun 2>/dev/null || echo 'not found') v\$(bun --version 2>/dev/null || echo '?')\n"
printf " \${DIM}shim dir\${RESET} \$BIN_DIR\n"
echo ""
printf " \$(_shim_ok node) node "
[ -f "\$BIN_DIR/node" ] \
&& printf "\${DIM}→ bun\${RESET} (spoof: \$(BUN_NODE_SPOOF_VERSION='' node --version 2>/dev/null || echo '?'))\n" \
|| printf "\${RED}not installed\${RESET}\n"
printf " \$(_shim_ok npm) npm "
[ -f "\$BIN_DIR/npm" ] \
&& printf "\${DIM}→ bun\${RESET} (spoof: \$(BUN_NODE_SPOOF_NPM_VERSION='' npm --version 2>/dev/null || echo '?'))\n" \
|| printf "\${RED}not installed\${RESET}\n"
printf " \$(_shim_ok npx) npx "
[ -f "\$BIN_DIR/npx" ] \
&& printf "\${DIM}→ bunx\${RESET}\n" \
|| printf "\${RED}not installed\${RESET}\n"
printf " \$(_shim_ok yarn) yarn "
[ -f "\$BIN_DIR/yarn" ] \
&& printf "\${DIM}→ bun\${RESET} (spoof: \$(BUN_NODE_SPOOF_YARN_VERSION='' yarn --version 2>/dev/null || echo '?'))\n" \
|| printf "\${RED}not installed\${RESET}\n"
echo ""
printf " \${DIM}debug\${RESET} export BUN_NODE_DEBUG=1 (logs every translation)\n"
echo ""
;;
help|-h|--help)
echo ""
printf " \${BOLD}bun-node\${RESET} <command>\n\n"
printf " status show installed shims and versions (default)\n"
printf " help this message\n"
echo ""
;;
*)
echo "bun-node: unknown command '\$1'. Run 'bun-node status' or 'bun-node help'." >&2
exit 1
;;
esac
SHIM
chmod +x "$BIN_DIR/bun-node"
ok "bun-node status command written"
# ── Write: bun-node-uninstall ─────────────────────────────────────────────────
step "Writing uninstaller"
cat > "$BIN_DIR/bun-node-uninstall" << UNINSTALL
#!/usr/bin/env bash
set -euo pipefail
BIN_DIR="${SHIM_BIN_DIR}"
echo "Removing bun-node shims from \$BIN_DIR ..."
rm -f "\$BIN_DIR/node" "\$BIN_DIR/npm" "\$BIN_DIR/npx" "\$BIN_DIR/yarn" \
"\$BIN_DIR/bun-node" "\$BIN_DIR/bun-node-uninstall"
echo "Done. bun itself is untouched."
UNINSTALL
chmod +x "$BIN_DIR/bun-node-uninstall"
# ── Write: bun-node-update ────────────────────────────────────────────────────
step "Writing updater"
cat > "$BIN_DIR/bun-node-update" << 'UPDATE'
#!/usr/bin/env bash
set -euo pipefail
echo "Updating bun-node..."
curl -fsSL https://raw.githubusercontent.com/bethropolis/bun-node/main/install.sh | bash
UPDATE
chmod +x "$BIN_DIR/bun-node-update"
ok "Updater written"
# ── Done ─────────────────────────────────────────────────────────────────────
echo ""
echo "──────────────────────────────────────────"
printf "${GREEN}${BOLD} bun-node installed!${RESET}\n"
echo "──────────────────────────────────────────"
echo ""
printf " ${DIM}shims${RESET} node npm npx yarn\n"
printf " ${DIM}location${RESET} $BIN_DIR\n"
printf " ${DIM}PATH${RESET} no changes — bun already owns this dir\n"
echo ""
printf " Verify:\n"
printf " ${BOLD}bun-node status${RESET}\n"
echo ""
printf " Debug mode:\n"
printf " ${BOLD}BUN_NODE_DEBUG=1 npm install${RESET}\n"
echo ""
printf " To update: ${BOLD}bun-node-update${RESET}\n"
printf " To uninstall: ${BOLD}bun-node-uninstall${RESET}\n"
echo ""