Last active
December 16, 2021 10:37
-
-
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
This file contains 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
# 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