[node] Install the latest version of nvm by default (#673)

feat(node): install the latest nvm by default
This commit is contained in:
eitsupi 2023-08-29 06:26:30 +09:00 committed by GitHub
parent 0668db5ede
commit 95a169b9ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 5 deletions

View file

@ -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.",
@ -30,7 +30,11 @@
},
"nvmVersion": {
"type": "string",
"default": "0.39.2",
"proposals": [
"latest",
"0.39"
],
"default": "latest",
"description": "Version of NVM to install."
}
},
@ -49,4 +53,4 @@
"installsAfter": [
"ghcr.io/devcontainers/features/common-utils"
]
}
}

View file

@ -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}"
@ -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
@ -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."
@ -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

14
test/node/install_nvm_0.39.sh Executable file
View file

@ -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"
check "pnpm" pnpm -v
check "nvm version" bash -c ". /usr/local/share/nvm/nvm.sh && nvm --version | grep 0.39"
# Report result
reportResults

View file

@ -44,5 +44,13 @@
"version": "16"
}
}
},
"install_nvm_0.39": {
"image": "mcr.microsoft.com/devcontainers/base",
"features": {
"node": {
"nvmVersion": "0.39"
}
}
}
}
}