droboticflow-new/scripts/local/check-dev-stack.mjs

44 lines
1.6 KiB
JavaScript

#!/usr/bin/env node
import { execFileSync } from "node:child_process";
const origin = new URL(process.argv[2] || "http://localhost:5173");
if (
!["localhost", "127.0.0.1", "[::1]"].includes(origin.hostname) ||
origin.protocol !== "http:" ||
origin.username ||
origin.password
)
throw Error("Use a loopback HTTP development URL");
const get = async (path) => {
const response = await fetch(new URL(path, origin), { redirect: "error", signal: AbortSignal.timeout(5000) });
if (!response.ok) throw Error(`${path}: HTTP ${response.status}`);
return response.json();
};
try {
const [frontend, api] = await Promise.all([get("/__dev/status"), get("/api/health")]);
const expected = execFileSync("git", ["rev-parse", "HEAD"], { encoding: "utf8" }).trim();
const issues = [];
if (frontend.commit !== expected) issues.push("Frontend is from another commit");
if (!api.buildCommit) issues.push("API has no buildCommit; restart through npm run dev:api:replica");
else if (api.buildCommit !== expected) issues.push("API is from another commit");
if (frontend.dirty)
issues.push("Frontend source has uncommitted changes; commit comparison alone cannot prove the running build");
if (api.status !== "ok") issues.push("API is not healthy");
console.log(
JSON.stringify(
{
url: origin.origin,
expectedCommit: expected,
frontend,
api: { status: api.status, commit: api.buildCommit, buildTime: api.buildTime },
issues,
},
null,
2,
),
);
if (issues.length) process.exitCode = 1;
} catch (error) {
console.error(`Local stack check failed: ${error.message}`);
process.exitCode = 1;
}