Skip to content

Commit

Permalink
Make cache directory configurable (#717)
Browse files Browse the repository at this point in the history
Read the cache directory from the environment, falling back to a location under `N_PREFIX`
  • Loading branch information
UiP9AV6Y committed Mar 18, 2022
1 parent 8060fba commit e1b5795
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -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:

Expand Down
6 changes: 5 additions & 1 deletion bin/n
Expand Up @@ -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%/}
Expand Down
18 changes: 18 additions & 0 deletions test/tests/install-contents.bats
Expand Up @@ -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}"
}
1 change: 1 addition & 0 deletions test/tests/shared-functions.bash
Expand Up @@ -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
Expand Down

0 comments on commit e1b5795

Please sign in to comment.