From 008bf47d3f79c539c36be795dfa8fff9a1ab8f6a Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Thu, 5 Oct 2017 12:46:29 -0400 Subject: [PATCH 1/3] Fall back to .node-version if it exists --- README.md | 9 +++++++++ nvm.sh | 9 +++++++-- test/slow/nvm exec/setup_dir | 4 ++++ test/slow/nvm exec/teardown_dir | 5 +++++ ...unning 'nvm run' should pick up .nvmrc version | 15 +++++++++++++++ 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9e47e0147e..246a8f86f3 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ - [Set default node version](#set-default-node-version) - [Use a mirror of node binaries](#use-a-mirror-of-node-binaries) - [.nvmrc](#nvmrc) + - [.node-version](#node-version) - [Deeper Shell Integration](#deeper-shell-integration) - [Calling `nvm use` automatically in a directory with a `.nvmrc` file](#calling-nvm-use-automatically-in-a-directory-with-a-nvmrc-file) - [bash](#bash) @@ -565,6 +566,14 @@ Now using node v5.9.1 (npm v3.7.3) The contents of a `.nvmrc` file **must** be the `` (as described by `nvm --help`) followed by a newline. No trailing spaces are allowed, and the trailing newline is required. +### .node-version + +For a little compatability with other node version managers, nvm will also sniff for `.node-version` files. They're the same as `.nmvrc`, they just share a common name. + +$ echo "5.9" > .node-version + +They'll be loaded after `.nvmrc`, and can contain the same values as `.nvmrc`. + ### Deeper Shell Integration You can use [`avn`](https://github.com/wbyoung/avn) to deeply integrate into your shell and automatically invoke `nvm` when changing directories. `avn` is **not** supported by the `nvm` maintainers. Please [report issues to the `avn` team](https://github.com/wbyoung/avn/issues/new). diff --git a/nvm.sh b/nvm.sh index 2e4378f2d2..8685a6e5cf 100644 --- a/nvm.sh +++ b/nvm.sh @@ -464,6 +464,11 @@ nvm_find_nvmrc() { dir="$(nvm_find_up '.nvmrc')" if [ -e "${dir}/.nvmrc" ]; then nvm_echo "${dir}/.nvmrc" + else + dir="$(nvm_find_up '.node-version')" + if [ -e "${dir}/.node-version" ]; then + nvm_echo "${dir}/.node-version" + fi fi } @@ -474,14 +479,14 @@ nvm_rc_version() { NVMRC_PATH="$(nvm_find_nvmrc)" if [ ! -e "${NVMRC_PATH}" ]; then if [ "${NVM_SILENT:-0}" -ne 1 ]; then - nvm_err "No .nvmrc file found" + nvm_err "No .nvmrc or .node-version file found" fi return 1 fi NVM_RC_VERSION="$(command head -n 1 "${NVMRC_PATH}" | command tr -d '\r')" || command printf '' if [ -z "${NVM_RC_VERSION}" ]; then if [ "${NVM_SILENT:-0}" -ne 1 ]; then - nvm_err "Warning: empty .nvmrc file found at \"${NVMRC_PATH}\"" + nvm_err "Warning: empty nvm file found at \"${NVMRC_PATH}\"" fi return 2 fi diff --git a/test/slow/nvm exec/setup_dir b/test/slow/nvm exec/setup_dir index 7921742733..5f728e433a 100755 --- a/test/slow/nvm exec/setup_dir +++ b/test/slow/nvm exec/setup_dir @@ -8,3 +8,7 @@ nvm install --lts if [ -f ".nvmrc" ]; then mv .nvmrc .nvmrc.bak fi + +if [ -f ".node-version" ]; then + mv .node-version .node-version.bak +fi diff --git a/test/slow/nvm exec/teardown_dir b/test/slow/nvm exec/teardown_dir index b16b41695a..d1955c40d9 100755 --- a/test/slow/nvm exec/teardown_dir +++ b/test/slow/nvm exec/teardown_dir @@ -7,7 +7,12 @@ nvm uninstall v1.0.0 nvm uninstall --lts rm .nvmrc +rm .node-version if [ -f ".nvmrc.bak" ]; then mv .nvmrc.bak .nvmrc fi + +if [ -f ".node-version.bak" ]; then + mv .node-version.bak .node-version +fi diff --git a/test/slow/nvm run/Running 'nvm run' should pick up .nvmrc version b/test/slow/nvm run/Running 'nvm run' should pick up .nvmrc version index a34968954d..37454887e4 100755 --- a/test/slow/nvm run/Running 'nvm run' should pick up .nvmrc version +++ b/test/slow/nvm run/Running 'nvm run' should pick up .nvmrc version @@ -12,6 +12,21 @@ echo "0.10.7" > .nvmrc [ "$(nvm run --version | head -1)" = "Found '$PWD/.nvmrc' with version <0.10.7>" ] || die "\`nvm run\` failed to print out the \"found in .nvmrc\" message" +echo "0.12.0" > .node-version + +[ "$(nvm run --version | tail -1)" = "v0.10.7" ] || die "\`nvm run\` failed to run with the .nvmrc version" + +[ "$(nvm run --version | head -1)" = "Found '$PWD/.nvmrc' with version <0.10.7>" ] || die "\`nvm run\` failed to print out the \"found in .nvmrc\" message" + +rm .nvmrc + +[ "$(nvm run --version | tail -1)" = "v0.12.0" ] || die "\`nvm run\` failed to run with the .node-version version" + +[ "$(nvm run --version | head -1)" = "Found '$PWD/.node-version' with version <0.12.0>" ] || die "\`nvm run\` failed to print out the \"found in .node-version\" message" + +rm .node-version + + echo "foo" > .nvmrc # running nvm run with .nvmrc should not print the version information when not installed From dba34046937bba9549934388f3eb93172a62c0fe Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Thu, 5 Oct 2017 16:24:32 -0400 Subject: [PATCH 2/3] Check for both files then up to the parent --- nvm.sh | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/nvm.sh b/nvm.sh index 8685a6e5cf..d9ab0f7e08 100644 --- a/nvm.sh +++ b/nvm.sh @@ -452,24 +452,27 @@ nvm_find_project_dir() { # Traverse up in directory tree to find containing folder nvm_find_up() { local path_ + local file path_="${PWD}" - while [ "${path_}" != "" ] && [ "${path_}" != '.' ] && [ ! -f "${path_}/${1-}" ]; do - path_=${path_%/*} + + # Iterate through the multiple files + while [ $# -ne 0 ]; do + # Look for files in turn in this path_ + while [ "${path_}" != "" ] && [ "${path_}" != '.' ]; do + # Is the file here? + if [ -f "${path_}/${1-}" ]; then + file="${path_}/${1-}" + echo "${file}" + break 2 + fi + path_=${path_%/*} + done + shift done - nvm_echo "${path_}" } nvm_find_nvmrc() { - local dir - dir="$(nvm_find_up '.nvmrc')" - if [ -e "${dir}/.nvmrc" ]; then - nvm_echo "${dir}/.nvmrc" - else - dir="$(nvm_find_up '.node-version')" - if [ -e "${dir}/.node-version" ]; then - nvm_echo "${dir}/.node-version" - fi - fi + nvm_echo "$(nvm_find_up '.nvmrc' '.node-version')" } # Obtain nvm version from rc file @@ -483,15 +486,16 @@ nvm_rc_version() { fi return 1 fi + NVMRC_BASENAME="$(command basename "$NVMRC_PATH")" NVM_RC_VERSION="$(command head -n 1 "${NVMRC_PATH}" | command tr -d '\r')" || command printf '' if [ -z "${NVM_RC_VERSION}" ]; then if [ "${NVM_SILENT:-0}" -ne 1 ]; then - nvm_err "Warning: empty nvm file found at \"${NVMRC_PATH}\"" + nvm_err "Warning: empty '${NVMRC_BASENAME}' file found at \"${NVMRC_PATH}\"" fi return 2 fi if [ "${NVM_SILENT:-0}" -ne 1 ]; then - nvm_echo "Found '${NVMRC_PATH}' with version <${NVM_RC_VERSION}>" + nvm_echo "Found '${NVMRC_BASENAME}' with version <${NVM_RC_VERSION}>" fi } From bf89c7cde0cb59ff7ff3053bb5140bccfe4ee10f Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Tue, 10 Oct 2017 17:06:04 -0400 Subject: [PATCH 3/3] Lovely text improvements from @ljharb --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 246a8f86f3..88bc69194d 100644 --- a/README.md +++ b/README.md @@ -568,11 +568,13 @@ The contents of a `.nvmrc` file **must** be the `` (as described by `nv ### .node-version -For a little compatability with other node version managers, nvm will also sniff for `.node-version` files. They're the same as `.nmvrc`, they just share a common name. +For a little compatability with other node version managers, nvm will also sniff for `.node-version` files, defaulting to `.nvmrc` if both are found in the same folder. +``` $ echo "5.9" > .node-version +``` -They'll be loaded after `.nvmrc`, and can contain the same values as `.nvmrc`. +Unlike `.nvmrc`, `.node-version` cannot contain a "versionish" (an alias, like `node`, `iojs`, or a custom alias you’ve defined). `.node-version` can only have versions in the format of v1, v1.2, or v1.2.3 (the `v` is optional). ### Deeper Shell Integration