Add seal-home ZCode plugin packaging
This commit is contained in:
1 parent
0959cef1cc
commit
38610d3573
24 files changed
+31884
-1655
No files matched your search
@@ -0,0 +1,23 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { runCli } from "./runner.js";
|
||||
|
||||
describe("CLI runner", () => {
|
||||
test("returns version output without touching the process lifecycle", async () => {
|
||||
const result = await runCli(["version"]);
|
||||
|
||||
expect(result).toMatchObject({
|
||||
stderr: "",
|
||||
isError: false,
|
||||
exitCode: 0
|
||||
});
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({ name: "seal-home" });
|
||||
});
|
||||
|
||||
test("returns diagnostics and a non-zero exit code for invalid commands", async () => {
|
||||
const result = await runCli(["not-a-command"]);
|
||||
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain("Unknown command: not-a-command");
|
||||
});
|
||||
});
|
||||
Executable
+1582
File diff suppressed because it is too large.
Load diff
+38
-27
@@ -7,27 +7,31 @@ export type ServiceConfig = {
|
||||
entryFile: string;
|
||||
stateDir: string;
|
||||
version?: string;
|
||||
launcher?: {
|
||||
command: string;
|
||||
args?: string[];
|
||||
};
|
||||
};
|
||||
|
||||
export async function runServiceCommand(action: string | undefined, config: ServiceConfig) {
|
||||
export async function runServiceCommand(
|
||||
action: string | undefined,
|
||||
config: ServiceConfig,
|
||||
options: { quiet?: boolean } = {}
|
||||
) {
|
||||
switch (action) {
|
||||
case "run":
|
||||
await runService(config);
|
||||
return;
|
||||
return undefined;
|
||||
case "start":
|
||||
startService(config);
|
||||
return;
|
||||
return startService(config);
|
||||
case "stop":
|
||||
stopService(config);
|
||||
return;
|
||||
return stopService(config, options);
|
||||
case "restart":
|
||||
stopService(config, { quiet: true });
|
||||
startService(config);
|
||||
return;
|
||||
return startService(config);
|
||||
case "status":
|
||||
case undefined:
|
||||
console.log(JSON.stringify(serviceStatus(config), null, 2));
|
||||
return;
|
||||
return serviceStatus(config);
|
||||
default:
|
||||
throw new Error("Usage: seal-home service <start|stop|restart|status>");
|
||||
}
|
||||
@@ -67,28 +71,36 @@ async function runService(config: ServiceConfig) {
|
||||
appendLog(logFile, `stopped pid=${process.pid}`);
|
||||
};
|
||||
|
||||
process.on("SIGTERM", () => {
|
||||
cleanup();
|
||||
process.exit(0);
|
||||
await new Promise<void>((resolve) => {
|
||||
let stopped = false;
|
||||
const heartbeat = setInterval(() => {}, 60_000);
|
||||
const stop = () => {
|
||||
if (stopped) return;
|
||||
stopped = true;
|
||||
cleanup();
|
||||
clearInterval(heartbeat);
|
||||
process.off("SIGTERM", stop);
|
||||
process.off("SIGINT", stop);
|
||||
resolve();
|
||||
};
|
||||
process.once("SIGTERM", stop);
|
||||
process.once("SIGINT", stop);
|
||||
});
|
||||
process.on("SIGINT", () => {
|
||||
cleanup();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
setInterval(() => {}, 60_000);
|
||||
await new Promise(() => {});
|
||||
}
|
||||
|
||||
function startService(config: ServiceConfig) {
|
||||
ensureStateDir(config.stateDir);
|
||||
const current = serviceStatus(config);
|
||||
if (current.running) {
|
||||
console.log(JSON.stringify(current, null, 2));
|
||||
return;
|
||||
return current;
|
||||
}
|
||||
|
||||
const child = spawn(process.execPath, [config.entryFile, "service", "run"], {
|
||||
const launcher = config.launcher ?? { command: process.execPath, args: [config.entryFile] };
|
||||
const child = spawn(launcher.command, [
|
||||
...(launcher.args ?? []),
|
||||
"service",
|
||||
"run"
|
||||
], {
|
||||
cwd: config.appRoot,
|
||||
detached: true,
|
||||
stdio: ["ignore", "ignore", "ignore"],
|
||||
@@ -99,7 +111,7 @@ function startService(config: ServiceConfig) {
|
||||
const pidFile = getPidFile(config);
|
||||
writeFileSync(pidFile, `${child.pid}\n`, { mode: 0o600 });
|
||||
appendLog(getLogFile(config), `started pid=${child.pid}`);
|
||||
console.log(JSON.stringify({ started: true, ...serviceStatus(config) }, null, 2));
|
||||
return { started: true, ...serviceStatus(config) };
|
||||
}
|
||||
|
||||
function stopService(config: ServiceConfig, options: { quiet?: boolean } = {}) {
|
||||
@@ -107,14 +119,13 @@ function stopService(config: ServiceConfig, options: { quiet?: boolean } = {}) {
|
||||
const pid = readServicePid(pidFile);
|
||||
if (!pid || !isSealHomeProcess(pid, config.appRoot)) {
|
||||
rmSync(pidFile, { force: true });
|
||||
if (!options.quiet) console.log(JSON.stringify({ stopped: false, reason: "not running" }, null, 2));
|
||||
return;
|
||||
return options.quiet ? undefined : { stopped: false, reason: "not running" };
|
||||
}
|
||||
|
||||
process.kill(pid, "SIGTERM");
|
||||
rmSync(pidFile, { force: true });
|
||||
appendLog(getLogFile(config), `stop requested pid=${pid}`);
|
||||
if (!options.quiet) console.log(JSON.stringify({ stopped: true, pid }, null, 2));
|
||||
return options.quiet ? undefined : { stopped: true, pid };
|
||||
}
|
||||
|
||||
function isSealHomeProcess(pid: number, appRoot: string) {
|
||||
|
||||
Reference in new issue
Block a user