Skip to content

Commit

Permalink
Make it work on Windows for GitBash and Cygwin
Browse files Browse the repository at this point in the history
  • Loading branch information
nmarghetti committed Dec 27, 2020
1 parent 3c079f1 commit 68cca1b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 33 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,17 @@ which should output `nvm` if the installation was successful. Please note that `

If you're running a system without prepackaged binary available, which means you're going to install nodejs or io.js from its source code, you need to make sure your system has a C++ compiler. For OS X, Xcode will work, for Debian/Ubuntu based GNU/Linux, the `build-essential` and `libssl-dev` packages work.

**Note:** `nvm` does not support Windows (see [#284](https://github.com/nvm-sh/nvm/issues/284)), but may work in WSL (Windows Subsystem for Linux) depending on the version of WSL. For Windows, two alternatives exist, which are neither supported nor developed by us:
**Note:** `nvm` does not support Windows (see [#284](https://github.com/nvm-sh/nvm/issues/284)), but may work in WSL (Windows Subsystem for Linux) depending on the version of WSL. It may work also with GitBash or Cygwin. For Windows, two alternatives exist, which are neither supported nor developed by us:

- [nvm-windows](https://github.com/coreybutler/nvm-windows)
- [nodist](https://github.com/marcelklehr/nodist)

**Note:** `nvm` may work with [GitBash](https://gitforwindows.org/) if installed with script method (the git repository contains filenames with characters not supported on Windows):

```sh
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | METHOD=script bash
```

**Note:** `nvm` does not support [Fish] either (see [#303](https://github.com/nvm-sh/nvm/issues/303)). Alternatives exist, which are neither supported nor developed by us:

- [bass](https://github.com/edc/bass) allows you to use utilities written for Bash in fish shell
Expand Down
104 changes: 74 additions & 30 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ nvm_has_system_iojs() {
}

nvm_is_version_installed() {
[ -n "${1-}" ] && [ -x "$(nvm_version_path "$1" 2>/dev/null)"/bin/node ]
local NVM_OS
NVM_OS="$(nvm_get_os)"
local NODE
NODE='node'
[ "_${NVM_OS}" = "_win" ] && NODE='node.exe'
[ -n "${1-}" ] && [ -x "$(nvm_version_path "$1" 2>/dev/null)"/bin/"${NODE}" ]
}

nvm_print_npm_version() {
Expand Down Expand Up @@ -1559,7 +1564,7 @@ nvm_get_checksum() {
SHASUMS_URL="${MIRROR}/${3}/SHASUMS.txt"
fi

nvm_download -L -s "${SHASUMS_URL}" -o - | command awk "{ if (\"${4}.tar.${5}\" == \$2) print \$1}"
nvm_download -L -s "${SHASUMS_URL}" -o - | command awk "{ if (\"${4}.${5}\" == \$2) print \$1}"
}

nvm_print_versions() {
Expand Down Expand Up @@ -1761,6 +1766,7 @@ nvm_get_os() {
SunOS\ *) NVM_OS=sunos ;;
FreeBSD\ *) NVM_OS=freebsd ;;
AIX\ *) NVM_OS=aix ;;
CYGWIN* | MSYS*) NVM_OS=win ;;
esac
nvm_echo "${NVM_OS-}"
}
Expand Down Expand Up @@ -1882,14 +1888,11 @@ nvm_install_binary() {
local VERSION
VERSION="$(nvm_strip_iojs_prefix "${PREFIXED_VERSION}")"

if [ -z "$(nvm_get_os)" ]; then
return 2
fi
local NVM_OS
NVM_OS="$(nvm_get_os)"

local tar_compression_flag
tar_compression_flag='z'
if nvm_supports_xz "${VERSION}"; then
tar_compression_flag='J'
if [ -z "${NVM_OS}" ]; then
return 2
fi

local TARBALL
Expand All @@ -1914,21 +1917,52 @@ nvm_install_binary() {
if [ -f "${TARBALL}" ]; then
TMPDIR="$(dirname "${TARBALL}")/files"
fi
local tar
tar='tar'
if [ "${NVM_OS}" = 'aix' ]; then
tar='gtar'

local UNTAR_SUCCESS
UNTAR_SUCCESS=0

# For non Windows system (including WSL running on Windows)
if [ ! "${NVM_OS}" = 'win' ]; then
local tar_compression_flag
tar_compression_flag='z'
if nvm_supports_xz "${VERSION}"; then
tar_compression_flag='J'
fi

local tar
if [ "${NVM_OS}" = 'aix' ]; then
tar='gtar'
else
tar='tar'
fi

if (
[ -n "${TMPDIR-}" ] && \
command mkdir -p "${TMPDIR}" && \
command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 && \
VERSION_PATH="$(nvm_version_path "${PREFIXED_VERSION}")" && \
command mkdir -p "${VERSION_PATH}" && \
command mv "${TMPDIR}/"* "${VERSION_PATH}" && \
command rm -rf "${TMPDIR}"
); then
UNTAR_SUCCESS=1
fi
# For Windows system (GitBash, Cygwin)
else
if (
[ -n "${TMPDIR-}" ] && \
command mkdir -p "${TMPDIR}" && \
command unzip "${TARBALL}" -d "${TMPDIR}" && \
VERSION_PATH="$(nvm_version_path "${PREFIXED_VERSION}")/bin" && \
command mkdir -p "${VERSION_PATH}" && \
command mv "${TMPDIR}/"*/* "${VERSION_PATH}" && \
command rm -rf "${TMPDIR}"
); then
UNTAR_SUCCESS=1
fi
fi
if (
[ -n "${TMPDIR-}" ] && \
command mkdir -p "${TMPDIR}" && \
command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 && \
VERSION_PATH="$(nvm_version_path "${PREFIXED_VERSION}")" && \
command mkdir -p "${VERSION_PATH}" && \
command mv "${TMPDIR}/"* "${VERSION_PATH}" && \
command rm -rf "${TMPDIR}"
); then

if [ "$UNTAR_SUCCESS" -eq 1 ]; then
if [ -n "${ALIAS-}" ]; then
nvm alias "${ALIAS}" "${provided_version}"
fi
Expand Down Expand Up @@ -2028,10 +2062,15 @@ nvm_download_artifact() {
local SLUG
SLUG="$(nvm_get_download_slug "${FLAVOR}" "${KIND}" "${VERSION}")"

local NVM_OS
NVM_OS="$(nvm_get_os)"

local COMPRESSION
COMPRESSION='gz'
if nvm_supports_xz "${VERSION}"; then
COMPRESSION='xz'
COMPRESSION='tar.gz'
if [ "${NVM_OS}" = "win" ]; then
COMPRESSION='zip'
elif nvm_supports_xz "${VERSION}"; then
COMPRESSION='tar.xz'
fi

local CHECKSUM
Expand All @@ -2049,13 +2088,13 @@ nvm_download_artifact() {
)

local TARBALL
TARBALL="${tmpdir}/${SLUG}.tar.${COMPRESSION}"
TARBALL="${tmpdir}/${SLUG}.${COMPRESSION}"
local TARBALL_URL
if nvm_version_greater_than_or_equal_to "${VERSION}" 0.1.14; then
TARBALL_URL="${MIRROR}/${VERSION}/${SLUG}.tar.${COMPRESSION}"
TARBALL_URL="${MIRROR}/${VERSION}/${SLUG}.${COMPRESSION}"
else
# node <= 0.1.13 does not have a directory
TARBALL_URL="${MIRROR}/${SLUG}.tar.${COMPRESSION}"
TARBALL_URL="${MIRROR}/${SLUG}.${COMPRESSION}"
fi

if [ -r "${TARBALL}" ]; then
Expand Down Expand Up @@ -3130,8 +3169,13 @@ nvm() {
nvm_get_make_jobs
fi

NVM_NO_PROGRESS="${NVM_NO_PROGRESS:-${noprogress}}" nvm_install_source "${FLAVOR}" std "${VERSION}" "${NVM_MAKE_JOBS}" "${ADDITIONAL_PARAMETERS}"
EXIT_CODE=$?
if [ "_${NVM_OS}" = "_win" ]; then
nvm_err "Unable to install from source for Windows"
EXIT_CODE=1
else
NVM_NO_PROGRESS="${NVM_NO_PROGRESS:-${noprogress}}" nvm_install_source "${FLAVOR}" std "${VERSION}" "${NVM_MAKE_JOBS}" "${ADDITIONAL_PARAMETERS}"
EXIT_CODE=$?
fi
fi

fi
Expand Down
4 changes: 2 additions & 2 deletions test/fast/Unit tests/nvm_get_checksum
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ nvm_download() {
nvm_get_checksum_alg() {
echo 'sha-256'
}
OUTPUT="$(nvm_get_checksum node std foo bar baz)"
OUTPUT="$(nvm_get_checksum node std foo bar tar.baz)"
EXPECTED_OUTPUT="mirror-node-std/foo/SHASUMS256.txt"
[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<"

nvm_get_checksum_alg() {
echo 'sha-1'
}
OUTPUT="$(nvm_get_checksum iojs std foo bar baz)"
OUTPUT="$(nvm_get_checksum iojs std foo bar tar.baz)"
EXPECTED_OUTPUT="mirror-iojs-std/foo/SHASUMS.txt"
[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<"

Expand Down

0 comments on commit 68cca1b

Please sign in to comment.