Last active
May 13, 2024 11:20
-
-
Save casaper/629274f86493c899b8b04bcc086cb1ec to your computer and use it in GitHub Desktop.
Direnv project config handler for fnm (Fast Node Manager)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Automatically install fnm (Fast Node Manager) and the correct Node version. | |
# [fnm (Fast Node Manager)](https://github.com/Schniz/fnm) performs the same function as [nvm](https://nvm.sh/), but is a bit faster. | |
use_fnm() { | |
local version_file="${1:-.nvmrc}" | |
if ! has fnm; then | |
if [[ "$OSTYPE" == darwin* ]] && has brew; then | |
brew install fnm | |
elif has cargo; then | |
cargo install fnm | |
elif has curl; then | |
curl -fsSL https://fnm.vercel.app/install | bash | |
elif has wget; then | |
wget -qO- https://fnm.vercel.app/install | bash | |
else | |
echo "Could not determine a way to automatically install fnm (Fast Node Manager) for you." | |
echo "Refer to the fnm readme for instructions on how to install it on your system: https://github.com/Schniz/fnm?tab=readme-ov-file#installation" | |
return 1 | |
fi | |
# Try to automatically load fnm from the shell config for ZSH or Bash. | |
if [[ "$SHELL" == *zsh ]]; then | |
add_fnm_eval ~/.zshrc | |
elif [[ "$SHELL" == *bash ]]; then | |
add_fnm_eval ~/.bashrc | |
add_fnm_eval ~/.bash_profile | |
else | |
echo "Your shell '$SHELL' cannot automatically install fnm (Fast Node Manager)." | |
echo "Refer to the fnm readme for instructions on how to install it on your system: https://github.com/Schniz/fnm?tab=readme-ov-file#installation" | |
eval "$(fnm env --use-on-cd)" | |
fi | |
fi | |
# Install the version configured in .nvmrc | |
fnm install < "$version_file" | |
# Get yarn with corepack, if the shell has no yarn available. | |
if ! has yarn; then | |
corepack enable yarn | |
fi | |
# Add “$PWD/node_modules/.bin” to the PATH environment variable. | |
layout node | |
} | |
add_fnm_eval() { | |
local target_file="$1" | |
if [[ -f "$target_file" ]]; then | |
echo -e $'\neval "$(fnm env --use-on-cd)"\n' >> "$target_file" | |
source "$target_file" | |
else | |
echo "Could not add fnm init to $target_file" | |
eval "$(fnm env --use-on-cd)" | |
fi | |
} | |
watch_file .nvmrc .node-version | |
[ -f .nvmrc ] && use_fnm .nvmrc | |
[ -f .node-version ] && use_fnm .node-version |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fnm (Fast Node Manager) performs the same function as nvm, but is a bit faster.