Skip to content

Commit 15cfde9

Browse files
committed
Add a feature to install the asdf Node.js plugin
Unfortunately, the data structure supported for Dev Container feature options is severely limited which leads us to need to implement new features for every plugin supported by asdf. This would be best solved in the spec itself by allowing more complex data structures: arrays and maps, for instance but none of these have any reasonable progress so we're resorted to needing many features (and in this commit, use a comma separated list to work around it too). This installs the Node.js plugin, and parses a comma seperated list of potential versions to install. The intention is that you'd be able to seed a container image with multiple versions and so speed up the time it takes for a container to start where many versions are required. To ensure the permissions are correct later on, we run the install commands as the local user rather than root (the default user for installing features). devcontainers/spec#57 devcontainers/cli#1298 devcontainers/spec#766 devcontainers/spec#44
1 parent 4aac9ae commit 15cfde9

4 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": "asdf-plugin-nodejs",
3+
"version": "1.0.0",
4+
"name": "nodejs plugin for asdf",
5+
"documentationURL": "https://github.com/thoughtbot/devcontainers/tree/main/features/src/asdf-plugin-nodejs",
6+
"description": "Installs the nodejs plugin for the asdf version manager",
7+
"options": {
8+
"versions": {
9+
"type": "string",
10+
"default": "lts",
11+
"description": "nodejs version to install (comma seperated list)"
12+
}
13+
},
14+
"dependsOn": {
15+
"ghcr.io/thoughtbot/devcontainers/features/asdf:1": {}
16+
}
17+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
VERSIONS="${VERSIONS:-}"
6+
USERNAME="${USERNAME:-"${_REMOTE_USER:-"automatic"}"}"
7+
8+
case "$USERNAME" in
9+
auto*)
10+
USERNAME=
11+
uid_1000="$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)"
12+
possible_users="vscode node codespace $uid_1000"
13+
14+
while read -r user; do
15+
if id -u "$user" > /dev/null 2>&1; then
16+
USERNAME="$user"
17+
break
18+
fi
19+
done <<-EOT
20+
$(echo "$possible_users" | tr ' ' '\n')
21+
EOT
22+
23+
if [ "$USERNAME" = "" ]; then
24+
USERNAME=root
25+
fi
26+
;;
27+
"none")
28+
USERNAME="root"
29+
;;
30+
esac
31+
32+
run_as() {
33+
runuser -u "$USERNAME" -- "$@"
34+
}
35+
36+
if ! run_as asdf plugin list 2>&1 | grep nodejs > /dev/null; then
37+
run_as asdf plugin add nodejs https://github.com/asdf-vm/asdf-nodejs.git
38+
fi
39+
40+
run_as asdf cmd nodejs update-nodebuild
41+
42+
echo "$VERSIONS" | tr ',' '\n' | while read -r version; do
43+
if [ "$version" = "" ]; then
44+
continue
45+
fi
46+
47+
if [ "$version" = "lts" ]; then
48+
run_as asdf install nodejs "$(run_as asdf cmd nodejs resolve lts)"
49+
continue
50+
fi
51+
52+
run_as asdf install nodejs "$version"
53+
done
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
VERBOSE="${VERBOSE:-0}"
4+
LAST_COMMAND_OUTPUT=
5+
6+
# assert_command command [expected status code]
7+
#
8+
# Run a command and assert it's exit code is 0 or one given
9+
assert_command() {
10+
cmd="$1"
11+
expected_return_status="${2:-0}"
12+
13+
LAST_COMMAND_OUTPUT=
14+
15+
# Always capture stdout and stderr, and don't let a failing command
16+
# cause this function to fail
17+
if LAST_COMMAND_OUTPUT="$(printf '%s' "$cmd" | sh -a 2>&1)"; then
18+
status="$?" # ok
19+
else
20+
status="$?" # also ok
21+
fi
22+
23+
if [ ! "$VERBOSE" -eq 0 ]; then
24+
echo "Command: $cmd"
25+
echo "Command output: $LAST_COMMAND_OUTPUT"
26+
echo "Status code: $status"
27+
fi
28+
29+
if [ "$status" -ne "$expected_return_status" ]; then
30+
echo "$cmd exited with: $status"
31+
exit 1
32+
fi
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
cwd=$(readlink -f "${0%/*}")
6+
7+
. "$cwd/test-utils.sh"
8+
9+
echo "Checking the nodejs plugin is installed..."
10+
assert_command "asdf plugin list 2>&1 | grep nodejs"
11+
12+
echo "Checking current lts is installed..."
13+
nodejs_lts_version=$(asdf cmd nodejs resolve lts)
14+
assert_command "asdf list | grep $nodejs_lts_version"

0 commit comments

Comments
 (0)