Files
easyzsh/install_fnm.sh
T
Jeffrey 42c8305871 fix(fnm): load managed Node from always-on zsh layer
fnm lived in interactive-only ~/.config/zshrc, so ssh/agents/zsh -c
missed fnm-managed node. Source ~/.config/zsh/*.zsh from .zshenv and
install the fragment as zsh/fnm instead.
2026-08-07 14:08:07 +08:00

104 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
# Function to detect the operating system
detect_os() {
if [[ "$OSTYPE" == "darwin"* ]]; then
echo "macos"
elif [[ -f /etc/os-release ]]; then
. /etc/os-release
echo "$ID"
else
echo "unknown"
fi
}
run_as_root() {
if (( EUID == 0 )); then
"$@"
elif command -v sudo &> /dev/null; then
sudo "$@"
else
echo "Error: sudo is required to install unzip." >&2
exit 1
fi
}
install_unzip() {
command -v unzip &> /dev/null && return
echo "Installing required dependency: unzip"
case $(detect_os) in
ubuntu|debian)
run_as_root apt-get update
run_as_root apt-get install -y unzip
;;
fedora)
run_as_root dnf install -y unzip
;;
centos|rhel)
if command -v dnf &> /dev/null; then
run_as_root dnf install -y unzip
else
run_as_root yum install -y unzip
fi
;;
arch|manjaro)
run_as_root pacman -S --needed --noconfirm unzip
;;
*)
echo "Error: unzip is required. Install it with your package manager and retry." >&2
exit 1
;;
esac
}
# Install fnm (Fast Node Manager) if it is not already installed
if command -v fnm &> /dev/null; then
echo -e "fnm is already installed\n"
fnm --version
else
echo -e "Installing fnm (Fast Node Manager)\n"
os=$(detect_os)
if [[ "$os" == "macos" ]] && command -v brew &> /dev/null; then
echo "Installing fnm via Homebrew..."
brew install fnm
else
install_unzip
install_dir="${XDG_DATA_HOME:-$HOME/.local/share}/fnm"
installer_args=(--install-dir "$install_dir" --skip-shell)
[[ "$os" == "macos" ]] && installer_args+=(--force-install)
echo "Installing fnm via official installer..."
curl -fsSL https://fnm.vercel.app/install | bash -s -- "${installer_args[@]}"
export PATH="$install_dir:$PATH"
fi
command -v fnm &> /dev/null
eval "$(fnm env --use-on-cd --shell bash)"
fi
# OPTIONAL: Install the latest LTS version of Node.js
# Uncomment the following lines if you would like to automatically install it
# echo "Installing latest LTS Node.js version..."
# fnm install --lts
# fnm use lts-latest
# fnm default lts-latest
# Always-on fragment: ~/.config/zsh/fnm.zsh (sourced from ~/.zshenv)
curl -fsSL https://git.miomio.moe/mio/easyzsh/raw/branch/master/patch.sh | bash -s -- zsh/fnm
# Drop legacy interactive-only copy if present
rm -f "${XDG_CONFIG_HOME:-$HOME/.config}/zshrc/fnm.zsh"
echo -e "\nfnm installation completed!"
echo -e "To start using fnm, restart your terminal or run: source ~/.zshenv"
echo -e "\nUseful fnm commands:"
echo -e " fnm install --lts # Install latest LTS Node.js"
echo -e " fnm use lts-latest # Use the latest LTS release"
echo -e " fnm default lts-latest # Set the latest LTS as default"
echo -e " fnm list # List installed versions"
echo -e " fnm list-remote # List available versions"