Skip to content

Instantly share code, notes, and snippets.

@GalassoLuca
Last active December 16, 2021 10:37
Show Gist options
  • Save GalassoLuca/36d0ae8ead141275feaba285863fede5 to your computer and use it in GitHub Desktop.
Save GalassoLuca/36d0ae8ead141275feaba285863fede5 to your computer and use it in GitHub Desktop.
Load node version with fnm and automatically taking the node version contained in the first upwards .nvmrc (or .node-version) file
# Load .nvmrc file using fnm
## Description: load nvmrc by automatically taking the node version contained in the first upwards .nvmrc (or .node-version) file
## See GIST/upfind.sh https://gist.github.com/GalassoLuca/a502c3ba2489b6fae7270f5653bedfc0
local last_nvmrc_path=null
load-nvmrc() {
local nvmrc_path="$(upfind .nvmrc .node-version)"
if [ "$nvmrc_path" = "$last_nvmrc_path" ]; then
return
fi
if [ -z "$nvmrc_path" ]; then
fnm use default
else
local version="$(cat $nvmrc_path)"
fnm use $version
fi
last_nvmrc_path="$nvmrc_path"
}
load-nvmrc "$@"
## Usage
# load-nvmrc
## Automatic loading of nvmrc
# add to ~/.zshrc the following lines:
# # Autoload .nvmrc with fnm when changing directory
# add-zsh-hook chpwd load-nvmrc
# autoload -U add-zsh-hook
# load-nvmrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment