-
Notifications
You must be signed in to change notification settings - Fork 53
/
prepare-nodejs.sh
executable file
·50 lines (41 loc) · 1.87 KB
/
prepare-nodejs.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/bash
# Install the nodejs version specified in the package.json file using asdf
# Requires: jq, asdf asdf-nodjs to be installed. A package.json file should
# be present in the current directory
#
# export the required node version as REQUIRED_NODE_VERSION
set -e
# Get the currently installed Node.js version using asdf
CURRENT_NODE_VERSION=$(asdf current nodejs 2>/dev/null | awk '{print $2}')
# Extract the required Node.js version from package.json
REQUIRED_NODE_VERSION=$(jq -r '.engines.node' package.json)
# Handle version ranges specified in package.json (e.g., "^18.19.1" or "~18.19.1")
# when minor version changes are allowed:
if [[ "$REQUIRED_NODE_VERSION" == ^* ]]; then
nodeversion=$(echo ${REQUIRED_NODE_VERSION:1} | cut -d '.' -f 1)
fi
# when patch version changes are allowed:
if [[ "$REQUIRED_NODE_VERSION" == ~* ]]; then
nodeversion=$(echo ${REQUIRED_NODE_VERSION:1} | cut -d '.' -f 1,2)
fi
# when required version has another syntax, try to extract numerical version:
# (NB < or > are not supported and version should be in format x.x.x)
if [ -z "$nodeversion" ]; then
nodeversion=$(echo $REQUIRED_NODE_VERSION | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')
fi
# find the latest version of the required version
REQUIRED_NODE_VERSION=$(asdf list all nodejs | grep -E "^$nodeversion" | tail -n1)
# Compare the versions
if [[ "$CURRENT_NODE_VERSION" == "$REQUIRED_NODE_VERSION" ]]; then
echo "Node.js is already at the required version: $CURRENT_NODE_VERSION"
else
echo "Node.js version mismatch. Current: $CURRENT_NODE_VERSION, Required: $REQUIRED_NODE_VERSION"
echo "Installing required Node.js version..."
asdf install nodejs $REQUIRED_NODE_VERSION
asdf local nodejs $REQUIRED_NODE_VERSION
asdf global nodejs $REQUIRED_NODE_VERSION
echo "Node.js updated to version: $REQUIRED_NODE_VERSION"
asdf reshim nodejs
fi
export REQUIRED_NODE_VERSION
echo $REQUIRED_NODE_VERSION