diff --git a/README.md b/README.md index d8982fc..82d599f 100644 --- a/README.md +++ b/README.md @@ -255,6 +255,9 @@ To change the location to say `$HOME/.n`, add lines like the following to your s export N_PREFIX=$HOME/.n export PATH=$N_PREFIX/bin:$PATH +If you want to store the downloads under a different location, use `N_CACHE_PREFIX`. This does *not* affect the currently active +node version. + `n` defaults to using xz compressed Node.js tarballs for the download if it is likely tar on the system supports xz decompression. You can override the automatic choice by setting an environment variable to zero or non-zero: diff --git a/bin/n b/bin/n index 125600d..99ac2f2 100755 --- a/bin/n +++ b/bin/n @@ -66,7 +66,11 @@ VERSION="v8.0.3-0" N_PREFIX="${N_PREFIX-/usr/local}" N_PREFIX=${N_PREFIX%/} readonly N_PREFIX -readonly CACHE_DIR=$N_PREFIX/n/versions + +N_CACHE_PREFIX="${N_CACHE_PREFIX-${N_PREFIX}}" +N_CACHE_PREFIX=${N_CACHE_PREFIX%/} +CACHE_DIR="${N_CACHE_PREFIX}/n/versions" +readonly N_CACHE_PREFIX CACHE_DIR N_NODE_MIRROR=${N_NODE_MIRROR:-${NODE_MIRROR:-https://nodejs.org/dist}} N_NODE_MIRROR=${N_NODE_MIRROR%/} diff --git a/test/tests/install-contents.bats b/test/tests/install-contents.bats index 620f488..5027a20 100644 --- a/test/tests/install-contents.bats +++ b/test/tests/install-contents.bats @@ -42,3 +42,21 @@ function setup() { rm -rf "${TMP_PREFIX_DIR}" } + +@test "install: cache prefix" { + readonly N_CACHE_PREFIX="$(mktemp -d)" + readonly TARGET_VERSION="4.9.1" + setup_tmp_prefix + export N_CACHE_PREFIX + + [ ! -d "${N_CACHE_PREFIX}/n/versions/node/${TARGET_VERSION}" ] + [ ! -d "${N_PREFIX}/n/versions/node/${TARGET_VERSION}" ] + + n ${TARGET_VERSION} + + # Cached version + [ -d "${N_CACHE_PREFIX}/n/versions/node/${TARGET_VERSION}" ] + [ ! -d "${N_PREFIX}/n/versions/node/${TARGET_VERSION}" ] + + rm -rf "${TMP_PREFIX_DIR}" "${N_CACHE_PREFIX}" +} diff --git a/test/tests/shared-functions.bash b/test/tests/shared-functions.bash index 0c6e1ba..a4bcaf4 100644 --- a/test/tests/shared-functions.bash +++ b/test/tests/shared-functions.bash @@ -7,6 +7,7 @@ function unset_n_env(){ unset N_PREFIX + unset N_CACHE_PREFIX unset NODE_MIRROR unset N_NODE_MIRROR unset N_NODE_DOWNLOAD_MIRROR