|
1 | 1 | #!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +if [[ ${OS:-} = Windows_NT ]]; then |
| 5 | + echo 'error: Please install bun using Windows Subsystem for Linux' |
| 6 | + exit 1 |
| 7 | +fi |
2 | 8 |
|
3 | 9 | # Reset |
4 | 10 | Color_Off='' |
5 | 11 |
|
6 | 12 | # Regular Colors |
7 | 13 | Red='' |
8 | 14 | Green='' |
| 15 | +Dim='' # White |
9 | 16 |
|
10 | 17 | # Bold |
11 | | -BWhite='' |
12 | | -BGreen='' |
| 18 | +Bold_White='' |
| 19 | +Bold_Green='' |
13 | 20 |
|
14 | | -Dim='' # White |
15 | | - |
16 | | -if test -t 1; then |
| 21 | +if [[ -t 1 ]]; then |
17 | 22 | # Reset |
18 | 23 | Color_Off='033円[0m' # Text Reset |
19 | 24 |
|
20 | 25 | # Regular Colors |
21 | 26 | Red='033円[0;31m' # Red |
22 | 27 | Green='033円[0;32m' # Green |
23 | | - White='033円[0;37m' # White |
24 | | - |
25 | | - Dim='033円[0;2m' # White |
| 28 | + Dim='033円[0;2m' # White |
26 | 29 |
|
27 | 30 | # Bold |
28 | | - BGreen='033円[1;32m' # Green |
29 | | - BWhite='033円[1;37m' # White |
| 31 | + Bold_Green='033円[1;32m' # Bold Green |
| 32 | + Bold_White='033円[1;37m' # Bold White |
30 | 33 | fi |
31 | 34 |
|
32 | | -if!command -v unzip >/dev/null;then |
33 | | - echo -e "\n${Red}error${Color_Off}: unzip is required to install bun (see: https://github.com/oven-sh/bun#unzip-is-required)."1>&2 |
| 35 | +error() { |
| 36 | + echo -e "${Red}error${Color_Off}:""$@">&2 |
34 | 37 | exit 1 |
35 | | -fi |
| 38 | +} |
36 | 39 |
|
37 | | -if [ "$OS" = "Windows_NT" ]; then |
38 | | - echo "error: Please install bun using Windows Subsystem for Linux." |
39 | | - exit 1 |
40 | | -else |
41 | | - case $(uname -sm) in |
42 | | - "Darwin x86_64") target="darwin-x64" ;; |
43 | | - "Darwin arm64") target="darwin-aarch64" ;; |
44 | | - "Linux aarch64") target="linux-aarch64" ;; |
45 | | - "Linux arm64") target="linux-aarch64" ;; |
46 | | - "Linux x86_64") target="linux-x64" ;; |
47 | | - *) target="linux-x64" ;; |
48 | | - esac |
| 40 | +command -v unzip >/dev/null || |
| 41 | + error 'unzip is required to install bun (see: https://github.com/oven-sh/bun#unzip-is-required)' |
| 42 | + |
| 43 | +if [[ $# -gt 1 ]]; then |
| 44 | + error 'Too many arguments, only 1 is allowed, which can be a specific tag of bun to install (e.g. "bun-v0.1.4")' |
49 | 45 | fi |
50 | 46 |
|
51 | | -if [ "$target" = "darwin-x64" ]; then |
52 | | - # Is it rosetta |
53 | | - sysctl sysctl.proc_translated >/dev/null 2>&1 |
54 | | - if [ $? -eq 0 ]; then |
55 | | - target="darwin-aarch64" |
56 | | - echo -e "$Dim Your shell is running in Rosetta 2. Downloading bun for $target instead. $Color_Off" |
| 47 | +case $(uname -ms) in |
| 48 | +'Darwin x86_64') |
| 49 | + target=darwin-x64 |
| 50 | + ;; |
| 51 | +'Darwin arm64') |
| 52 | + target=darwin-aarch64 |
| 53 | + ;; |
| 54 | +'Linux aarch64' | 'Linux arm64') |
| 55 | + target=linux-aarch64 |
| 56 | + ;; |
| 57 | +'Linux x86_64' | *) |
| 58 | + target=linux-x64 |
| 59 | + ;; |
| 60 | +esac |
| 61 | + |
| 62 | +if [[ $target = darwin-x64 ]]; then |
| 63 | + # Is this process running in Rosetta? |
| 64 | + if [[ $(sysctl -n sysctl.proc_translated) = 1 ]]; then |
| 65 | + target=darwin-aarch64 |
| 66 | + echo -e "${Dim}Your shell is running in Rosetta 2. Downloading bun for $target instead$Color_Off" |
57 | 67 | fi |
58 | 68 | fi |
59 | 69 |
|
60 | | -github_repo="https://github.com/Jarred-Sumner/bun-releases-for-updater" |
| 70 | +github_repo=https://github.com/Jarred-Sumner/bun-releases-for-updater |
61 | 71 |
|
62 | | -if [ $# -eq 0 ]; then |
63 | | - bun_uri="$github_repo/releases/latest/download/bun-${target}.zip" |
| 72 | +if [[ $# = 0 ]]; then |
| 73 | + bun_uri=$github_repo/releases/latest/download/bun-$target.zip |
64 | 74 | else |
65 | | - bun_uri="$github_repo/releases/download/${1}/bun-${target}.zip" |
| 75 | + bun_uri=$github_repo/releases/download/$1/bun-$target.zip |
66 | 76 | fi |
67 | 77 |
|
68 | | -bun_install="${BUN_INSTALL:-$HOME/.bun}" |
69 | | -bin_dir="$bun_install/bin" |
70 | | -exe="$bin_dir/bun" |
| 78 | +install_env=BUN_INSTALL |
| 79 | +bin_env=\$$install_env/bin |
71 | 80 |
|
72 | | -if [ ! -d "$bin_dir" ]; then |
73 | | - mkdir -p "$bin_dir" |
| 81 | +install_dir=${!install_env:-$HOME/.bun} |
| 82 | +bin_dir=$install_dir/bin |
| 83 | +exe=$bin_dir/bun |
74 | 84 |
|
75 | | - if (($?)); then |
76 | | - echo -e "${Red}error${Color_Off}: Failed to create install directory $bin_dir" 1>&2 |
77 | | - exit 1 |
78 | | - fi |
| 85 | +if [[ ! -d $bin_dir ]]; then |
| 86 | + mkdir -p "$bin_dir" || |
| 87 | + error "Failed to create install directory \"$bin_dir\"" |
79 | 88 | fi |
80 | 89 |
|
81 | | -curl --fail --location --progress-bar --output "$exe.zip" "$bun_uri" |
| 90 | +curl --fail --location --progress-bar --output "$exe.zip" "$bun_uri" || |
| 91 | + error "Failed to download bun from \"$bun_uri\"" |
82 | 92 |
|
83 | | -if (($?)); then |
84 | | - echo -e "${Red}error${Color_Off}: Failed to download bun from $bun_uri" 1>&2 |
85 | | - exit 1 |
86 | | -fi |
87 | | -unzip -d "$bin_dir" -q -o "$exe.zip" |
88 | | -if (($?)); then |
89 | | - echo -e "${Red}error${Color_Off}: Failed to extract bun" 1>&2 |
90 | | - exit 1 |
91 | | -fi |
92 | | -mv "$bin_dir/bun-${target}/bun" "$exe" |
93 | | -if (($?)); then |
94 | | - echo -e "${Red}error${Color_Off}: Failed to extract bun" 1>&2 |
95 | | - exit 1 |
96 | | -fi |
97 | | -chmod +x "$exe" |
98 | | -if (($?)); then |
99 | | - echo -e "${Red}error${Color_Off}: Failed to set permissions on bun executable." 1>&2 |
100 | | - exit 1 |
101 | | -fi |
102 | | -rmdir $bin_dir/bun-${target} |
103 | | -rm "$exe.zip" |
| 93 | +unzip -oqd "$bin_dir" "$exe.zip" || |
| 94 | + error 'Failed to extract bun' |
| 95 | + |
| 96 | +mv "$bin_dir/bun-$target/bun" "$exe" || |
| 97 | + error 'Failed to move extracted bun to destination' |
| 98 | + |
| 99 | +chmod +x "$exe" || |
| 100 | + error 'Failed to set permissions on bun executable' |
104 | 101 |
|
105 | | -echo -e "${Green}bun was installed successfully to ${BGreen}$exe$Color_Off" |
| 102 | +rm -r "$bin_dir/bun-$target""$exe.zip" |
106 | 103 |
|
107 | | -if command -v bun --version >/dev/null; then |
| 104 | +tildify() { |
| 105 | + if [[ 1ドル = $HOME/* ]]; then |
| 106 | + local replacement=\~/ |
| 107 | + |
| 108 | + echo "${1/$HOME\//$replacement}" |
| 109 | + else |
| 110 | + echo "1ドル" |
| 111 | + fi |
| 112 | +} |
| 113 | + |
| 114 | +echo -e "${Green}bun was installed successfully to $Bold_Green$(tildify "$exe")$Color_Off" |
| 115 | + |
| 116 | +if command -v bun >/dev/null; then |
108 | 117 | # Install completions, but we don't care if it fails |
109 | | - IS_BUN_AUTO_UPDATE="true" $exe completions >/dev/null 2>&1 |
| 118 | + IS_BUN_AUTO_UPDATE=true $exe completions &>/dev/null ||: |
110 | 119 |
|
111 | 120 | echo "Run 'bun --help' to get started" |
112 | | - exit 0 |
| 121 | + exit |
| 122 | +fi |
| 123 | + |
| 124 | +refresh_command='' |
| 125 | + |
| 126 | +tilde_bin_dir=$(tildify "$bin_dir") |
| 127 | +quoted_install_dir=\"${install_dir//\"/\\\"}\" |
| 128 | + |
| 129 | +if [[ $quoted_install_dir = \"$HOME/* ]]; then |
| 130 | + quoted_install_dir=${quoted_install_dir/$HOME\//\$HOME/} |
113 | 131 | fi |
114 | 132 |
|
115 | | -if test $(basename $SHELL) == "fish"; then |
| 133 | +echo |
| 134 | + |
| 135 | +case $(basename "$SHELL") in |
| 136 | +fish) |
116 | 137 | # Install completions, but we don't care if it fails |
117 | | - IS_BUN_AUTO_UPDATE="true" SHELL="fish" $exe completions >/dev/null 2>&1 |
118 | | - if test -f $HOME/.config/fish/config.fish; then |
119 | | - echo -e "\n# bun\nset -Ux BUN_INSTALL \"$bun_install\"" >>"$HOME/.config/fish/config.fish" |
120 | | - echo -e "fish_add_path \"$bin_dir\"\n" >>"$HOME/.config/fish/config.fish" |
121 | | - echo "" |
122 | | - echo -e "$Dim Added \"$bin_dir\" to \$PATH in \"\~/.config/fish/config.fish\"$Color_Off" |
123 | | - echo "" |
124 | | - echo -e "To get started, run" |
125 | | - echo -e "$BWhite" |
126 | | - echo -e " source ~/.config/fish/config.fish" |
127 | | - echo -e " bun --help$Color_Off" |
128 | | - exit 0 |
| 138 | + IS_BUN_AUTO_UPDATE=true SHELL=fish $exe completions &>/dev/null || : |
| 139 | + |
| 140 | + commands=( |
| 141 | + "set --export $install_env $quoted_install_dir" |
| 142 | + "set --export PATH $bin_env \$PATH" |
| 143 | + ) |
| 144 | + |
| 145 | + fish_config=$HOME/.config/fish/config.fish |
| 146 | + tilde_fish_config=$(tildify "$fish_config") |
| 147 | + |
| 148 | + if [[ -w $fish_config ]]; then |
| 149 | + { |
| 150 | + echo -e '\n# bun' |
| 151 | + |
| 152 | + for command in "${commands[@]}"; do |
| 153 | + echo "$command" |
| 154 | + done |
| 155 | + } >>"$fish_config" |
| 156 | + |
| 157 | + echo -e "${Dim}Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_fish_config\"$Color_Off" |
| 158 | + |
| 159 | + refresh_command="source $tilde_fish_config" |
129 | 160 | else |
130 | | - echo "" |
131 | | - echo "Manually add the directory to your \$HOME/.config/fish/config.fish (or similar)" |
132 | | - echo "" |
133 | | - echo -e " $BWhite set -Ux BUN_INSTALL \"$bun_install\"$Color_Off" |
134 | | - echo -e " $BWhite fish_add_path \"$bin_dir\"$Color_Off" |
135 | | - echo "" |
| 161 | + echo "Manually add the directory to $tilde_fish_config (or similar):" |
| 162 | + |
| 163 | + for command in "${commands[@]}"; do |
| 164 | + echo -e " $Bold_White $command$Color_Off" |
| 165 | + done |
136 | 166 | fi |
137 | | -elif |
138 | | - test $(basename $SHELL) == "zsh" |
139 | | -then |
| 167 | + ;; |
| 168 | +zsh) |
140 | 169 | # Install completions, but we don't care if it fails |
141 | | - IS_BUN_AUTO_UPDATE="true" SHELL="zsh" $exe completions >/dev/null 2>&1 |
142 | | - |
143 | | - if test -f $HOME/.zshrc; then |
144 | | - echo -e "\n# bun\nexport BUN_INSTALL=\"$bun_install\"" >>"$HOME/.zshrc" |
145 | | - echo -e "export PATH=\"\$BUN_INSTALL/bin:\$PATH\"" >>"$HOME/.zshrc" |
146 | | - echo "" |
147 | | - echo -e "$Dim Added \"$bin_dir\" to \$PATH in \"~/.zshrc\"$Color_Off" |
148 | | - |
149 | | - echo "" |
150 | | - echo -e "To get started, run" |
151 | | - echo -e "$BWhite" |
152 | | - echo -e " exec $SHELL" |
153 | | - echo -e " bun --help$Color_Off" |
154 | | - echo "" |
155 | | - exit 0 |
| 170 | + IS_BUN_AUTO_UPDATE=true SHELL=zsh $exe completions &>/dev/null || : |
| 171 | + |
| 172 | + commands=( |
| 173 | + "export $install_env=$quoted_install_dir" |
| 174 | + "export PATH=\"$bin_env:\$PATH\"" |
| 175 | + ) |
| 176 | + |
| 177 | + zsh_config=$HOME/.zshrc |
| 178 | + tilde_zsh_config=$(tildify "$zsh_config") |
| 179 | + |
| 180 | + if [[ -w $zsh_config ]]; then |
| 181 | + { |
| 182 | + echo -e '\n# bun' |
| 183 | + |
| 184 | + for command in "${commands[@]}"; do |
| 185 | + echo "$command" |
| 186 | + done |
| 187 | + } >>"$zsh_config" |
| 188 | + |
| 189 | + echo -e "${Dim}Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_zsh_config\"$Color_Off" |
| 190 | + |
| 191 | + refresh_command="exec $SHELL" |
156 | 192 | else |
157 | | - echo "" |
158 | | - echo "Manually add the directory to your \$HOME/.zshrc (or similar)" |
159 | | - echo "" |
160 | | - echo -e " $BWhite export BUN_INSTALL=\"$bun_install\"$Color_Off" |
161 | | - echo -e " $BWhite export PATH=\"\$BUN_INSTALL/bin:\$PATH\"$Color_Off" |
162 | | - fi |
| 193 | + echo "Manually add the directory to $tilde_zsh_config (or similar):" |
163 | 194 |
|
164 | | -else |
165 | | - echo "" |
166 | | - echo "Manually add the directory to your \$HOME/.bashrc (or similar)" |
167 | | - echo "" |
168 | | - echo -e " $BWhiteexport export BUN_INSTALL=\"$bun_install\"$Color_Off" |
169 | | - echo -e " $BWhiteexport export PATH=\"\$BUN_INSTALL/bin:\$PATH\"$Color_Off" |
| 195 | + for command in "${commands[@]}"; do |
| 196 | + echo -e " $Bold_White $command$Color_Off" |
| 197 | + done |
| 198 | + fi |
| 199 | + ;; |
| 200 | +*) |
| 201 | + echo 'Manually add the directory to ~/.bashrc (or similar):' |
| 202 | + echo -e " $Bold_White export $install_env=$quoted_install_dir$Color_Off" |
| 203 | + echo -e " $Bold_White export PATH=\"$bin_env:\$PATH\"$Color_Off" |
| 204 | + ;; |
| 205 | +esac |
| 206 | + |
| 207 | +echo |
| 208 | +echo -e "To get started, run:$Bold_White" |
| 209 | + |
| 210 | +if [[ $refresh_command ]]; then |
| 211 | + echo -e " $refresh_command" |
170 | 212 | fi |
171 | | -echo "" |
172 | | -echo -e "To get started, run" |
173 | | -echo -e "$BWhite" |
| 213 | + |
174 | 214 | echo -e " bun --help$Color_Off" |
0 commit comments