Skip to content

feat: added auto-dev-server #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added registry/kunstewi/.images/avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions registry/kunstewi/README.md
Original file line number Diff line number Diff line change
@@ -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: [email protected]
status: "community"
---

# Stewi

I build and break things, probably i break things more.
41 changes: 41 additions & 0 deletions registry/kunstewi/modules/auto-dev-server/README.md
Original file line number Diff line number Diff line change
@@ -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"
}
```
33 changes: 33 additions & 0 deletions registry/kunstewi/modules/auto-dev-server/main.tf
Original file line number Diff line number Diff line change
@@ -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
}

43 changes: 43 additions & 0 deletions registry/kunstewi/modules/auto-dev-server/run.sh
Original file line number Diff line number Diff line change
@@ -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."