From d8fc8a0838b952e0463182556353e8c1286dc807 Mon Sep 17 00:00:00 2001 From: Andrea Ghensi Date: Sun, 4 Dec 2022 17:36:10 +0100 Subject: [PATCH] plugins/nvm: add nvmrc autoload option fixes #371 --- plugins/nvm/README.md | 11 +++++++ plugins/nvm/nvm.plugin.sh | 62 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/plugins/nvm/README.md b/plugins/nvm/README.md index 882187d24..2e7c674f9 100644 --- a/plugins/nvm/README.md +++ b/plugins/nvm/README.md @@ -36,5 +36,16 @@ completions=( ) ``` +## `.nvmrc` autoload + +If set, the plugin will automatically load a node version when if finds a +`.nvmrc` file[3] in the current working directory indicating which node version to load. +This can be done adding the following to your `.bashrc`: + +```bash +export OMB_PLUGIN_NVM_AUTO_USE=true +``` + [1]: https://github.com/nvm-sh/nvm [2]: https://github.com/nvm-sh/nvm#manual-install +[3]: https://github.com/nvm-sh/nvm#nvmrc diff --git a/plugins/nvm/nvm.plugin.sh b/plugins/nvm/nvm.plugin.sh index 068ce1d2c..e3e07fd7f 100644 --- a/plugins/nvm/nvm.plugin.sh +++ b/plugins/nvm/nvm.plugin.sh @@ -1,4 +1,7 @@ #! bash oh-my-bash.module +# Description: automatically load nvm +# +# @var[opt] OMB_PLUGIN_NVM_AUTO_USE enable .nvmrc autoload # Set NVM_DIR if it isn't already defined [[ -z "$NVM_DIR" ]] && export NVM_DIR="$HOME/.nvm" @@ -6,7 +9,60 @@ # Try to load nvm only if command not already available if ! _omb_util_command_exists nvm; then - [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm - # This is done as part of completions!!! - # [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion + # shellcheck source=/dev/null + [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" +fi + +# Optional .nvmrc autoload +if _omb_util_command_exists nvm && [[ ${OMB_PLUGIN_NVM_AUTO_USE} == true ]]; then + find-up () { + path=$(pwd) + while [[ "$path" != "" && ! -e "$path/$1" ]]; do + path=${path%/*} + done + echo "$path" + } + + cdnvm(){ + cd "$@" || return $? + nvm_path=$(find-up .nvmrc | tr -d '\n') + + # If there are no .nvmrc file, use the default nvm version + if [[ ! $nvm_path = *[^[:space:]]* ]]; then + + declare default_version; + default_version=$(nvm version default); + + # If there is no default version, set it to `node` + # This will use the latest version on your machine + if [[ $default_version == "N/A" ]]; then + nvm alias default node; + default_version=$(nvm version default); + fi + + # If the current version is not the default version, set it to use the default version + if [[ $(nvm current) != "$default_version" ]]; then + nvm use default; + fi + + elif [[ -s $nvm_path/.nvmrc && -r $nvm_path/.nvmrc ]]; then + declare nvm_version + nvm_version=$(<"$nvm_path"/.nvmrc) + + # Add the `v` suffix if it does not exists in the .nvmrc file + if [[ $nvm_version != v* ]]; then + nvm_version="v""$nvm_version" + fi + + # If it is not already installed, install it + if [[ $(nvm ls "$nvm_version" | tr -d '[:space:]') == "N/A" ]]; then + nvm install "$nvm_version"; + fi + + if [[ $(nvm current) != "$nvm_version" ]]; then + nvm use "$nvm_version"; + fi + fi + } + alias cd='cdnvm' fi