[Refactor] add `nvm_extract_tarball` to consolidate extraction logic
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Bob Bregant II · committed by Jordan Harband · parent 9600617 · Browse files · 2 files·+49 added−27 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+49 added−27 removed
- tests:+12 added−0 removed
- other:+37 added−27 removed
Provenance
- Commit
ccd442d- Tree
b790776- Author
Bob Bregant II <bob@opshelm.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
9600617
2 files changed+49 added−27 removed
added:test/fast/Unit tests/nvm_extract_tarball+12 added
1+#!/bin/sh2+3+die () { echo "$@" ; exit 1; }4+5+\. ../../../nvm.sh6+7+[ "$(nvm_extract_tarball 2>&1)" = "nvm_extract_tarball requires exactly 4 arguments" ] || die 'incorrect error message with no args'8+[ "$(nvm_extract_tarball > /dev/null 2>&1 ; echo $?)" = "5" ] || die 'incorrect error code with no args'9+[ "$(nvm_extract_tarball one two three 2>&1)" = "nvm_extract_tarball requires exactly 4 arguments" ] || die 'incorrect error message with three args'10+[ "$(nvm_extract_tarball one two three > /dev/null 2>&1 ; echo $?)" = "5" ] || die 'incorrect error code with three args'11+[ "$(nvm_extract_tarball one two three four five 2>&1)" = "nvm_extract_tarball requires exactly 4 arguments" ] || die 'incorrect error message with five args'12+[ "$(nvm_extract_tarball one two three four five > /dev/null 2>&1 ; echo $?)" = "5" ] || die 'incorrect error code with five args'modified:nvm.sh+37 added-27 removed
1969 unmodified lines1970 VERSION_PATH="${VERSION_PATH}/bin"1971 command unzip -q "${TARBALL}" -d "${TMPDIR}" || return 11972 # For non Windows system (including WSL running on Windows)1973 else1974- local tar_compression_flag1975- tar_compression_flag='z'1976- if nvm_supports_xz "${VERSION}"; then1977- tar_compression_flag='J'1978- fi1979-1980- local tar1981- if [ "${NVM_OS}" = 'aix' ]; then1982- tar='gtar'1983- else1984- tar='tar'1985- fi1986- command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 || return 11974+ nvm_extract_tarball "${NVM_OS}" "${VERSION}" "${TARBALL}" "${TMPDIR}"1975 fi1976 1977 command mkdir -p "${VERSION_PATH}" || return 11978 259 unmodified lines2238 2239 nvm_echo "${TARBALL}"2240 }2241 2242+# args: nvm_os, version, tarball, tmpdir2243+nvm_extract_tarball() {2244+ if [ "$#" -ne 4 ]; then2245+ nvm_err 'nvm_extract_tarball requires exactly 4 arguments'2246+ return 52247+ fi2248+2249+ local NVM_OS2250+ NVM_OS="${1-}"2251+2252+ local VERSION2253+ VERSION="${2-}"2254+2255+ local TARBALL2256+ TARBALL="${3-}"2257+2258+ local TMPDIR2259+ TMPDIR="${4-}"2260+2261+ local tar_compression_flag2262+ tar_compression_flag='z'2263+ if nvm_supports_xz "${VERSION}"; then2264+ tar_compression_flag='J'2265+ fi2266+2267+ local tar2268+ tar='tar'2269+ if [ "${NVM_OS}" = 'aix' ]; then2270+ tar='gtar'2271+ fi2272+2273+ command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 || return 12274+}2275+2276 nvm_get_make_jobs() {2277 if nvm_is_natural_num "${1-}"; then2278 NVM_MAKE_JOBS="$1"2279 nvm_echo "number of \`make\` jobs: ${NVM_MAKE_JOBS}"103 unmodified lines2383 MAKE_CXX="CC=${CC:-cc} CXX=${CXX:-c++}"2384 fi2385 fi2386 2365- local tar_compression_flag2366- tar_compression_flag='z'2367- if nvm_supports_xz "${VERSION}"; then2368- tar_compression_flag='J'2369- fi2370-2371- local tar2372- tar='tar'2373- if [ "${NVM_OS}" = 'aix' ]; then2374- tar='gtar'2375- fi2376-2387 local TARBALL2388 local TMPDIR2389 local VERSION_PATH2390 11 unmodified lines2402 TMPDIR="$(dirname "${TARBALL}")/files" && \2403 if ! (2404 # shellcheck disable=SC20862405 command mkdir -p "${TMPDIR}" && \2396- command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 && \2406+ nvm_extract_tarball "${NVM_OS}" "${VERSION}" "${TARBALL}" "${TMPDIR}" && \2407 VERSION_PATH="$(nvm_version_path "${PREFIXED_VERSION}")" && \2408 nvm_cd "${TMPDIR}" && \2409 nvm_echo '$>'./configure --prefix="${VERSION_PATH}" $ADDITIONAL_PARAMETERS'<' && \2410 ./configure --prefix="${VERSION_PATH}" $ADDITIONAL_PARAMETERS && \1749 unmodified lines4160 nvm_curl_libz_support nvm_command_info nvm_is_zsh nvm_stdout_is_terminal \4161 nvm_npmrc_bad_news_bears \4162 nvm_get_colors nvm_set_colors nvm_print_color_code nvm_format_help_message_colors \4163 nvm_echo_with_colors nvm_err_with_colors \4154- nvm_get_artifact_compression nvm_install_binary_extract \4164+ nvm_get_artifact_compression nvm_install_binary_extract nvm_extract_tarball \4165 >/dev/null 2>&14166 unset NVM_RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \4167 NVM_CD_FLAGS NVM_BIN NVM_INC NVM_MAKE_JOBS \4168 NVM_COLORS INSTALLED_COLOR SYSTEM_COLOR \165 unmodified lines