diff --git a/registry/kunstewi/.images/avatar.png b/registry/kunstewi/.images/avatar.png new file mode 100644 index 00000000..e918aea2 Binary files /dev/null and b/registry/kunstewi/.images/avatar.png differ diff --git a/registry/kunstewi/README.md b/registry/kunstewi/README.md new file mode 100644 index 00000000..88588d2b --- /dev/null +++ b/registry/kunstewi/README.md @@ -0,0 +1,14 @@ +--- +display_name: Stewi +bio: I build and break things, probably i break things more. +avatar_url: ./.images/avatar.png +github: kunstewi +linkedin: "https://www.linkedin.com/in/kunstewi" +website: "https://kunstewi.tech" +support_email: kunstewi@gmail.com +status: "community" +--- + +# Stewi + +I build and break things, probably i break things more. \ No newline at end of file diff --git a/registry/kunstewi/modules/auto-dev-server/README.md b/registry/kunstewi/modules/auto-dev-server/README.md new file mode 100644 index 00000000..d1f03018 --- /dev/null +++ b/registry/kunstewi/modules/auto-dev-server/README.md @@ -0,0 +1,41 @@ +--- +display_name: Auto npm start +description: Automatically starts a Node.js development server via `npm start`. +icon: ../../../../.icons/node.svg +maintainer_github: kunstewi +verified: false +tags: [helper, nodejs, automation, dev-server] +--- + +# Auto npm start + +This module automatically detects a Node.js project in your workspace and runs `npm start` in the background when the workspace starts. + +It looks for a `package.json` file in the specified project directory. If found, it starts the server and logs the output to `auto-npm-start.log` within that directory. + +## Basic Usage + +Add this to your Coder template. It will check for a project in `/home/coder/project`. + +```tf +module "auto_npm_start" { + count = data.coder_workspace.me.start_count + source = "registry.coder.com/thezoker/auto-npm-start/coder" + version = "1.0.0" + agent_id = coder_agent.example.id +} +``` + +## Custom Project Directory + +If your project is in a different location, you can specify the `project_dir` variable. + +```tf +module "auto_npm_start" { + count = data.coder_workspace.me.start_count + source = "registry.coder.com/thezoker/auto-npm-start/coder" + version = "1.0.0" + agent_id = coder_agent.example.id + project_dir = "/home/coder/my-awesome-app" +} +``` \ No newline at end of file diff --git a/registry/kunstewi/modules/auto-dev-server/main.tf b/registry/kunstewi/modules/auto-dev-server/main.tf new file mode 100644 index 00000000..446c8bef --- /dev/null +++ b/registry/kunstewi/modules/auto-dev-server/main.tf @@ -0,0 +1,33 @@ +terraform { + required_version = ">= 1.0" + + required_providers { + coder = { + source = "coder/coder" + version = ">= 0.12" + } + } +} + +variable "agent_id" { + type = string + description = "The ID of a Coder agent." +} + +variable "project_dir" { + type = string + description = "The directory to check for a package.json file." + default = "/home/coder/project" +} + +resource "coder_script" "auto_npm_start" { + agent_id = var.agent_id + display_name = "Auto npm start" + icon = "/icon/node.svg" + script = templatefile("${path.module}/run.sh", { + PROJECT_DIR : var.project_dir, + }) + run_on_start = true + start_blocks_login = false # Run in background, don't block login +} + diff --git a/registry/kunstewi/modules/auto-dev-server/run.sh b/registry/kunstewi/modules/auto-dev-server/run.sh new file mode 100644 index 00000000..3518f5f9 --- /dev/null +++ b/registry/kunstewi/modules/auto-dev-server/run.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +# Exit on error, treat unset variables as an error, and propagate exit status through pipes. +set -euo pipefail + +PROJECT_DIR="${PROJECT_DIR}" +PACKAGE_JSON_PATH="$PROJECT_DIR/package.json" +LOG_FILE="$PROJECT_DIR/auto-npm-start.log" +PID_FILE="$PROJECT_DIR/.auto-npm-start.pid" + +# Check if npm is installed +if ! command -v npm &> /dev/null; then + echo "npm could not be found. Skipping auto-start." + exit 0 +fi + +# Check if the project directory and package.json exist +if [ ! -f "$PACKAGE_JSON_PATH" ]; then + echo "No package.json found in $PROJECT_DIR. Skipping auto-start." + exit 0 +fi + +# Check if a server is already running from a previous start +if [ -f "$PID_FILE" ]; then + PID=$(cat "$PID_FILE") + # Check if the process with that PID is still running + if ps -p "$PID" > /dev/null; then + echo "Server is already running with PID $PID. Skipping." + exit 0 + else + echo "PID file found, but process $PID is not running. Cleaning up." + rm "$PID_FILE" + fi +fi + +echo "package.json found. Starting development server in the background..." +echo "Log file will be at: $LOG_FILE" + +# Change to the project directory, run npm start in the background, and store its PID. +(cd "$PROJECT_DIR" && nohup npm start > "$LOG_FILE" 2>&1 & echo $! > "$PID_FILE") + +echo "Server start command issued. Check log for details." +