forked from divad12/khan-dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmac-shared-functions.sh
More file actions
128 lines (116 loc) · 4.87 KB
/
mac-shared-functions.sh
File metadata and controls
128 lines (116 loc) · 4.87 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
#!/bin/bash
# Shared functions for mac setup scripts. Source this file after
# sourcing shared-functions.sh.
install_mise_mac() {
# This deactivates Mise's automatic activation for Fish as we want to
# use shims and not Homebrew's auto-activated default!
# See: https://mise.jdx.dev/configuration.html#mise-fish-auto-activate-1
if [ "$(basename "$SHELL")" = "fish" ]; then
fish --command "set -U MISE_FISH_AUTO_ACTIVATE 0"
fi
if ! which mise >/dev/null 2>&1; then
info "Installing mise\n"
brew install mise
else
info "Updating mise\n"
brew upgrade mise
success "mise updated to $(mise --version)"
fi
}
# Returns true if a binary is on PATH and not provided by mise shims.
_non_mise_binary_on_path() {
local bin="$1"
local bin_path
bin_path="$(which "$bin" 2>/dev/null)" || return 1
[[ "$bin_path" == "$HOME/.local/share/mise/shims/"* ]] && return 1
return 0
}
uninstall_node_mac() {
# --- Detect phase: determine what changes are needed ---
local has_node_brew=false has_node16_brew=false has_node20_brew=false
local has_nvm_brew=false has_fnm_brew=false has_nvm_dir=false
local fish_has_fnm=false
local -a rc_files_with_nvm=()
local fish_config=~/.config/fish/config.fish
brew ls --versions node >/dev/null 2>&1 && has_node_brew=true
brew ls --versions node@16 >/dev/null 2>&1 && has_node16_brew=true
brew ls --versions node@20 >/dev/null 2>&1 && has_node20_brew=true
brew ls --versions nvm >/dev/null 2>&1 && has_nvm_brew=true
brew ls --versions fnm >/dev/null 2>&1 && has_fnm_brew=true
[ -d "$HOME/.nvm" ] && has_nvm_dir=true
[ -f "$fish_config" ] && grep -qF 'fnm env --use-on-cd | source' "$fish_config" && fish_has_fnm=true
for rcfile in ~/.bashrc ~/.bash_profile ~/.zshrc ~/.zprofile; do
if [ -f "$rcfile" ] && grep -qE '^export NVM_DIR=|^\[ -s "\$NVM_DIR|^eval "\$\(fnm env' "$rcfile"; then
rc_files_with_nvm+=("$rcfile")
fi
done
# --- Build list of pending changes ---
local -a changes=()
$has_node_brew && changes+=("Uninstall node (brew formula)")
$has_node16_brew && changes+=("Uninstall node@16 (brew formula)")
$has_node20_brew && changes+=("Uninstall node@20 (brew formula)")
$has_nvm_brew && changes+=("Uninstall nvm (brew formula)")
$has_fnm_brew && changes+=("Uninstall fnm (brew formula)")
for rcfile in "${rc_files_with_nvm[@]}"; do
changes+=("Remove nvm/fnm config lines from $rcfile")
done
$fish_has_fnm && changes+=("Remove fnm config line from $fish_config")
$has_nvm_dir && changes+=("Remove ~/.nvm directory")
if [ ${#changes[@]} -eq 0 ]; then
# Check if node or pnpm still exist on the current path
if _non_mise_binary_on_path node || _non_mise_binary_on_path pnpm; then
error "node and/or pnpm are still on the current PATH but no brew-managed installations were found."
exit 1
fi
return
fi
# --- Prompt phase: show list and ask once ---
notice "The following changes will be made to remove old Node.js installations:"
for change in "${changes[@]}"; do
notice " - $change"
done
echo
if [ "$(get_yn_input "Proceed with the above changes?" y)" != "y" ]; then
return
fi
# --- Execute phase ---
if $has_node_brew; then
# We _try_ to uninstall node, but if it's depended on by other pacakges,
# we can't. So in that case we fall back to simply unlinking it so it no
# longer appears in the `bin` folder for Homebrew. Packages that depend
# on it will continue to work after unlinking it.
if ! (brew uninstall node > /dev/null 2>&1); then
warn "We could not uninstall the Homebrew version of node. We will unlink it so that it does not conflict with the Mise-managed version that will be installed."
brew unlink node
fi
fi
if $has_node16_brew; then
brew uninstall node@16
fi
if $has_node20_brew; then
brew uninstall node@20
fi
if $has_nvm_brew; then
brew uninstall nvm
fi
if $has_fnm_brew; then
brew uninstall fnm
fi
for rcfile in "${rc_files_with_nvm[@]}"; do
sed -i '' '/^export NVM_DIR="\$HOME\/\.nvm"/d' "$rcfile"
sed -i '' '/^\[ -s "\$NVM_DIR\/nvm\.sh" \]/d' "$rcfile"
sed -i '' '/^\[ -s "\$NVM_DIR\/bash_completion" \]/d' "$rcfile"
sed -i '' '/^eval "\$(fnm env --use-on-cd)"/d' "$rcfile"
done
if $fish_has_fnm; then
sed -i '' '/^fnm env --use-on-cd | source/d' "$fish_config"
fi
if $has_nvm_dir; then
rm -rf "$HOME/.nvm"
fi
# Check if node or pnpm still exist on the current path
if _non_mise_binary_on_path node || _non_mise_binary_on_path pnpm; then
error "node and/or pnpm are still on the current PATH. Uninstall may have failed."
exit 1
fi
}