diff --git a/README.md b/README.md index 365b8e4..6676ffa 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,8 @@ - [Automatically call `nvm use`](#automatically-call-nvm-use) - [zsh](#zsh) - [Calling `nvm use` automatically in a directory with a `.nvmrc` file](#calling-nvm-use-automatically-in-a-directory-with-a-nvmrc-file) + - [fish](#fish) + - [Calling `nvm use` automatically in a directory with a `.nvmrc` file](#calling-nvm-use-automatically-in-a-directory-with-a-nvmrc-file-1) - [License](#license) - [Running Tests](#running-tests) - [Bash Completion](#bash-completion) @@ -536,6 +538,45 @@ add-zsh-hook chpwd load-nvmrc load-nvmrc ``` +#### fish + +##### Calling `nvm use` automatically in a directory with a `.nvmrc` file +This requires that you have [bass](https://github.com/edc/bass) installed. +```fish +# ~/.config/fish/functions/nvm.fish +function nvm + bass source ~/.nvm/nvm.sh --no-use ';' nvm $argv +end + +# ~/.config/fish/functions/nvm_find_nvmrc.fish +function nvm_find_nvmrc + bass source ~/.nvm/nvm.sh --no-use ';' nvm_find_nvmrc +end + +# ~/.config/fish/functions/load_nvm.fish +function load_nvm --on-variable="PWD" + set -l default_node_version (nvm version default) + set -l node_version (nvm version) + set -l nvmrc_path (nvm_find_nvmrc) + if test -n "$nvmrc_path" + set -l nvmrc_node_version (nvm version (cat $nvmrc_path)) + if test "$nvmrc_node_version" = "N/A" + nvm install $nvmrc_node_version + else if test nvmrc_node_version != node_version + nvm use $nvmrc_node_version + end + else if test "$node_version" != "$default_node_version" + echo "Reverting to default Node version" + nvm use default + end +end + +# ~/.config/fish/config.fish +# You must call it on initialization or listening to directory switching won't work +load_nvm +``` + + ## License nvm is released under the MIT license.