diff --git a/bash_completion b/bash_completion index 9dbd50ff..4faffbbb 100644 --- a/bash_completion +++ b/bash_completion @@ -107,6 +107,7 @@ __nvm_parse_context() { --no-alias) no_alias='' ;; -s | -b) build_mode='' ;; --save | -w) save='' ;; + --delete-prefix) delete_prefix='' ;; --default | --alias=*) alias_target='' ;; --reinstall-packages-from=* | --copy-packages-from=*) reinstall='' ;; -*) ;; @@ -131,7 +132,7 @@ __nvm_options() { uninstall | use) flags="${only}" if (( arg_count == 0 )); then flags="${flags} ${lts_flags}"; fi - if [ "${subcommand}" = use ]; then flags="${flags} --silent ${save}"; fi + if [ "${subcommand}" = use ]; then flags="${flags} --silent ${save} ${delete_prefix}"; fi ;; exec | run) flags="--silent ${lts_flags} ${only}" ;; ls | list) @@ -186,6 +187,7 @@ __nvm_candidates() { local arg_count=0 end_options=0 local only='--only=' lts='--lts --lts=' no_alias='--no-alias' local build_mode='-s -b' save='--save' alias_target='--alias= --default' reinstall='--reinstall-packages-from=' + local delete_prefix='--delete-prefix' local NVM_ONLY_SELECTED="${NVM_ONLY_SELECTED-}" __nvm_parse_context || return 0 if (( ! end_options )); then diff --git a/nvm.sh b/nvm.sh index 190223f4..0bfc2cdd 100755 --- a/nvm.sh +++ b/nvm.sh @@ -5416,6 +5416,7 @@ nvm() { nvm_echo ' --lts= Uses automatic alias for provided LTS line, if available.' nvm_echo ' --only= Only select from these version categories (see above)' nvm_echo ' --save Writes the specified version to .nvmrc.' + nvm_echo ' --delete-prefix Removes `prefix` and `globalconfig` from the user, global and builtin npmrc files instead of erroring' nvm_echo ' nvm exec [current | ] [] Run on . Uses .nvmrc if version is omitted; otherwise errors.' nvm_echo ' The following optional arguments, if provided, must appear directly after `nvm exec`:' nvm_echo ' --silent Silences stdout/stderr output' diff --git a/test/fast/Unit tests/bash_completion offers nvm use --delete-prefix b/test/fast/Unit tests/bash_completion offers nvm use --delete-prefix new file mode 100755 index 00000000..186aed63 --- /dev/null +++ b/test/fast/Unit tests/bash_completion offers nvm use --delete-prefix @@ -0,0 +1,53 @@ +#!/bin/sh +# shellcheck shell=bash + +{ [ -n "${BASH_VERSION-}" ] && ! shopt -oq posix; } || exec bash "$0" "$@" +set -e + +die() { printf '%s\n' "$*" >&2; exit 1; } + +root="$(cd "$(dirname "$0")/../../.." && pwd)" +NVM_DIR="$(mktemp -d)" +export NVM_DIR +trap 'rm -rf "${NVM_DIR}"; [ -n "${finished-}" ]' EXIT +# shellcheck disable=SC1091 +. "${root}/nvm.sh" --no-use +# shellcheck disable=SC1091 +. "${root}/bash_completion" + +complete_for() { + COMP_WORDS=("$@") + COMP_CWORD=$(( $# - 1 )) + COMPREPLY=(stale) + __nvm +} + +offers() { + local reply + for reply in "${COMPREPLY[@]}"; do + [ "${reply}" = '--delete-prefix' ] && return 0 + done + return 1 +} + +complete_for nvm use --de +[ "${COMPREPLY[*]}" = '--delete-prefix' ] || die "nvm use --de: got [${COMPREPLY[*]}]" + +complete_for nvm use 22 --de +[ "${COMPREPLY[*]}" = '--delete-prefix' ] || die "nvm use 22 --de: got [${COMPREPLY[*]}]" + +complete_for nvm use --silent - +offers || die "nvm use --silent -: --delete-prefix missing from [${COMPREPLY[*]}]" + +complete_for nvm use --delete-prefix - +! offers || die 'offered --delete-prefix twice' + +complete_for nvm use -- --de +! offers || die 'offered --delete-prefix after --' + +for cmd in install uninstall exec run; do + complete_for nvm "${cmd}" --de + ! offers || die "offered --delete-prefix to nvm ${cmd}" +done + +finished=1