Files
scoreko-electron-dev/scripts/doctor.mjs
T
2026-03-02 23:57:40 +01:00

113 lines
3.4 KiB
JavaScript

#!/usr/bin/env node
import fs from "node:fs";
import net from "node:net";
import path from "node:path";
const cwd = process.cwd();
const scorekoRootPath = path.resolve(cwd, "lib", "scoreko-dev");
const checks = [];
function addCheck(ok, title, details) {
checks.push({ ok, title, details });
}
function parsePort(name, fallback) {
const raw = process.env[name] ?? fallback;
const parsed = Number.parseInt(raw, 10);
if (!Number.isFinite(parsed) || parsed < 1 || parsed > 65535) {
addCheck(false, `${name} invalid`, `It must be an integer between 1 and 65535. Received value: '${raw}'.`);
return null;
}
addCheck(true, `${name} valid`, `${parsed}`);
return parsed;
}
function parseIntInRange(name, fallback, min, max) {
const raw = process.env[name] ?? String(fallback);
const parsed = Number.parseInt(raw, 10);
if (!Number.isFinite(parsed) || parsed < min || parsed > max) {
addCheck(false, `${name} invalid`, `It must be an integer between ${min} and ${max}. Received value: '${raw}'.`);
return;
}
addCheck(true, `${name} valid`, `${parsed}`);
}
function checkNodecgInstall() {
const packageJsonPath = path.join(scorekoRootPath, "package.json");
const nodecgDependencyPath = path.join(scorekoRootPath, "node_modules", "nodecg", "package.json");
const nodecgCliPath = path.join(
scorekoRootPath,
"node_modules",
".bin",
process.platform === "win32" ? "nodecg.cmd" : "nodecg",
);
const bundleAssetPaths = ["dashboard", "graphics", "extension", "extensions"].map((folder) =>
path.join(scorekoRootPath, folder),
);
addCheck(fs.existsSync(scorekoRootPath), "Scoreko root", scorekoRootPath);
addCheck(fs.existsSync(packageJsonPath), "scoreko-dev package.json", packageJsonPath);
addCheck(fs.existsSync(nodecgDependencyPath), "NodeCG dependency", nodecgDependencyPath);
addCheck(fs.existsSync(nodecgCliPath), "NodeCG CLI", nodecgCliPath);
addCheck(
bundleAssetPaths.some((candidatePath) => fs.existsSync(candidatePath)),
"scoreko-dev bundle assets",
`Expected one of: ${bundleAssetPaths.join(", ")}`,
);
try {
fs.accessSync(scorekoRootPath, fs.constants.R_OK | fs.constants.W_OK);
addCheck(true, "lib/scoreko-dev permissions", "Read/write OK");
} catch {
addCheck(false, "lib/scoreko-dev permissions", "No read/write permissions in lib/scoreko-dev");
}
}
function checkPortAvailability(port) {
return new Promise((resolve) => {
const server = net.createServer();
server.once("error", () => {
addCheck(false, `Port ${port}`, "It is in use. Free it or change NODECG_PORT.");
resolve();
});
server.listen(port, "127.0.0.1", () => {
server.close(() => {
addCheck(true, `Port ${port}`, "Available");
resolve();
});
});
});
}
async function main() {
const port = parsePort("NODECG_PORT", "9090");
parseIntInRange("ELECTRON_LOAD_DELAY_MS", 10000, 0, 600000);
parseIntInRange("NODECG_STARTUP_TIMEOUT_MS", 30000, 1000, 600000);
parseIntInRange("NODECG_KILL_TIMEOUT_MS", 2500, 0, 120000);
checkNodecgInstall();
if (port) {
await checkPortAvailability(port);
}
for (const check of checks) {
const icon = check.ok ? "✅" : "❌";
console.log(`${icon} ${check.title}: ${check.details}`);
}
const hasFailures = checks.some((check) => !check.ok);
if (hasFailures) {
process.exitCode = 1;
return;
}
console.log("\nDoctor finished: valid configuration.");
}
main();