feat: add Screeps console monitoring with configurable error handling and shard support (#84)
Reviewed-on: #84
This commit was merged in pull request #84.
This commit is contained in:
@@ -4,6 +4,7 @@ import fs from "fs";
|
||||
import { glob } from "glob";
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
import { monitorConsole } from "./monitor.js";
|
||||
|
||||
/**
|
||||
* Replaces specific placeholder strings within the provided content with corresponding dynamic values.
|
||||
@@ -39,7 +40,8 @@ export function replacePlaceholders(content, hostname) {
|
||||
* @returns {Promise<string[]>} A promise that resolves with an array of file paths that were processed, or rejects with an error if the process fails.
|
||||
*/
|
||||
export async function readReplaceAndWriteFiles(pattern, prefix, hostname) {
|
||||
const globPattern = prefix ? path.join(prefix, pattern) : pattern;
|
||||
let globPattern = prefix ? path.join(prefix, pattern) : pattern;
|
||||
globPattern = globPattern.replace(/\\/g, "/");
|
||||
const files = await glob(globPattern);
|
||||
|
||||
let processPromises = files.map((file) => {
|
||||
@@ -61,7 +63,8 @@ export async function readReplaceAndWriteFiles(pattern, prefix, hostname) {
|
||||
*/
|
||||
export async function readFilesIntoDict(pattern, prefix) {
|
||||
// Prepend the prefix to the glob pattern
|
||||
const globPattern = prefix ? path.join(prefix, pattern) : pattern;
|
||||
let globPattern = prefix ? path.join(prefix, pattern) : pattern;
|
||||
globPattern = globPattern.replace(/\\/g, "/");
|
||||
const files = await glob(globPattern);
|
||||
|
||||
let fileDict = {};
|
||||
@@ -108,6 +111,26 @@ export function validateAuthentication(token, username, password) {
|
||||
return null; // No errors found
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the 'ignore' | 'warn' | 'fail' enum action when the given flag is true.
|
||||
* Exported so it can be unit-tested independently.
|
||||
*
|
||||
* @param {'ignore'|'warn'|'fail'} action
|
||||
* @param {boolean} flag - Only acts when true
|
||||
* @param {string} message - Passed to core.warning / core.setFailed
|
||||
*/
|
||||
export function applyOnAction(action, flag, message) {
|
||||
if (!flag) return;
|
||||
if (action === "warn") {
|
||||
core.warning(message);
|
||||
return;
|
||||
}
|
||||
if (action === "fail") {
|
||||
core.setFailed(message);
|
||||
}
|
||||
// 'ignore' → no-op
|
||||
}
|
||||
|
||||
/**
|
||||
* Posts code to Screeps server.
|
||||
*/
|
||||
@@ -159,21 +182,55 @@ export async function postCode() {
|
||||
if (token) {
|
||||
const response = await api.code.set(branch, files_to_push);
|
||||
core.info(JSON.stringify(response, null, 2));
|
||||
console.log(`Code set successfully to ${branch}`);
|
||||
core.info(`Code set successfully to ${branch}`);
|
||||
} else {
|
||||
core.info(`Logging in as user ${username}`);
|
||||
await Promise.resolve()
|
||||
.then(() => api.auth(username, password, login_arguments))
|
||||
.then(() => api.code.set(branch, files_to_push))
|
||||
.then(() => {
|
||||
api.code.set(branch, files_to_push);
|
||||
})
|
||||
.then(() => {
|
||||
console.log(`Code set successfully to ${branch}`);
|
||||
core.info(`Code set successfully to ${branch}`);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error("Error:", err);
|
||||
core.error(`Upload error: ${err}`);
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
// Console monitoring (optional)
|
||||
const monitorTicks = parseInt(core.getInput("monitor") || "0", 10);
|
||||
if (monitorTicks > 0) {
|
||||
const result = await monitorConsole(api, {
|
||||
monitor: monitorTicks,
|
||||
logToFile: core.getBooleanInput("log_to_file"),
|
||||
onTraceback: core.getInput("on_traceback") || "fail",
|
||||
onErrorLog: core.getInput("on_error_log") || "warn",
|
||||
onWarningLog: core.getInput("on_warning_log") || "ignore",
|
||||
monitorInterval: parseInt(core.getInput("monitor_interval") || "10", 10),
|
||||
hostname,
|
||||
shard: core.getInput("shard") || undefined,
|
||||
});
|
||||
|
||||
core.setOutput("saw_traceback", String(result.sawTraceback));
|
||||
core.setOutput("saw_error_log", String(result.sawErrorLog));
|
||||
core.setOutput("saw_warning_log", String(result.sawWarningLog));
|
||||
|
||||
applyOnAction(
|
||||
core.getInput("on_traceback"),
|
||||
result.sawTraceback,
|
||||
"Screeps console: traceback detected",
|
||||
);
|
||||
applyOnAction(
|
||||
core.getInput("on_error_log"),
|
||||
result.sawErrorLog,
|
||||
"Screeps console: error log output detected",
|
||||
);
|
||||
applyOnAction(
|
||||
core.getInput("on_warning_log"),
|
||||
result.sawWarningLog,
|
||||
"Screeps console: warning log output detected",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
|
||||
Reference in New Issue
Block a user