Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit f335a62

Browse files
TheZokermafredri
andauthored
Add new nodejs module (#165)
Co-authored-by: Mathias Fredriksson <mafredri@gmail.com>
1 parent 8ed13be commit f335a62

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed

.icons/node.svg

Lines changed: 1 addition & 0 deletions
Loading

nodejs/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
display_name: nodejs
3+
description: Install Node.js via nvm
4+
icon: ../.icons/node.svg
5+
maintainer_github: TheZoker
6+
verified: false
7+
tags: [helper]
8+
---
9+
10+
# nodejs
11+
12+
Automatically installs [Node.js](https://github.com/nodejs/node) via [nvm](https://github.com/nvm-sh/nvm). It can also install multiple versions of node and set a default version. If no options are specified, the latest version is installed.
13+
14+
```tf
15+
module "nodejs" {
16+
source = "registry.coder.com/modules/nodejs/coder"
17+
version = "1.0.2"
18+
agent_id = coder_agent.example.id
19+
}
20+
```
21+
22+
### Install multiple versions
23+
24+
This installs multiple versions of Node.js:
25+
26+
```tf
27+
module "nodejs" {
28+
source = "registry.coder.com/modules/nodejs/coder"
29+
version = "1.0.2"
30+
agent_id = coder_agent.example.id
31+
node_versions = [
32+
"18",
33+
"20",
34+
"node"
35+
]
36+
default_node_version = "20"
37+
}
38+
```
39+
40+
### Full example
41+
42+
A example with all available options:
43+
44+
```tf
45+
module "nodejs" {
46+
source = "registry.coder.com/modules/nodejs/coder"
47+
version = "1.0.2"
48+
agent_id = coder_agent.example.id
49+
nvm_version = "v0.39.7"
50+
nvm_install_prefix = "/opt/nvm"
51+
node_versions = [
52+
"16",
53+
"18",
54+
"node"
55+
]
56+
default_node_version = "16"
57+
}
58+
```

nodejs/main.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { describe, expect, it } from "bun:test";
2+
import { runTerraformInit, testRequiredVariables } from "../test";
3+
4+
describe("nodejs", async () => {
5+
await runTerraformInit(import.meta.dir);
6+
7+
testRequiredVariables(import.meta.dir, {
8+
agent_id: "foo",
9+
});
10+
11+
// More tests depend on shebang refactors
12+
});

nodejs/main.tf

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
4+
required_providers {
5+
coder = {
6+
source = "coder/coder"
7+
version = ">= 0.12"
8+
}
9+
}
10+
}
11+
12+
variable "agent_id" {
13+
type = string
14+
description = "The ID of a Coder agent."
15+
}
16+
17+
variable "nvm_version" {
18+
type = string
19+
description = "The version of nvm to install."
20+
default = "master"
21+
}
22+
23+
variable "nvm_install_prefix" {
24+
type = string
25+
description = "The prefix to install nvm to."
26+
default = "$HOME/.nvm"
27+
}
28+
29+
variable "node_versions" {
30+
type = list(string)
31+
description = "A list of Node.js versions to install."
32+
default = ["node"]
33+
}
34+
35+
variable "default_node_version" {
36+
type = string
37+
description = "The default Node.js version"
38+
default = "node"
39+
}
40+
41+
resource "coder_script" "nodejs" {
42+
agent_id = var.agent_id
43+
display_name = "Node.js:"
44+
script = templatefile("${path.module}/run.sh", {
45+
NVM_VERSION : var.nvm_version,
46+
INSTALL_PREFIX : var.nvm_install_prefix,
47+
NODE_VERSIONS : join(",", var.node_versions),
48+
DEFAULT : var.default_node_version,
49+
})
50+
run_on_start = true
51+
start_blocks_login = true
52+
}

nodejs/run.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bash
2+
3+
NVM_VERSION='${NVM_VERSION}'
4+
NODE_VERSIONS='${NODE_VERSIONS}'
5+
INSTALL_PREFIX='${INSTALL_PREFIX}'
6+
DEFAULT='${DEFAULT}'
7+
BOLD='\033[0;1m'
8+
CODE='\033[36;40;1m'
9+
RESET='\033[0m'
10+
11+
printf "$${BOLD}Installing nvm!$${RESET}\n"
12+
13+
export NVM_DIR="$${INSTALL_PREFIX}/nvm"
14+
15+
script="$(curl -sS -o- "https://raw.githubusercontent.com/nvm-sh/nvm/$${NVM_VERSION}/install.sh" 2>&1)"
16+
if [ $? -ne 0 ]; then
17+
echo "Failed to download nvm installation script: $script"
18+
exit 1
19+
fi
20+
21+
output="$(bash <<< "$script" 2>&1)"
22+
if [ $? -ne 0 ]; then
23+
echo "Failed to install nvm: $output"
24+
exit 1
25+
fi
26+
27+
printf "🥳 nvm has been installed\n\n"
28+
29+
# Set up nvm for the rest of the script.
30+
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
31+
32+
# Install each node version...
33+
IFS=',' read -r -a VERSIONLIST <<< "$${NODE_VERSIONS}"
34+
for version in "$${VERSIONLIST[@]}"; do
35+
if [ -z "$version" ]; then
36+
continue
37+
fi
38+
printf "🛠️ Installing node version $${CODE}$version$${RESET}...\n"
39+
output=$(nvm install "$version" 2>&1)
40+
if [ $? -ne 0 ]; then
41+
echo "Failed to install version: $version: $output"
42+
exit 1
43+
fi
44+
done
45+
46+
# Set default if provided
47+
if [ -n "$${DEFAULT}" ]; then
48+
printf "🛠️ Setting default node version $${CODE}$DEFAULT$${RESET}...\n"
49+
output=$(nvm alias default $DEFAULT 2>&1)
50+
fi

0 commit comments

Comments
 (0)