This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { existsSync, mkdirSync, readdirSync, writeFileSync } from "node:fs";
|
||||
import { basename, join, relative } from "node:path";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const workspaceRoot =
|
||||
export const workspaceRoot =
|
||||
process.env.WORKSPACE_FATHER_PATH || "/Users/Tabitha/.openclaw/workspace_Father";
|
||||
const outputPath = join(process.cwd(), "public", "local-context.json");
|
||||
const giteaBaseUrl = "http://gitea.orson.tealthrone";
|
||||
@@ -149,7 +150,7 @@ function repoForWorktree(path) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function buildContext() {
|
||||
export function buildContext() {
|
||||
const topLevelWorkspaces = readdirSync(workspaceRoot, { withFileTypes: true })
|
||||
.filter((entry) => entry.isDirectory())
|
||||
.map((entry) => ({
|
||||
@@ -208,16 +209,22 @@ function buildContext() {
|
||||
};
|
||||
}
|
||||
|
||||
mkdirSync(join(process.cwd(), "public"), { recursive: true });
|
||||
export function writeLocalContext() {
|
||||
mkdirSync(join(process.cwd(), "public"), { recursive: true });
|
||||
|
||||
if (!existsSync(workspaceRoot)) {
|
||||
if (existsSync(outputPath)) {
|
||||
console.log(`Workspace root missing; preserving ${outputPath}`);
|
||||
process.exit(0);
|
||||
if (!existsSync(workspaceRoot)) {
|
||||
if (existsSync(outputPath)) {
|
||||
console.log(`Workspace root missing; preserving ${outputPath}`);
|
||||
return;
|
||||
}
|
||||
throw new Error(`Workspace root does not exist: ${workspaceRoot}`);
|
||||
}
|
||||
throw new Error(`Workspace root does not exist: ${workspaceRoot}`);
|
||||
|
||||
const context = buildContext();
|
||||
writeFileSync(outputPath, `${JSON.stringify(context, null, 2)}\n`);
|
||||
console.log(`Wrote ${outputPath} with ${context.workspaces.length} workspaces`);
|
||||
}
|
||||
|
||||
const context = buildContext();
|
||||
writeFileSync(outputPath, `${JSON.stringify(context, null, 2)}\n`);
|
||||
console.log(`Wrote ${outputPath} with ${context.workspaces.length} workspaces`);
|
||||
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
||||
writeLocalContext();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { createServer } from "node:http";
|
||||
import { buildContext, workspaceRoot } from "./generate-local-context.mjs";
|
||||
|
||||
const host = process.env.LOCAL_CONTEXT_HOST || "127.0.0.1";
|
||||
const port = Number(process.env.LOCAL_CONTEXT_PORT || 23873);
|
||||
|
||||
function sendJson(response, status, payload) {
|
||||
response.writeHead(status, {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "GET, OPTIONS",
|
||||
"Access-Control-Allow-Headers": "Content-Type",
|
||||
"Cache-Control": "no-store",
|
||||
"Content-Type": "application/json; charset=utf-8"
|
||||
});
|
||||
response.end(`${JSON.stringify(payload, null, 2)}\n`);
|
||||
}
|
||||
|
||||
const server = createServer((request, response) => {
|
||||
if (request.method === "OPTIONS") {
|
||||
response.writeHead(204, {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "GET, OPTIONS",
|
||||
"Access-Control-Allow-Headers": "Content-Type"
|
||||
});
|
||||
response.end();
|
||||
return;
|
||||
}
|
||||
|
||||
if (request.method !== "GET") {
|
||||
sendJson(response, 405, { error: "Method not allowed" });
|
||||
return;
|
||||
}
|
||||
|
||||
const url = new URL(request.url || "/", `http://${host}:${port}`);
|
||||
|
||||
if (url.pathname === "/health") {
|
||||
sendJson(response, 200, { ok: true, workspaceRoot });
|
||||
return;
|
||||
}
|
||||
|
||||
if (url.pathname !== "/local-context.json") {
|
||||
sendJson(response, 404, { error: "Not found" });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
sendJson(response, 200, buildContext());
|
||||
} catch (error) {
|
||||
sendJson(response, 500, {
|
||||
error: error instanceof Error ? error.message : "Could not build local context"
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
server.listen(port, host, () => {
|
||||
console.log(`Serving live local context for ${workspaceRoot} at http://${host}:${port}/local-context.json`);
|
||||
});
|
||||
Reference in New Issue
Block a user