1 Commits

Author SHA1 Message Date
Philipp bf52580bf3 feat: add Screeps console monitoring with configurable error handling and shard support (#84)
Lint / pre-commit Linting (push) Successful in 1m5s
Test / Run Tests (push) Successful in 1m23s
Reviewed-on: #84
2026-05-16 19:44:39 +02:00
4 changed files with 48 additions and 48 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -197,7 +197,7 @@ export async function postCode() {
});
}
// ── Console monitoring (optional) ────────────────────────────────────────
// Console monitoring (optional)
const monitorTicks = parseInt(core.getInput("monitor") || "0", 10);
if (monitorTicks > 0) {
const result = await monitorConsole(api, {
+43 -43
View File
@@ -4,10 +4,6 @@ import fs from "fs";
import path from "path";
import os from "os";
// ────────────────────────────────────────────────────────────────────────────
// Shard / subscribe-path helpers
// ────────────────────────────────────────────────────────────────────────────
/**
* Returns true if the hostname is the official Screeps server.
* Used to decide whether to prefix the subscribe path with a shard name.
@@ -37,10 +33,6 @@ export function buildSubscribePath(hostname, shard) {
return isOfficialServer(hostname) ? "shard0/console" : "console";
}
// ────────────────────────────────────────────────────────────────────────────
// Detection helpers
// ────────────────────────────────────────────────────────────────────────────
/**
* Returns true when errorText contains JavaScript stack-frame lines.
* Screeps places runtime errors (including stack traces) in event.data.error.
@@ -76,17 +68,38 @@ export function detectWarning(logLines) {
* @returns {string}
*/
export function safeDecode(text) {
if (typeof text !== "string" || !text.includes("%")) return text;
if (typeof text !== "string") return text;
let result = text;
if (result.includes("%")) {
try {
return decodeURIComponent(text);
result = decodeURIComponent(result);
} catch (err) {
return text;
// Ignore decoding errors
}
}
// Screeps console often contains HTML entities
return result
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&quot;/g, '"')
.replace(/&amp;/g, "&");
}
// ────────────────────────────────────────────────────────────────────────────
// Output helpers
// ────────────────────────────────────────────────────────────────────────────
/**
* Outputs text to the action log, splitting by newline to ensure proper display.
*
* @param {string} text
* @param {"info"|"warning"|"error"} level
*/
export function outputMultiline(text, level = "info") {
if (!text) return;
const lines = text.split(/\r?\n/);
lines.forEach((line) => {
if (level === "error") core.error(line);
else if (level === "warning") core.warning(line);
else core.info(line);
});
}
/**
* Builds a CI-friendly progress string for core.info().
@@ -99,10 +112,6 @@ export function buildProgressMessage(elapsed, total) {
return `[Monitor] ${elapsed}/${total} ticks elapsed...`;
}
// ────────────────────────────────────────────────────────────────────────────
// File / artifact helpers
// ────────────────────────────────────────────────────────────────────────────
/**
* Writes an array of log lines to a UTF-8 text file, one line per entry.
*
@@ -142,10 +151,6 @@ export async function uploadLogArtifact(
}
}
// ────────────────────────────────────────────────────────────────────────────
// Console event handler
// ────────────────────────────────────────────────────────────────────────────
/**
* Processes a single 'console' WebSocket event from the Screeps socket.
* Mutates `state` and `stdoutBuffer` in place; never throws.
@@ -179,46 +184,42 @@ export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
const results = data?.messages?.results ?? [];
const errorText = data?.error ?? null;
// ── Warn detection (always live regardless of logToFile) ─────────────────
// Warn detection (always live regardless of logToFile)
if (detectWarning(logLines)) {
state.sawWarningLog = true;
const warnPattern = /<font\s+color=['"](?:orange|yellow)['"]/i;
logLines
.filter((l) => warnPattern.test(l))
.forEach((l) => core.warning(safeDecode(l)));
.forEach((l) => outputMultiline(safeDecode(l), "warning"));
}
// ── Traceback detection in log lines (Screeps sometimes sends errors here)
// Traceback detection in log lines (Screeps sometimes sends errors here)
if (logLines.some((l) => detectTraceback(l))) {
state.sawTraceback = true;
state.sawErrorLog = true;
}
// ── Stdout lines ──────────────────────────────────────────────────────────
// Stdout lines
const allStdout = [...logLines, ...results].map(safeDecode);
if (allStdout.length > 0) {
if (logToFile) {
stdoutBuffer.push(...allStdout);
} else {
allStdout.forEach((l) => core.info(l));
allStdout.forEach((l) => outputMultiline(l, "info"));
}
}
// ── Error field (always live) ─────────────────────────────────────────────
// Error field (always live)
if (errorText) {
state.sawErrorLog = true;
const decodedError = safeDecode(errorText);
core.error(decodedError);
outputMultiline(decodedError, "error");
if (detectTraceback(decodedError)) {
state.sawTraceback = true;
}
}
}
// ────────────────────────────────────────────────────────────────────────────
// Tick polling
// ────────────────────────────────────────────────────────────────────────────
/**
* Sleeps for the given number of milliseconds.
*
@@ -262,10 +263,6 @@ export async function pollUntilDone(
return elapsed;
}
// ────────────────────────────────────────────────────────────────────────────
// Main orchestrator
// ────────────────────────────────────────────────────────────────────────────
/**
* @typedef {Object} MonitorOptions
* @property {number} monitor - Number of game ticks to collect.
@@ -325,10 +322,10 @@ export async function monitorConsole(api, opts) {
};
let lastProgressTick = 0;
// ── Step 1: record starting tick ─────────────────────────────────────────
// Step 1: record starting tick
const { time: startTick } = await api.time(shard);
// ── Step 2: connect socket + subscribe ───────────────────────────────────
// Step 2: connect socket + subscribe
await api.socket.connect();
await api.socket.subscribe(subscribePath, (event) => {
handleConsoleEvent(event, opts, stdoutBuffer, state);
@@ -340,7 +337,7 @@ export async function monitorConsole(api, opts) {
"...",
);
// ── Step 3 & 4: tick-poll loop ───────────────────────────────────────────
// Step 3 & 4: tick-poll loop
try {
await pollUntilDone(
api,
@@ -368,13 +365,16 @@ export async function monitorConsole(api, opts) {
},
);
} finally {
// ── Step 5: always disconnect cleanly ────────────────────────────────
// Step 5: always disconnect cleanly
api.socket.disconnect();
}
// ── Step 6: artifact upload ───────────────────────────────────────────────
// Step 6: artifact upload
if (logToFile && stdoutBuffer.length > 0) {
const tmpFile = path.join(os.tmpdir(), "screeps_console_log.txt");
const tmpDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), "screeps-monitor-"),
);
const tmpFile = path.join(tmpDir, "screeps_console_log.txt");
await writeLogFile(stdoutBuffer, tmpFile);
await uploadLogArtifact(tmpFile);
} else if (logToFile) {
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "screeps-deploy-action",
"version": "0.2.0",
"description": "Deploys screeps code to the official game or an pirvate server.",
"description": "Deploys screeps code to the official game or a private server.",
"type": "module",
"main": "index.js",
"scripts": {