mirror of
https://github.com/ohmybash/oh-my-bash.git
synced 2024-05-11 05:55:37 +00:00
plugins/nvm: Fix docs, clean up code, and add license
Co-authored-by: Koichi Murase <myoga.murase@gmail.com>
This commit is contained in:
committed by
Koichi Murase
parent
fadaaa1263
commit
16da2bdc54
@@ -38,12 +38,12 @@ completions=(
|
||||
|
||||
## `.nvmrc` autoload
|
||||
|
||||
If set, the plugin will automatically load a node version when if finds a
|
||||
If set, the plugin will automatically load a node version when it 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`:
|
||||
This can be done by adding the following to your `.bashrc`:
|
||||
|
||||
```bash
|
||||
export OMB_PLUGIN_NVM_AUTO_USE=true
|
||||
OMB_PLUGIN_NVM_AUTO_USE=true
|
||||
```
|
||||
|
||||
[1]: https://github.com/nvm-sh/nvm
|
||||
|
@@ -10,59 +10,90 @@
|
||||
# Try to load nvm only if command not already available
|
||||
if ! _omb_util_command_exists nvm; then
|
||||
# shellcheck source=/dev/null
|
||||
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
|
||||
[[ -s $NVM_DIR/nvm.sh ]] && source "$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%/*}
|
||||
#
|
||||
# This part is originally from the official nvm documentation
|
||||
# (README.md). Here is the license of the original project:
|
||||
#
|
||||
# The MIT License (MIT)
|
||||
#
|
||||
# Copyright (c) 2010 Tim Caswell
|
||||
# Copyright (c) 2014 Jordan Harband
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person
|
||||
# obtaining a copy of this software and associated documentation files
|
||||
# (the "Software"), to deal in the Software without restriction,
|
||||
# including without limitation the rights to use, copy, modify, merge,
|
||||
# publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
# and to permit persons to whom the Software is furnished to do so,
|
||||
# subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be
|
||||
# included in all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
#
|
||||
if _omb_util_command_exists nvm && [[ ${OMB_PLUGIN_NVM_AUTO_USE-} == true ]]; then
|
||||
_omb_plugin_nvm_find_up() {
|
||||
local path=$PWD
|
||||
while [[ $path && ! -e $path/$1 ]]; do
|
||||
path=${path%/*}
|
||||
done
|
||||
echo "$path"
|
||||
}
|
||||
|
||||
cdnvm(){
|
||||
cd "$@" || return $?
|
||||
nvm_path=$(find-up .nvmrc | tr -d '\n')
|
||||
_omb_plugin_nvm_cd(){
|
||||
cd "$@" || return "$?"
|
||||
local nvm_path=$(_omb_plugin_nvm_find_up .nvmrc)
|
||||
|
||||
# If there are no .nvmrc file, use the default nvm version
|
||||
if [[ ! $nvm_path = *[^[:space:]]* ]]; then
|
||||
if [[ $nvm_path != *[^[:space:]]* ]]; then
|
||||
|
||||
declare default_version;
|
||||
default_version=$(nvm version default);
|
||||
local 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);
|
||||
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;
|
||||
nvm use default
|
||||
fi
|
||||
|
||||
elif [[ -s $nvm_path/.nvmrc && -r $nvm_path/.nvmrc ]]; then
|
||||
declare nvm_version
|
||||
nvm_version=$(<"$nvm_path"/.nvmrc)
|
||||
local 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"
|
||||
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";
|
||||
if nvm ls "$nvm_version" | grep -qx '[[:space:]]*N/A[[:space:]]*'; then
|
||||
nvm install "$nvm_version"
|
||||
fi
|
||||
|
||||
if [[ $(nvm current) != "$nvm_version" ]]; then
|
||||
nvm use "$nvm_version";
|
||||
nvm use "$nvm_version"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
alias cd='cdnvm'
|
||||
alias cd='_omb_plugin_nvm_cd'
|
||||
fi
|
||||
#------------------------------------------------------------------------------
|
||||
|
Reference in New Issue
Block a user