2023-08-04 16:56:55 +00:00
|
|
|
import { path } from "../common/deps.ts";
|
|
|
|
import { DiskSpacePrimitives } from "../common/spaces/disk_space_primitives.ts";
|
|
|
|
import { AssetBundle } from "../plugos/asset_bundle/bundle.ts";
|
|
|
|
|
2023-08-11 18:37:13 +00:00
|
|
|
import { Application } from "../server/deps.ts";
|
2023-08-29 19:17:29 +00:00
|
|
|
import { sleep } from "$sb/lib/async.ts";
|
2023-08-26 06:31:51 +00:00
|
|
|
import { ServerSystem } from "../server/server_system.ts";
|
|
|
|
import { AssetBundlePlugSpacePrimitives } from "../common/spaces/asset_bundle_space_primitives.ts";
|
2023-08-11 18:37:13 +00:00
|
|
|
|
2023-08-04 16:56:55 +00:00
|
|
|
export async function runPlug(
|
|
|
|
spacePath: string,
|
2023-08-11 18:37:13 +00:00
|
|
|
functionName: string | undefined,
|
2023-08-04 16:56:55 +00:00
|
|
|
args: string[] = [],
|
|
|
|
builtinAssetBundle: AssetBundle,
|
|
|
|
indexFirst = false,
|
2023-08-11 18:37:13 +00:00
|
|
|
httpServerPort = 3123,
|
|
|
|
httpHostname = "127.0.0.1",
|
2023-08-04 16:56:55 +00:00
|
|
|
) {
|
|
|
|
spacePath = path.resolve(spacePath);
|
2023-08-11 18:37:13 +00:00
|
|
|
const tempFile = Deno.makeTempFileSync({ suffix: ".db" });
|
2023-08-26 06:31:51 +00:00
|
|
|
console.log("Tempt db file", tempFile);
|
2023-08-11 18:37:13 +00:00
|
|
|
const serverController = new AbortController();
|
2023-08-26 06:31:51 +00:00
|
|
|
const app = new Application();
|
|
|
|
|
|
|
|
const serverSystem = new ServerSystem(
|
|
|
|
new AssetBundlePlugSpacePrimitives(
|
|
|
|
new DiskSpacePrimitives(spacePath),
|
|
|
|
builtinAssetBundle,
|
|
|
|
),
|
|
|
|
tempFile,
|
|
|
|
app,
|
|
|
|
);
|
|
|
|
await serverSystem.init();
|
2023-08-11 18:37:13 +00:00
|
|
|
app.listen({
|
|
|
|
hostname: httpHostname,
|
|
|
|
port: httpServerPort,
|
|
|
|
signal: serverController.signal,
|
|
|
|
});
|
|
|
|
|
2023-08-04 16:56:55 +00:00
|
|
|
if (indexFirst) {
|
2023-08-28 15:12:15 +00:00
|
|
|
await serverSystem.system.loadedPlugs.get("index")!.invoke(
|
2023-08-26 06:31:51 +00:00
|
|
|
"reindexSpace",
|
|
|
|
[],
|
|
|
|
);
|
2023-08-04 16:56:55 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 18:37:13 +00:00
|
|
|
if (functionName) {
|
|
|
|
const [plugName, funcName] = functionName.split(".");
|
2023-08-04 16:56:55 +00:00
|
|
|
|
2023-08-26 06:31:51 +00:00
|
|
|
const plug = serverSystem.system.loadedPlugs.get(plugName);
|
2023-08-11 18:37:13 +00:00
|
|
|
if (!plug) {
|
|
|
|
throw new Error(`Plug ${plugName} not found`);
|
|
|
|
}
|
|
|
|
const result = await plug.invoke(funcName, args);
|
2023-08-26 06:31:51 +00:00
|
|
|
await serverSystem.close();
|
2023-08-28 15:12:15 +00:00
|
|
|
serverSystem.denoKv.close();
|
|
|
|
await Deno.remove(tempFile);
|
2023-08-11 18:37:13 +00:00
|
|
|
serverController.abort();
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
console.log("Running in server mode, use Ctrl-c to stop");
|
|
|
|
while (true) {
|
|
|
|
await sleep(1000);
|
|
|
|
}
|
2023-08-04 16:56:55 +00:00
|
|
|
}
|
|
|
|
}
|