Skip to content

Commit

Permalink
feat(node): install the latest nvm by default
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi committed Aug 25, 2023
1 parent 0cf690e commit f6ed55e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/node/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "node",
"version": "1.2.1",
"version": "1.3.0",
"name": "Node.js (via nvm), yarn and pnpm",
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/node",
"description": "Installs Node.js, nvm, yarn, pnpm, and needed dependencies.",
Expand Down Expand Up @@ -30,7 +30,11 @@
},
"nvmVersion": {
"type": "string",
"default": "0.39.2",
"proposals": [
"latest",
"0.39"
],
"default": "latest",
"description": "Version of NVM to install."
}
},
Expand All @@ -49,4 +53,4 @@
"installsAfter": [
"ghcr.io/devcontainers/features/common-utils"
]
}
}
42 changes: 41 additions & 1 deletion src/node/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Maintainer: The Dev Container spec maintainers

export NODE_VERSION="${VERSION:-"lts"}"
export NVM_VERSION="${NVMVERSION:-"0.39.2"}"
export NVM_VERSION="${NVMVERSION:-"latest"}"
export NVM_DIR="${NVMINSTALLPATH:-"/usr/local/share/nvm"}"
INSTALL_TOOLS_FOR_NODE_GYP="${NODEGYPDEPENDENCIES:-true}"

Expand Down Expand Up @@ -78,6 +78,40 @@ check_packages() {
fi
}

# Figure out correct version of a three part version number is not passed
find_version_from_git_tags() {
local variable_name=$1
local requested_version=${!variable_name}
if [ "${requested_version}" = "none" ]; then return; fi
local repository=$2
local prefix=${3:-"tags/v"}
local separator=${4:-"."}
local last_part_optional=${5:-"false"}
if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then
local escaped_separator=${separator//./\\.}
local last_part
if [ "${last_part_optional}" = "true" ]; then
last_part="(${escaped_separator}[0-9]+)?"
else
last_part="${escaped_separator}[0-9]+"
fi
local regex="${prefix}\\K[0-9]+${escaped_separator}[0-9]+${last_part}$"
local version_list="$(git ls-remote --tags ${repository} | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)"
if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ] || [ "${requested_version}" = "lts" ]; then
declare -g ${variable_name}="$(echo "${version_list}" | head -n 1)"
else
set +e
declare -g ${variable_name}="$(echo "${version_list}" | grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")"
set -e
fi
fi
if [ -z "${!variable_name}" ] || ! echo "${version_list}" | grep "^${!variable_name//./\\.}$" > /dev/null 2>&1; then
echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2
exit 1
fi
echo "${variable_name}=${!variable_name}"
}

# Ensure apt is in non-interactive to avoid prompts
export DEBIAN_FRONTEND=noninteractive

Expand All @@ -92,6 +126,10 @@ fi
# Install dependencies
check_packages apt-transport-https curl ca-certificates tar gnupg2 dirmngr

if ! type git > /dev/null 2>&1; then
check_packages git
fi

# Install yarn
if type yarn > /dev/null 2>&1; then
echo "Yarn already installed."
Expand All @@ -112,6 +150,8 @@ elif [ "${NODE_VERSION}" = "latest" ]; then
export NODE_VERSION="node"
fi

find_version_from_git_tags NVM_VERSION "https://github.com/nvm-sh/nvm"

# Install snipppet that we will run as the user
nvm_install_snippet="$(cat << EOF
set -e
Expand Down
14 changes: 14 additions & 0 deletions test/node/install_nvm_0.39.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

# Definition specific tests
check "version" bash -c "node --version | grep 16"
check "pnpm" pnpm -v
check "nvm version" bash -c "nvm --version | grep 0.39"

# Report result
reportResults
10 changes: 9 additions & 1 deletion test/node/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,13 @@
"version": "16"
}
}
},
"install_nvm_0.39": {
"image": "mcr.microsoft.com/devcontainers/base",
"features": {
"node": {
"nvmVersion": "0.39"
}
}
}
}
}

0 comments on commit f6ed55e

Please sign in to comment.