Update 05.04.2026
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
# nvm plugin
|
||||
|
||||
This plugin adds autocompletions for [nvm](https://github.com/nvm-sh/nvm) — a Node.js version manager.
|
||||
It also automatically sources nvm, so you don't need to do it manually in your `.zshrc`.
|
||||
This plugin adds autocompletions for [nvm](https://github.com/nvm-sh/nvm) — a Node.js version manager. It also
|
||||
automatically sources nvm, so you don't need to do it manually in your `.zshrc`.
|
||||
|
||||
To use it, add `nvm` to the plugins array of your zshrc file:
|
||||
|
||||
@@ -21,14 +21,40 @@ These settings should go in your zshrc file, before Oh My Zsh is sourced:
|
||||
[Homebrew is installed in `/opt/homebrew`](https://docs.brew.sh/Installation). To get the directory where
|
||||
nvm has been installed, regardless of chip architecture, use `NVM_HOMEBREW=$(brew --prefix nvm)`.
|
||||
|
||||
- **`NVM_LAZY`**: if you want the plugin to defer the load of nvm to speed-up the start of your zsh session,
|
||||
set `NVM_LAZY` to `1`. This will source nvm script only when using it, and will create a function for `node`,
|
||||
`npm`, `pnpm`, `yarn`, and the command(s) specified by `NVM_LAZY_CMD`, so when you call either of them,
|
||||
nvm will be loaded and run with default version.
|
||||
## Customization
|
||||
|
||||
- **`NVM_LAZY_CMD`**: if you want additional command(s) to trigger lazy loading of nvm, set `NVM_LAZY_CMD` to
|
||||
the command or an array of the commands.
|
||||
#### Lazy startup
|
||||
|
||||
- **`NVM_AUTOLOAD`**: if `NVM_AUTOLOAD` is set to `1`, the plugin will automatically load a node version when
|
||||
if finds a [`.nvmrc` file](https://github.com/nvm-sh/nvm#nvmrc) in the current working directory indicating
|
||||
which node version to load.
|
||||
This option will help you to defer nvm's load until you use it to speed-up your zsh startup. This will source
|
||||
nvm script only when using it, and will create a function for `node`, `npm`, `npx`, `pnpm`, `pnpx`, `yarn`,
|
||||
`corepack` and the command(s) specified by `lazy-cmd` option, so when you call either of them, nvm will be
|
||||
loaded and run with default version. To enable it, you can add this snippet to your zshrc, before Oh My Zsh is
|
||||
sourced:
|
||||
|
||||
```zsh
|
||||
zstyle ':omz:plugins:nvm' lazy yes
|
||||
```
|
||||
|
||||
Then, to define extra commands that will also trigger nvm load, you can use a similar syntax, adding as many
|
||||
as you want:
|
||||
|
||||
```zsh
|
||||
zstyle ':omz:plugins:nvm' lazy-cmd eslint prettier typescript ...
|
||||
```
|
||||
|
||||
There will be a function `_omz_nvm_load` available to load `nvm` without executing any other trigger command.
|
||||
|
||||
#### `.nvmrc` autoload
|
||||
|
||||
Note: _if used at the same time as `lazy`, `autoload` will start working only after nvm has been lazy-loaded_
|
||||
|
||||
If set, the plugin will automatically load a node version when if finds a
|
||||
[`.nvmrc` file](https://github.com/nvm-sh/nvm#nvmrc) in the current working directory indicating which node
|
||||
version to load. This can be done, similar as previous options, adding:
|
||||
|
||||
```zsh
|
||||
zstyle ':omz:plugins:nvm' autoload yes
|
||||
zstyle ':omz:plugins:nvm' silent-autoload yes # optionally remove the output generated by NVM when autoloading
|
||||
```
|
||||
|
||||
Note: _this will not remove regular `nvm` output_
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
#compdef nvm
|
||||
#autoload
|
||||
|
||||
[[ -f "$NVM_DIR/nvm.sh" ]] || return 0
|
||||
|
||||
local -a _1st_arguments
|
||||
_1st_arguments=(
|
||||
'help:show help'
|
||||
'--version:print out the latest released version of nvm'
|
||||
'install:download and install a version in <node|iojs|node version number>'
|
||||
'install-latest-npm:download and install the latest npm version'
|
||||
'uninstall:uninstall a version'
|
||||
'use:modify PATH to use <version>. Uses .nvmrc if available'
|
||||
'exec:run <command> on <version>. Uses .nvmrc if available'
|
||||
'run:run `node` on <version> with <args> as arguments. Uses .nvmrc if available'
|
||||
'current:list installed versions'
|
||||
'ls:list installed versions or versions matching a given description'
|
||||
'version:resolve the given description to a single local version'
|
||||
'version-remote:resolve the given description to a single remote version'
|
||||
'ls-remote:list remote versions available for install'
|
||||
'deactivate:undo effects of `nvm` on current shell'
|
||||
'alias:show or set aliases'
|
||||
'unalias:deletes an alias'
|
||||
'reinstall-packages:reinstall global `npm` packages contained in <version> to current version'
|
||||
'unload:unload `nvm` from shell'
|
||||
'which:display path to installed node version. Uses .nvmrc if available'
|
||||
)
|
||||
|
||||
_arguments -C '*:: :->subcmds' && return 0
|
||||
|
||||
if (( CURRENT == 1 )); then
|
||||
_describe -t commands "nvm subcommand" _1st_arguments
|
||||
return
|
||||
fi
|
||||
@@ -1,3 +1,7 @@
|
||||
# Don't try to load nvm if command already available
|
||||
# Note: nvm is a function so we need to use `which`
|
||||
which nvm &>/dev/null && return
|
||||
|
||||
# See https://github.com/nvm-sh/nvm#installation-and-update
|
||||
if [[ -z "$NVM_DIR" ]]; then
|
||||
if [[ -d "$HOME/.nvm" ]]; then
|
||||
@@ -12,43 +16,51 @@ if [[ -z "$NVM_DIR" ]]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
# Don't try to load nvm if command already available
|
||||
# Note: nvm is a function so we need to use `which`
|
||||
which nvm &>/dev/null && return
|
||||
|
||||
if (( $+NVM_LAZY )); then
|
||||
# Call nvm when first using nvm, node, npm, pnpm, yarn or $NVM_LAZY_CMD
|
||||
function nvm node npm pnpm yarn $NVM_LAZY_CMD {
|
||||
unfunction nvm node npm pnpm yarn $NVM_LAZY_CMD
|
||||
# Load nvm if it exists in $NVM_DIR
|
||||
[[ -f "$NVM_DIR/nvm.sh" ]] && source "$NVM_DIR/nvm.sh"
|
||||
"$0" "$@"
|
||||
}
|
||||
elif [[ -f "$NVM_DIR/nvm.sh" ]]; then
|
||||
# Load nvm if it exists in $NVM_DIR
|
||||
source "$NVM_DIR/nvm.sh"
|
||||
else
|
||||
if [[ -z "$NVM_DIR" ]] || [[ ! -f "$NVM_DIR/nvm.sh" ]]; then
|
||||
return
|
||||
fi
|
||||
|
||||
# Autoload nvm when finding a .nvmrc file in the current directory
|
||||
# Adapted from: https://github.com/nvm-sh/nvm#zsh
|
||||
if (( $+NVM_AUTOLOAD )); then
|
||||
load-nvmrc() {
|
||||
local node_version="$(nvm version)"
|
||||
function _omz_nvm_setup_completion {
|
||||
local _nvm_completion
|
||||
# Load nvm bash completion
|
||||
for _nvm_completion in "$NVM_DIR/bash_completion" "$NVM_HOMEBREW/etc/bash_completion.d/nvm"; do
|
||||
if [[ -f "$_nvm_completion" ]]; then
|
||||
# Load bashcompinit
|
||||
autoload -U +X bashcompinit && bashcompinit
|
||||
# Bypass compinit call in nvm bash completion script. See:
|
||||
# https://github.com/nvm-sh/nvm/blob/4436638/bash_completion#L86-L93
|
||||
ZSH_VERSION= source "$_nvm_completion"
|
||||
break
|
||||
fi
|
||||
done
|
||||
unfunction _omz_nvm_setup_completion
|
||||
}
|
||||
|
||||
function _omz_nvm_setup_autoload {
|
||||
if ! zstyle -t ':omz:plugins:nvm' autoload; then
|
||||
unfunction _omz_nvm_setup_autoload
|
||||
return
|
||||
fi
|
||||
|
||||
# Autoload nvm when finding a .nvmrc file in the current directory
|
||||
# Adapted from: https://github.com/nvm-sh/nvm#zsh
|
||||
function load-nvmrc {
|
||||
local nvmrc_path="$(nvm_find_nvmrc)"
|
||||
local nvm_silent=""
|
||||
zstyle -t ':omz:plugins:nvm' silent-autoload && nvm_silent="--silent"
|
||||
|
||||
if [[ -n "$nvmrc_path" ]]; then
|
||||
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
|
||||
local nvmrc_node_version=$(nvm version $(command cat "$nvmrc_path" | tr -dc '[:print:]'))
|
||||
|
||||
if [[ "$nvmrc_node_version" = "N/A" ]]; then
|
||||
nvm install
|
||||
elif [[ "$nvmrc_node_version" != "$node_version" ]]; then
|
||||
nvm use
|
||||
elif [[ "$nvmrc_node_version" != "$(nvm version)" ]]; then
|
||||
nvm use $nvm_silent
|
||||
fi
|
||||
elif [[ "$node_version" != "$(nvm version default)" ]]; then
|
||||
echo "Reverting to nvm default version"
|
||||
nvm use default
|
||||
elif [[ -n "$(PWD=$OLDPWD nvm_find_nvmrc)" ]] && [[ "$(nvm version)" != "$(nvm version default)" ]]; then
|
||||
[[ -z $nvm_silent ]] && echo "Reverting to nvm default version"
|
||||
|
||||
nvm use default $nvm_silent
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -56,18 +68,32 @@ if (( $+NVM_AUTOLOAD )); then
|
||||
add-zsh-hook chpwd load-nvmrc
|
||||
|
||||
load-nvmrc
|
||||
unfunction _omz_nvm_setup_autoload
|
||||
}
|
||||
|
||||
if zstyle -t ':omz:plugins:nvm' lazy; then
|
||||
# Call nvm when first using nvm, node, npm, pnpm, yarn, corepack or other commands in lazy-cmd
|
||||
zstyle -a ':omz:plugins:nvm' lazy-cmd nvm_lazy_cmd
|
||||
nvm_lazy_cmd=(_omz_nvm_load nvm node npm npx pnpm pnpx yarn corepack $nvm_lazy_cmd) # default values
|
||||
eval "
|
||||
function $nvm_lazy_cmd {
|
||||
for func in $nvm_lazy_cmd; do
|
||||
if (( \$+functions[\$func] )); then
|
||||
unfunction \$func
|
||||
fi
|
||||
done
|
||||
# Load nvm if it exists in \$NVM_DIR
|
||||
[[ -f \"\$NVM_DIR/nvm.sh\" ]] && source \"\$NVM_DIR/nvm.sh\"
|
||||
_omz_nvm_setup_completion
|
||||
_omz_nvm_setup_autoload
|
||||
if [[ \"\$0\" != _omz_nvm_load ]]; then
|
||||
\"\$0\" \"\$@\"
|
||||
fi
|
||||
}
|
||||
"
|
||||
unset nvm_lazy_cmd
|
||||
else
|
||||
source "$NVM_DIR/nvm.sh"
|
||||
_omz_nvm_setup_completion
|
||||
_omz_nvm_setup_autoload
|
||||
fi
|
||||
|
||||
# Load nvm bash completion
|
||||
for nvm_completion in "$NVM_DIR/bash_completion" "$NVM_HOMEBREW/etc/bash_completion.d/nvm"; do
|
||||
if [[ -f "$nvm_completion" ]]; then
|
||||
# Load bashcompinit
|
||||
autoload -U +X bashcompinit && bashcompinit
|
||||
# Bypass compinit call in nvm bash completion script. See:
|
||||
# https://github.com/nvm-sh/nvm/blob/4436638/bash_completion#L86-L93
|
||||
ZSH_VERSION= source "$nvm_completion"
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
unset NVM_HOMEBREW NVM_LAZY NVM_AUTOLOAD nvm_completion
|
||||
|
||||
Reference in New Issue
Block a user