Files
easyzsh/install_fnm.sh
T
Frank Qing 76395923ce fix(install): make setup failures explicit and atomic
Installer pipelines masked dependency and download failures, causing
false success messages and unsafe config replacement.

Atomic updates and current upstream installers keep failures visible.
2026-07-15 00:16:33 +08:00

102 lines
3.0 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
# Apply the fnm zsh configuration patch (downloads zshrc/fnm.zsh to ~/.config/zshrc/fnm.zsh)
curl -fsSL https://git.miomio.moe/mio/easyzsh/raw/branch/master/patch.sh | bash -s -- fnm
echo -e "\nfnm installation completed!"
echo -e "To start using fnm, restart your terminal or run: source ~/.zshrc"
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"