diff --git a/bin/nvh b/bin/nvh index b6a36e0..4b791e9 100755 --- a/bin/nvh +++ b/bin/nvh @@ -22,6 +22,20 @@ NVH_NODE_DOWNLOAD_MIRROR=${NVH_NODE_DOWNLOAD_MIRROR:-https://nodejs.org/download NVH_NODE_DOWNLOAD_MIRROR=${NVH_NODE_DOWNLOAD_MIRROR%/} readonly NVH_NODE_DOWNLOAD_MIRROR +# Using xz instead of gzip is enabled by default, if xz compatibility checks pass. +# User may set NVH_USE_XZ to 0 to disable, or set to anything else to force-enable. +# May be overridden by command line flags. + +g_xz_force_enabled="false" +if [[ "${NVH_USE_XZ}" = "0" ]]; then + NVH_USE_XZ="false" +elif [[ -n "${NVH_USE_XZ+defined}" ]]; then + NVH_USE_XZ="true" + g_xz_force_enabled="true" +else + NVH_USE_XZ="true" +fi + # NVH_PRESERVE_NPM tested with -z -n # made readonly after CLI option parsing @@ -294,6 +308,9 @@ Options: -h, --help Display help information --insecure Turn off certificate checking for https requests (may be needed from behind a proxy server) -p, --preserve Preserve npm and npx during install of node + --use-xz Download xz-compressed tarballs, if available and extractable by tar, during install of node (this is the default) + --force-use-xz Download xz-compressed tarballs. May fail if xz tarball is not available and extractable + --no-use-xz Download gzip-compressed tarballs, rather than xz-compressed ones, during install of node -V, --version Output version of nvh Aliases: @@ -515,6 +532,18 @@ function is_ok() { fi } +# +# Synopsis: can_use_xz +# + +function can_use_xz() { + # nvh uses xz on Linux and macOS at this time, if the system supports it + if ! ([[ "$(uname -s)" = "Linux" ]] && command -v xz &> /dev/null) \ + && ! ([[ "$(uname -s)" = "Darwin" ]] && [[ "$(sw_vers -productVersion | cut -d '.' -f 2)" -gt "8" ]]); then + return 1 + fi +} + # # Synopsis: display_tarball_platform # @@ -568,7 +597,11 @@ function display_compatible_file_field { function tarball_url() { local version="$1" - echo "${g_mirror_url}/${version}/node-${version}-$(display_tarball_platform).tar.gz" + + local ext="gz" + if [[ "${NVH_USE_XZ}" = "true" ]]; then local ext="xz"; fi + + echo "${g_mirror_url}/${version}/node-${version}-$(display_tarball_platform).tar.${ext}" } # @@ -658,12 +691,27 @@ function install() { fi fi + if [[ "${NVH_USE_XZ}" = true ]] && [[ "S{g_xz_force_enabled}" = "false" ]]; then + can_use_xz || NVH_USE_XZ="false" + fi + echo install_log "installing" "${g_mirror_folder_name}/${resolved_version}" + if [[ "${NVH_USE_XZ}" = "true" ]] && [[ "${g_xz_force_enabled}" = "false" ]]; then + local resolved_major_version + resolved_major_version="$(echo ${resolved_version#v} | cut -d '.' -f '1')" + if [[ "${resolved_major_version}" -lt 4 ]]; then + NVH_USE_XZ="false" + fi + fi + local url="$(tarball_url "${resolved_version}")" is_ok "${url}" || abort "download preflight failed for '$resolved_version' (${url})" + local tarflags="-zx" + if [[ "${NVH_USE_XZ}" = "true" ]]; then local tarflags="-Jx"; fi + install_log "mkdir" "${dir}" mkdir -p "${dir}" || abort "sudo required (or change ownership, or define NVH_PREFIX)" touch "${dir}/nvh.lock" @@ -671,7 +719,7 @@ function install() { cd "${dir}" || abort "Failed to cd to directory" install_log "fetch" "${url}" - do_get "${url}" | tar -zx --strip-components=1 + do_get "${url}" | tar "${tarflags}" --strip-components=1 [[ "${GET_OPTIONS_SHOW_PROGRESS}" = "true" ]] && erase_line rm -f "${dir}/nvh.lock" @@ -1152,6 +1200,9 @@ function main() { --insecure) set_insecure ;; -p|--preserve) NVH_PRESERVE_NPM="true" ;; --no-preserve) NVH_PRESERVE_NPM="" ;; + --use-xz) NVH_USE_XZ="true"; g_xz_force_enabled="false" ;; + --force-use-xz) NVH_USE_XZ="true"; g_xz_force_enabled="true" ;; + --no-use-xz) NVH_USE_XZ="false" ;; -V|--version) echo "${VERSION}"; exit ;; -*) abort "unrecognised option '$1'"; exit 1 ;; exec) unprocessed_args=("$@"); break ;;