Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make .nvmrc more versatile/modular #3235

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
52 changes: 42 additions & 10 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -461,17 +461,24 @@ nvm_find_up() {

nvm_find_nvmrc() {
local dir
dir="$(nvm_find_up '.nvmrc')"
if [ -e "${dir}/.nvmrc" ]; then
nvm_echo "${dir}/.nvmrc"
local rcfile="${1:-.nvmrc}"
dir="$(nvm_find_up "${rcfile}")"
if [ -e "${dir}/${rcfile}" ]; then
nvm_echo "${dir}/${rcfile}"
fi
}

# Obtain nvm version from rc file
nvm_rc_version() {
export NVM_RC_VERSION=''
local NVMRC_PATH
NVMRC_PATH="$(nvm_find_nvmrc)"

if [ -n "$1" ]; then
NVMRC_PATH="$(nvm_find_nvmrc "$1")"
else
NVMRC_PATH="$(nvm_find_nvmrc)"
fi

if [ ! -e "${NVMRC_PATH}" ]; then
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
nvm_err "No .nvmrc file found"
Expand Down Expand Up @@ -3136,6 +3143,7 @@ nvm() {
local PROVIDED_REINSTALL_PACKAGES_FROM
local REINSTALL_PACKAGES_FROM
local SKIP_DEFAULT_PACKAGES
local NVM_RC_PATH

while [ $# -ne 0 ]; do
case "$1" in
Expand Down Expand Up @@ -3230,6 +3238,10 @@ nvm() {
SKIP_DEFAULT_PACKAGES=true
shift
;;
--config=*)
NVM_RC_PATH="${1#--config=}"
shift
;;
*)
break # stop parsing args
;;
Expand All @@ -3251,7 +3263,7 @@ nvm() {
shift
fi
else
nvm_rc_version
nvm_rc_version "$NVM_RC_PATH"
if [ $version_not_provided -eq 1 ] && [ -z "${NVM_RC_VERSION}" ]; then
unset NVM_RC_VERSION
>&2 nvm --help
Expand Down Expand Up @@ -3606,6 +3618,7 @@ nvm() {
local NVM_LTS
local IS_VERSION_FROM_NVMRC
IS_VERSION_FROM_NVMRC=0
local NVM_RC_PATH

while [ $# -ne 0 ]; do
case "$1" in
Expand All @@ -3617,6 +3630,10 @@ nvm() {
--) ;;
--lts) NVM_LTS='*' ;;
--lts=*) NVM_LTS="${1##--lts=}" ;;
--config=*)
NVM_RC_PATH="${1#--config=}"
shift
;;
--*) ;;
*)
if [ -n "${1-}" ]; then
Expand All @@ -3630,7 +3647,7 @@ nvm() {
if [ -n "${NVM_LTS-}" ]; then
VERSION="$(nvm_match_version "lts/${NVM_LTS:-*}")"
elif [ -z "${PROVIDED_VERSION-}" ]; then
NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version
NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version "$NVM_RC_PATH"
if [ -n "${NVM_RC_VERSION-}" ]; then
PROVIDED_VERSION="${NVM_RC_VERSION}"
IS_VERSION_FROM_NVMRC=1
Expand Down Expand Up @@ -3740,6 +3757,7 @@ nvm() {
local NVM_SILENT
local NVM_SILENT_ARG
local NVM_LTS
local NVM_RC_PATH
while [ $# -gt 0 ]; do
case "$1" in
--silent)
Expand All @@ -3749,6 +3767,10 @@ nvm() {
;;
--lts) NVM_LTS='*' ; shift ;;
--lts=*) NVM_LTS="${1##--lts=}" ; shift ;;
--config=*)
NVM_RC_PATH="${1#--config=}"
shift
;;
*)
if [ -n "$1" ]; then
break
Expand All @@ -3760,7 +3782,7 @@ nvm() {
done

if [ $# -lt 1 ] && [ -z "${NVM_LTS-}" ]; then
NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version && has_checked_nvmrc=1
NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version "$NVM_RC_PATH" && has_checked_nvmrc=1
if [ -n "${NVM_RC_VERSION-}" ]; then
VERSION="$(nvm_version "${NVM_RC_VERSION-}")" ||:
fi
Expand All @@ -3778,7 +3800,7 @@ nvm() {
if [ "_${VERSION:-N/A}" = '_N/A' ] && ! nvm_is_valid_version "${provided_version}"; then
provided_version=''
if [ $has_checked_nvmrc -ne 1 ]; then
NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version && has_checked_nvmrc=1
NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version "$NVM_RC_PATH" && has_checked_nvmrc=1
fi
provided_version="${NVM_RC_VERSION}"
IS_VERSION_FROM_NVMRC=1
Expand Down Expand Up @@ -3816,11 +3838,16 @@ nvm() {
"exec")
local NVM_SILENT
local NVM_LTS
local NVM_RC_PATH
while [ $# -gt 0 ]; do
case "$1" in
--silent) NVM_SILENT=1 ; shift ;;
--lts) NVM_LTS='*' ; shift ;;
--lts=*) NVM_LTS="${1##--lts=}" ; shift ;;
--config=*)
NVM_RC_PATH="${1#--config=}"
shift
;;
--) break ;;
--*)
nvm_err "Unsupported option \"$1\"."
Expand All @@ -3844,7 +3871,7 @@ nvm() {
elif [ -n "${provided_version}" ]; then
VERSION="$(nvm_version "${provided_version}")" ||:
if [ "_${VERSION}" = '_N/A' ] && ! nvm_is_valid_version "${provided_version}"; then
NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version && has_checked_nvmrc=1
NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version "$NVM_RC_PATH" && has_checked_nvmrc=1
provided_version="${NVM_RC_VERSION}"
unset NVM_RC_VERSION
VERSION="$(nvm_version "${provided_version}")" ||:
Expand Down Expand Up @@ -3968,16 +3995,21 @@ nvm() {
"which")
local NVM_SILENT
local provided_version
local NVM_RC_PATH
while [ $# -ne 0 ]; do
case "${1}" in
--silent) NVM_SILENT=1 ;;
--config=*)
NVM_RC_PATH="${1#--config=}"
shift
;;
--) ;;
*) provided_version="${1-}" ;;
esac
shift
done
if [ -z "${provided_version-}" ]; then
NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version
NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version "$NVM_RC_PATH"
if [ -n "${NVM_RC_VERSION}" ]; then
provided_version="${NVM_RC_VERSION}"
VERSION=$(nvm_version "${NVM_RC_VERSION}") ||:
Expand Down