diff --git a/README.md b/README.md index 4e96b713..d9b29ced 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ - [io.js](#iojs) - [System Version of Node](#system-version-of-node) - [Listing Versions](#listing-versions) + - [Pruning old versions](#pruning-old-versions) - [Setting Custom Colors](#setting-custom-colors) - [Persisting custom colors](#persisting-custom-colors) - [Suppressing colorized output](#suppressing-colorized-output) @@ -611,6 +612,23 @@ If you want to see what versions are available to install: nvm ls-remote ``` +### Pruning old versions + +To clean up old, unused Node.js versions, use `nvm prune`: + +```sh +nvm prune +``` + +This will uninstall all installed versions except for the latest one within each major version group (each minor version group for `v0.x`; e.g., if you have `v16.1.0` and `v16.2.0`, it will keep `v16.2.0`), then show you what it's about to remove and ask for confirmation before deleting anything. + +- **Safety**: The currently active version, and any version an alias (including `default`) points to, are **never** removed, even if they are not the latest in their group. +- **`.nvmrc` files**: `nvm prune` can't know which versions your projects' `.nvmrc` files need, so it may remove one of them; preview with `--dry-run` first. +- **Confirmation**: `nvm prune` lists the versions it will remove and asks for confirmation before deleting. Pass `--force` to skip the prompt, e.g. for use in scripts. +- **Global Modules**: `nvm prune` will warn you if it's about to uninstall a version that contains global npm modules (other than the bundled `npm` and `corepack`). +- **Preview**: Use `nvm prune --dry-run` to see which versions would be removed, without prompting or deleting anything. +- **Minor Version Retention**: Use `nvm prune --minor` to keep the latest version of each **minor** release group instead of major. + ### Setting Custom Colors You can set five colors that will be used to display version and alias information. These colors replace the default colors. diff --git a/bash_completion b/bash_completion index 8012bffa..9dbd50ff 100644 --- a/bash_completion +++ b/bash_completion @@ -144,6 +144,7 @@ __nvm_options() { which) flags="--silent ${only}" ;; version | reinstall-packages | copy-packages) flags="${only}" ;; deactivate) flags='--silent' ;; + prune) flags='--minor --dry-run --force' ;; *) return 0 ;; esac # Only split our fixed option lists, never user-supplied words. @@ -163,7 +164,7 @@ __nvm_candidates() { *) __nvm_complete help install uninstall use run exec alias unalias \ reinstall-packages current list ls list-remote ls-remote install-latest-npm \ - cache deactivate unload version version-remote which debug set-colors + cache deactivate unload version version-remote which prune debug set-colors ;; esac return 0 diff --git a/nvm.sh b/nvm.sh index 14251bb4..178b9800 100755 --- a/nvm.sh +++ b/nvm.sh @@ -2853,6 +2853,161 @@ nvm_ls_remote() { NVM_LTS="${NVM_LTS-}" nvm_ls_remote_index_tab node std "${PATTERN}" } +nvm_prune() { + local NVM_PRUNE_ARG + local NVM_DRY_RUN + local NVM_FORCE + local NVM_KEEP_MINOR + local NVM_CANDIDATES + local NVM_PROTECTED + local NVM_ALIAS_DIR + local NVM_ALIAS_FILE + local NVM_PRUNE_VERSION + local NVM_VERSION_PATH + local NVM_GLOBALS_DIR + local NVM_GLOBAL + local NVM_GLOBALS_COUNT + local NVM_REMOVE + local NVM_REMOVE_LIST + local NVM_ANSWER + local NVM_REMOVED_COUNT + local NVM_FAILED_COUNT + + NVM_DRY_RUN=0 + NVM_FORCE=0 + NVM_KEEP_MINOR=0 + for NVM_PRUNE_ARG in "$@"; do + case "${NVM_PRUNE_ARG}" in + --dry-run) NVM_DRY_RUN=1 ;; + --force) NVM_FORCE=1 ;; + --minor) NVM_KEEP_MINOR=1 ;; + *) + nvm_err "Unknown argument: ${NVM_PRUNE_ARG}" + nvm_err 'Usage: nvm prune [--minor] [--dry-run] [--force]' + return 127 + ;; + esac + done + + nvm_echo 'Pruning old versions...' + if [ "${NVM_DRY_RUN}" = 1 ]; then + nvm_echo 'Dry run: nothing will be removed.' + fi + + # Every installed version except the newest in its group. For 0.x, each minor is its own release line. + NVM_CANDIDATES="$(nvm_ls | command awk '{ + if ($1 == "system") next + for (i = 1; i <= NF; i++) { + if ($i ~ /^(iojs-)?v[0-9]+\.[0-9]+\.[0-9]+$/) { + v = $i + sub(/^(iojs-)?v/, "", v) + split(v, p, ".") + print p[1], p[2], p[3], $i + } + } + }' | command sort -k1,1n -k2,2n -k3,3n | command awk -v minor="${NVM_KEEP_MINOR}" '{ + key = ($4 ~ /^iojs-/ ? "iojs" : "node") " " $1 + if (minor == 1 || $1 == 0) key = key "." $2 + if (key == prev) print last + prev = key + last = $4 + }')" + + NVM_PROTECTED=" $(nvm_ls_current) " + NVM_ALIAS_DIR="$(nvm_alias_path)" + while IFS= read -r NVM_ALIAS_FILE; do + if [ -n "${NVM_ALIAS_FILE}" ]; then + NVM_PROTECTED="${NVM_PROTECTED}$(nvm_resolve_local_alias "${NVM_ALIAS_FILE#"${NVM_ALIAS_DIR}/"}" 2>/dev/null) " + fi + done </dev/null) +EOF + + NVM_REMOVE='' + NVM_REMOVE_LIST='' + while IFS= read -r NVM_PRUNE_VERSION; do + case "${NVM_PRUNE_VERSION}" in '') continue ;; esac + case "${NVM_PROTECTED}" in *" ${NVM_PRUNE_VERSION} "*) continue ;; esac + nvm_is_version_installed "${NVM_PRUNE_VERSION}" || continue + NVM_VERSION_PATH="$(nvm_version_path "${NVM_PRUNE_VERSION}")" + # Another shell may have pointed the NVM_SYMLINK_CURRENT symlink here. + if [ "${NVM_DIR}/current" -ef "${NVM_VERSION_PATH}" ]; then + continue + fi + + NVM_GLOBALS_DIR="${NVM_VERSION_PATH}/lib/node_modules" + NVM_GLOBALS_COUNT=0 + while IFS= read -r NVM_GLOBAL; do + case "${NVM_GLOBAL#"${NVM_GLOBALS_DIR}/"}" in + '' | .* | npm | corepack) ;; + @*) NVM_GLOBALS_COUNT=$((NVM_GLOBALS_COUNT + $(command find "${NVM_GLOBAL}/" -mindepth 1 -maxdepth 1 ! -name '.*' 2>/dev/null | command wc -l))) ;; + *) NVM_GLOBALS_COUNT=$((NVM_GLOBALS_COUNT + 1)) ;; + esac + done </dev/null) +GLOBALS + + NVM_REMOVE="${NVM_REMOVE}${NVM_PRUNE_VERSION} +" + if [ "${NVM_GLOBALS_COUNT}" -gt 0 ]; then + NVM_REMOVE_LIST="${NVM_REMOVE_LIST} ${NVM_PRUNE_VERSION} (warning: also removes ${NVM_GLOBALS_COUNT} global module(s)) +" + else + NVM_REMOVE_LIST="${NVM_REMOVE_LIST} ${NVM_PRUNE_VERSION} +" + fi + done </dev/null; then + nvm_echo "Uninstalled ${NVM_PRUNE_VERSION}." + NVM_REMOVED_COUNT=$((NVM_REMOVED_COUNT + 1)) + else + nvm_err "Failed to uninstall ${NVM_PRUNE_VERSION}." + NVM_FAILED_COUNT=$((NVM_FAILED_COUNT + 1)) + fi + done < Uninstall using automatic alias for provided LTS line, if available.' nvm_echo ' --only= Only select from these version categories (see above)' + nvm_echo ' nvm prune Uninstall old versions, keeping the newest install per major version (per minor for 0.x), the current version, and any version an alias points to.' + nvm_echo ' Versions that only an .nvmrc file needs are not detected.' + nvm_echo ' --minor Keep the newest install per minor version instead of per major version' + nvm_echo ' --dry-run Preview what would be removed, without prompting or deleting anything' + nvm_echo ' --force Skip the confirmation prompt before deleting (for scripts)' nvm_echo ' nvm use [current | ] Modify PATH to use . Uses .nvmrc if version is omitted; otherwise errors.' nvm_echo ' The following optional arguments, if provided, must appear directly after `nvm use`:' nvm_echo ' --silent Silences stdout/stderr output' @@ -6127,6 +6287,9 @@ nvm() { fi done ;; + "prune") + nvm_prune "$@" + ;; "deactivate") local NVM_SILENT NVM_SILENT='' @@ -7202,6 +7365,7 @@ nvm() { nvm_get_artifact_compression nvm_install_binary_extract nvm_extract_tarball \ nvm_process_nvmrc nvm_process_nvmrc_content nvm_nvmrc_invalid_msg nvm_is_version_or_alias \ nvm_write_nvmrc nvm_hash_reset \ + nvm_prune \ >/dev/null 2>&1 unset NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \ NVM_CD_FLAGS NVM_BIN NVM_INC NVM_MAKE_JOBS NVM_INSTALL_LOCK \ diff --git a/test/fast/Running 'nvm prune --dry-run' should preview without deleting b/test/fast/Running 'nvm prune --dry-run' should preview without deleting new file mode 100755 index 00000000..d90b12db --- /dev/null +++ b/test/fast/Running 'nvm prune --dry-run' should preview without deleting @@ -0,0 +1,45 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +. ../../nvm.sh + +nvm_ls() { + echo " v18.20.5" + echo " v18.20.7" +} + +# Ignore any aliases in the real NVM_DIR. +nvm_alias_path() { + echo /nonexistent +} + +nvm_ls_current() { + echo "none" +} + +nvm_is_version_installed() { + return 0 +} + +UNINSTALLED_VERSIONS="" +nvm() { + if [ "$1" = "uninstall" ]; then + UNINSTALLED_VERSIONS="${UNINSTALLED_VERSIONS} $2" + return 0 + fi + echo "nvm called with unexpected args: $*" +} + +# Closed stdin: a prompt would read nothing and abort. +OUTPUT="$(nvm_prune --dry-run ${EXPECTED}<, got >${OUTPUT}<" + +echo "All tests passed" diff --git a/test/fast/Running 'nvm prune --minor' should keep the latest minor version b/test/fast/Running 'nvm prune --minor' should keep the latest minor version new file mode 100755 index 00000000..99caa2ab --- /dev/null +++ b/test/fast/Running 'nvm prune --minor' should keep the latest minor version @@ -0,0 +1,45 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +. ../../nvm.sh + +# With --minor, grouping is per major.minor instead of per major: v18.20.5 +# and v18.20.7 share a minor (keep only v18.20.7), while v18.21.0 is a +# separate minor and must be kept too. +nvm_ls() { + echo " v18.20.5" + echo " v18.20.7" + echo " v18.21.0" +} + +# Ignore any aliases in the real NVM_DIR. +nvm_alias_path() { + echo /nonexistent +} + +nvm_ls_current() { + echo "none" +} + +nvm_is_version_installed() { + return 0 +} + +UNINSTALLED_VERSIONS="" +nvm() { + if [ "$1" = "uninstall" ]; then + UNINSTALLED_VERSIONS="${UNINSTALLED_VERSIONS} $2" + return 0 + fi + echo "nvm called with unexpected args: $*" +} + +nvm_prune --minor --force + +EXPECTED=" v18.20.5" +if [ "${UNINSTALLED_VERSIONS}" != "${EXPECTED}" ]; then + die "Expected uninstalled: '${EXPECTED}', got: '${UNINSTALLED_VERSIONS}'" +fi + +echo "All tests passed" diff --git a/test/fast/Running 'nvm prune' should group 0.x versions by minor version b/test/fast/Running 'nvm prune' should group 0.x versions by minor version new file mode 100755 index 00000000..ef8b95f4 --- /dev/null +++ b/test/fast/Running 'nvm prune' should group 0.x versions by minor version @@ -0,0 +1,45 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +. ../../nvm.sh + +# For 0.x releases the minor version is effectively the major version +# (0.10.x and 0.12.x were distinct release lines), so even without --minor +# they must be grouped - and kept - separately. +nvm_ls() { + echo " v0.10.48" + echo " v0.10.49" + echo " v0.12.18" +} + +# Ignore any aliases in the real NVM_DIR. +nvm_alias_path() { + echo /nonexistent +} + +nvm_ls_current() { + echo "none" +} + +nvm_is_version_installed() { + return 0 +} + +UNINSTALLED_VERSIONS="" +nvm() { + if [ "$1" = "uninstall" ]; then + UNINSTALLED_VERSIONS="${UNINSTALLED_VERSIONS} $2" + return 0 + fi + echo "nvm called with unexpected args: $*" +} + +nvm_prune --force + +EXPECTED=" v0.10.48" +if [ "${UNINSTALLED_VERSIONS}" != "${EXPECTED}" ]; then + die "Expected uninstalled: '${EXPECTED}', got: '${UNINSTALLED_VERSIONS}'" +fi + +echo "All tests passed" diff --git a/test/fast/Running 'nvm prune' should keep current, aliased, and newest installs b/test/fast/Running 'nvm prune' should keep current, aliased, and newest installs new file mode 100755 index 00000000..cee2d55b --- /dev/null +++ b/test/fast/Running 'nvm prune' should keep current, aliased, and newest installs @@ -0,0 +1,95 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +TEMP_NVM_DIR="$(mktemp -d)" +[ -n "${TEMP_NVM_DIR}" ] && [ -d "${TEMP_NVM_DIR}" ] || die 'mktemp failed' +trap 'rm -rf "${TEMP_NVM_DIR}"' EXIT +NVM_DIR="${TEMP_NVM_DIR}" +export NVM_DIR + +: nvm.sh +\. ../../nvm.sh +\. ../common.sh + +[ "${NVM_DIR}" = "${TEMP_NVM_DIR}" ] || die "NVM_DIR changed to ${NVM_DIR}" + +for v in v0.10.48 v0.12.18 v12.0.0 v12.1.0 v12.2.0 v16.0.0 v16.1.0 v18.20.5 v18.20.7 v20.0.0 v20.1.0 v22.0.0 v22.1.0 v22.2.0; do + make_fake_node "${v}" || die "make_fake_node ${v} failed" +done +# An incomplete install, with no node binary, is not offered for removal. +mkdir -p "$(nvm_version_path v14.0.0)" +make_fake_node v14.1.0 || die 'make_fake_node v14.1.0 failed' +make_fake_iojs v3.3.0 || die 'make_fake_iojs v3.3.0 failed' +make_fake_iojs v3.3.1 || die 'make_fake_iojs v3.3.1 failed' + +# Symlinked alias files and directories count too. +mkdir -p "${TEMP_NVM_DIR}/linked/lts" "${NVM_DIR}/alias" +printf 'v18.20.5\n' > "${TEMP_NVM_DIR}/linked/default" +ln -s "${TEMP_NVM_DIR}/linked/default" "${NVM_DIR}/alias/default" +ln -s "${TEMP_NVM_DIR}/linked/lts" "${NVM_DIR}/alias/lts" +printf '12.1\n' > "${NVM_DIR}/alias/partial" +printf 'second\n' > "${NVM_DIR}/alias/first" +printf 'v16.0.0\n' > "${NVM_DIR}/alias/second" +printf 'v20.0.0\n' > "${NVM_DIR}/alias/lts/foo" +ln -s "$(nvm_version_path v22.0.0)" "${NVM_DIR}/current" + +GLOBALS="$(nvm_version_path v22.1.0)/lib/node_modules" +mkdir -p "${GLOBALS}/npm" "${GLOBALS}/corepack" "${GLOBALS}/.bin" "${GLOBALS}/foo" "${GLOBALS}/@scope/a" "${GLOBALS}/@scope/b" +mkdir -p "${TEMP_NVM_DIR}/linked-scope/c" +ln -s "${TEMP_NVM_DIR}/linked-scope" "${GLOBALS}/@linked" + +# A system node newer than every install of its major is not an install. +mkdir "${TEMP_NVM_DIR}/system-bin" +printf '#!/bin/sh\necho v22.3.0\n' > "${TEMP_NVM_DIR}/system-bin/node" +chmod +x "${TEMP_NVM_DIR}/system-bin/node" +PATH="${TEMP_NVM_DIR}/system-bin:${PATH}" +nvm_ls | grep -q '^ *system' || die "the system node stub is not listed: $(nvm_ls)" + +OUTPUT="$(nvm prune --bogus 2>&1 ${EXPECTED_DRY_RUN}<, got >${OUTPUT}<" +for v in v12.0.0 v22.1.0 iojs-v3.3.0; do + nvm_is_version_installed "${v}" || die "the dry run removed ${v}" +done + +EXPECTED_OUTPUT="Pruning old versions... +These versions will be removed: + iojs-v3.3.0 + v12.0.0 + v22.1.0 (warning: also removes 4 global module(s)) +Uninstalled iojs-v3.3.0. +Uninstalled v12.0.0. +Uninstalled v22.1.0. +Pruned 3 version(s)." +OUTPUT="$(set -u; nvm prune --force 2>&1 ${EXPECTED_OUTPUT}<, got >${OUTPUT}<" + +for v in v12.0.0 v22.1.0 iojs-v3.3.0; do + ! nvm_is_version_installed "${v}" || die "${v} should have been removed" +done +for v in v0.10.48 v0.12.18 v12.1.0 v12.2.0 v16.0.0 v16.1.0 v18.20.5 v18.20.7 v20.0.0 v20.1.0 v22.0.0 v22.2.0 iojs-v3.3.1; do + nvm_is_version_installed "${v}" || die "${v} should have been kept" +done +for a in default partial first second lts/foo; do + [ -f "${NVM_DIR}/alias/${a}" ] || die "alias ${a} should have been kept" +done + +OUTPUT="$(nvm prune --force 2>&1 ${ANSWER}< should abort, got: ${OUTPUT}" ;; + esac +done + +OUTPUT="$({ nvm_prune; echo "exit: $?"; echo "uninstalled:${UNINSTALLED_VERSIONS}"; } ${ANSWER}< should uninstall v18.20.5, got: ${OUTPUT}" ;; + esac +done + +echo "All tests passed" diff --git a/test/fast/Running 'nvm prune' should remove old versions b/test/fast/Running 'nvm prune' should remove old versions new file mode 100755 index 00000000..4f56aadc --- /dev/null +++ b/test/fast/Running 'nvm prune' should remove old versions @@ -0,0 +1,88 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +. ../../nvm.sh + +nvm_ls() { + echo " v16.20.2" + echo " v18.20.5" + echo " v18.20.7" + echo " v20.17.0" + echo "-> v22.22.0" + echo " v24.13.0" +} + +# Ignore any aliases in the real NVM_DIR. +nvm_alias_path() { + echo /nonexistent +} + +nvm_ls_current() { + echo "v22.22.0" +} + +nvm_is_version_installed() { + return 0 +} + +UNINSTALLED_VERSIONS="" +nvm() { + if [ "$1" = "uninstall" ]; then + UNINSTALLED_VERSIONS="${UNINSTALLED_VERSIONS} $2" + return 0 + fi + echo "nvm called with unexpected args: $*" +} + +nvm_prune --force + +EXPECTED=" v18.20.5" +if [ "${UNINSTALLED_VERSIONS}" != "${EXPECTED}" ]; then + die "Expected uninstalled: '${EXPECTED}', got: '${UNINSTALLED_VERSIONS}'" +fi + +# The current version is kept even when it is not the newest. +nvm_ls() { + echo " v18.0.0" + echo "-> v18.1.0" + echo " v18.2.0" +} + +nvm_ls_current() { + echo "v18.1.0" +} + +UNINSTALLED_VERSIONS="" +nvm_prune --force + +EXPECTED=" v18.0.0" +if [ "${UNINSTALLED_VERSIONS}" != "${EXPECTED}" ]; then + die "current version case: expected uninstalled: '${EXPECTED}', got: '${UNINSTALLED_VERSIONS}'" +fi + +# Versions compare numerically, not lexically. +nvm_ls() { + echo " v18.10.0" + echo " v18.9.0" + echo " v18.9.10" + echo " v18.9.2" +} + +nvm_ls_current() { + echo "none" +} + +UNINSTALLED_VERSIONS="" +nvm_prune --force + +EXPECTED=" v18.9.0 v18.9.2 v18.9.10" +if [ "${UNINSTALLED_VERSIONS}" != "${EXPECTED}" ]; then + die "numeric order case: expected uninstalled: '${EXPECTED}', got: '${UNINSTALLED_VERSIONS}'" +fi + +for VAR in NVM_PRUNE_ARG NVM_DRY_RUN NVM_FORCE NVM_KEEP_MINOR NVM_CANDIDATES NVM_PROTECTED NVM_ALIAS_DIR NVM_ALIAS_FILE NVM_PRUNE_VERSION NVM_VERSION_PATH NVM_GLOBALS_DIR NVM_GLOBAL NVM_GLOBALS_COUNT NVM_REMOVE NVM_REMOVE_LIST NVM_ANSWER NVM_REMOVED_COUNT NVM_FAILED_COUNT; do + eval "[ -z \"\${${VAR}+set}\" ]" || die "nvm_prune leaked ${VAR}" +done + +echo "All tests passed" diff --git a/test/fast/Running 'nvm prune' should report failed uninstalls and exit non-zero b/test/fast/Running 'nvm prune' should report failed uninstalls and exit non-zero new file mode 100755 index 00000000..842b009d --- /dev/null +++ b/test/fast/Running 'nvm prune' should report failed uninstalls and exit non-zero @@ -0,0 +1,54 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +. ../../nvm.sh + +nvm_ls() { + echo " v18.0.0" + echo " v18.1.0" + echo " v18.2.0" +} + +# Ignore any aliases in the real NVM_DIR. +nvm_alias_path() { + echo /nonexistent +} + +nvm_ls_current() { + echo "none" +} + +nvm_is_version_installed() { + return 0 +} + +ATTEMPTED_VERSIONS="" +nvm() { + if [ "$1" = "uninstall" ]; then + ATTEMPTED_VERSIONS="${ATTEMPTED_VERSIONS} $2" + [ "$2" != v18.0.0 ] + return + fi + echo "nvm called with unexpected args: $*" +} + +OUTPUT="$(nvm_prune --force 2>&1; echo "exit: $?"; echo "attempted:${ATTEMPTED_VERSIONS}")" + +EXPECTED="Pruning old versions... +These versions will be removed: + v18.0.0 + v18.1.0 +Failed to uninstall v18.0.0. +Uninstalled v18.1.0. +Pruned 1 version(s); 1 failed. +exit: 1 +attempted: v18.0.0 v18.1.0" +[ "${OUTPUT}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${OUTPUT}<" + +STDERR="$(nvm_prune --force 2>&1 >/dev/null)" +EXPECTED_STDERR="Failed to uninstall v18.0.0. +Pruned 1 version(s); 1 failed." +[ "${STDERR}" = "${EXPECTED_STDERR}" ] || die "expected stderr >${EXPECTED_STDERR}<, got >${STDERR}<" + +echo "All tests passed"