Skip to content

Commit 95a169b

Browse files
authored
[node] Install the latest version of nvm by default (#673)
feat(node): install the latest nvm by default
1 parent 0668db5 commit 95a169b

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

src/node/devcontainer-feature.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "node",
3-
"version": "1.2.1",
3+
"version": "1.3.0",
44
"name": "Node.js (via nvm), yarn and pnpm",
55
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/node",
66
"description": "Installs Node.js, nvm, yarn, pnpm, and needed dependencies.",
@@ -30,7 +30,11 @@
3030
},
3131
"nvmVersion": {
3232
"type": "string",
33-
"default": "0.39.2",
33+
"proposals": [
34+
"latest",
35+
"0.39"
36+
],
37+
"default": "latest",
3438
"description": "Version of NVM to install."
3539
}
3640
},
@@ -49,4 +53,4 @@
4953
"installsAfter": [
5054
"ghcr.io/devcontainers/features/common-utils"
5155
]
52-
}
56+
}

src/node/install.sh

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# Maintainer: The Dev Container spec maintainers
99

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

@@ -78,6 +78,40 @@ check_packages() {
7878
fi
7979
}
8080

81+
# Figure out correct version of a three part version number is not passed
82+
find_version_from_git_tags() {
83+
local variable_name=$1
84+
local requested_version=${!variable_name}
85+
if [ "${requested_version}" = "none" ]; then return; fi
86+
local repository=$2
87+
local prefix=${3:-"tags/v"}
88+
local separator=${4:-"."}
89+
local last_part_optional=${5:-"false"}
90+
if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then
91+
local escaped_separator=${separator//./\\.}
92+
local last_part
93+
if [ "${last_part_optional}" = "true" ]; then
94+
last_part="(${escaped_separator}[0-9]+)?"
95+
else
96+
last_part="${escaped_separator}[0-9]+"
97+
fi
98+
local regex="${prefix}\\K[0-9]+${escaped_separator}[0-9]+${last_part}$"
99+
local version_list="$(git ls-remote --tags ${repository} | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)"
100+
if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ] || [ "${requested_version}" = "lts" ]; then
101+
declare -g ${variable_name}="$(echo "${version_list}" | head -n 1)"
102+
else
103+
set +e
104+
declare -g ${variable_name}="$(echo "${version_list}" | grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")"
105+
set -e
106+
fi
107+
fi
108+
if [ -z "${!variable_name}" ] || ! echo "${version_list}" | grep "^${!variable_name//./\\.}$" > /dev/null 2>&1; then
109+
echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2
110+
exit 1
111+
fi
112+
echo "${variable_name}=${!variable_name}"
113+
}
114+
81115
# Ensure apt is in non-interactive to avoid prompts
82116
export DEBIAN_FRONTEND=noninteractive
83117

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

129+
if ! type git > /dev/null 2>&1; then
130+
check_packages git
131+
fi
132+
95133
# Install yarn
96134
if type yarn > /dev/null 2>&1; then
97135
echo "Yarn already installed."
@@ -112,6 +150,8 @@ elif [ "${NODE_VERSION}" = "latest" ]; then
112150
export NODE_VERSION="node"
113151
fi
114152

153+
find_version_from_git_tags NVM_VERSION "https://github.com/nvm-sh/nvm"
154+
115155
# Install snipppet that we will run as the user
116156
nvm_install_snippet="$(cat << EOF
117157
set -e

test/node/install_nvm_0.39.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Definition specific tests
9+
check "version" bash -c "node --version"
10+
check "pnpm" pnpm -v
11+
check "nvm version" bash -c ". /usr/local/share/nvm/nvm.sh && nvm --version | grep 0.39"
12+
13+
# Report result
14+
reportResults

test/node/scenarios.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,13 @@
4444
"version": "16"
4545
}
4646
}
47+
},
48+
"install_nvm_0.39": {
49+
"image": "mcr.microsoft.com/devcontainers/base",
50+
"features": {
51+
"node": {
52+
"nvmVersion": "0.39"
53+
}
54+
}
4755
}
48-
}
56+
}

0 commit comments

Comments
 (0)