Skip to content

Instantly share code, notes, and snippets.

@v3rse
Last active April 3, 2024 04:59
Show Gist options
  • Save v3rse/5620a2913aaa2f9b7a93cf52f72f74e9 to your computer and use it in GitHub Desktop.
Save v3rse/5620a2913aaa2f9b7a93cf52f72f74e9 to your computer and use it in GitHub Desktop.
A faster NVM shell integration for Fish shell. No need for bass (DEPENDENCIES: https://github.com/jorgebucaran/nvm.fish)
# loader functions. place this in your fish functions directory. call it in your config.fish file
function load_nvm --on-variable="PWD"
set -l default_node_version "system"
set -l node_version (nvm current)
set -l nvmrc_path (nvm_find_nvmrc)
if test -n "$nvmrc_path"
set -l nvmrc_node_version (nvm_find_nvmrc_version)
if test "$nvmrc_node_version" != "$node_version"
set -l use_status (nvm use $nvmrc_node_version 2>&1)
if test $status != 0; and string match -q "*installed first*" $use_status
echo "Node version $nvmrc_node_version not installed. Installing..."
nvm install $nvmrc_node_version
else
nvm use $nvmrc_node_version
end
end
else if test "$node_version" != "$default_node_version"
echo "Reverting to default Node version"
nvm use $default_node_version
end
end
# utility functions. place this in your fish functions directory
function nvm_find_up
set _nvm_rc_path_ $PWD
while test -n "$_nvm_rc_path_" -a ! -f "$_nvm_rc_path_/$argv"
set -l parent_dir (dirname "$_nvm_rc_path_")
if test "$parent_dir" = "$_nvm_rc_path_"
break
end
set _nvm_rc_path_ $parent_dir
end
nvm_echo "$_nvm_rc_path_"
end
function nvm_find_nvmrc
set -l dir (nvm_find_up '.nvmrc')
if test -e "$dir/.nvmrc"
nvm_echo "$dir/.nvmrc"
end
end
function nvm_find_nvmrc_version
set -l nvmrc_path (nvm_find_nvmrc)
if test -e "$nvmrc_path"
set -l nvm_nvmrc_version (command head -n 1 "$nvmrc_path" | command tr -d '\r')
nvm_echo "$nvm_nvmrc_version"
end
end
function nvm_echo
command printf "%s\n" $argv
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment