refactor(cli): dedupe daemon install finalize

This commit is contained in:
Peter Steinberger 2026-02-15 16:49:38 +00:00
parent 08f16da8d7
commit bf61d94083
3 changed files with 70 additions and 51 deletions

View File

@ -16,7 +16,11 @@ import { resolveGatewayService } from "../../daemon/service.js";
import { resolveGatewayAuth } from "../../gateway/auth.js"; import { resolveGatewayAuth } from "../../gateway/auth.js";
import { defaultRuntime } from "../../runtime.js"; import { defaultRuntime } from "../../runtime.js";
import { formatCliCommand } from "../command-format.js"; import { formatCliCommand } from "../command-format.js";
import { buildDaemonServiceSnapshot, createDaemonActionContext } from "./response.js"; import {
buildDaemonServiceSnapshot,
createDaemonActionContext,
installDaemonServiceAndEmit,
} from "./response.js";
import { parsePort } from "./shared.js"; import { parsePort } from "./shared.js";
export async function runDaemonInstall(opts: DaemonInstallOptions) { export async function runDaemonInstall(opts: DaemonInstallOptions) {
@ -154,29 +158,20 @@ export async function runDaemonInstall(opts: DaemonInstallOptions) {
config: cfg, config: cfg,
}); });
try { await installDaemonServiceAndEmit({
await service.install({ serviceNoun: "Gateway",
env: process.env, service,
stdout, warnings,
programArguments, emit,
workingDirectory, fail,
environment, install: async () => {
}); await service.install({
} catch (err) { env: process.env,
fail(`Gateway install failed: ${String(err)}`); stdout,
return; programArguments,
} workingDirectory,
environment,
let installed = true; });
try { },
installed = await service.isLoaded({ env: process.env });
} catch {
installed = true;
}
emit({
ok: true,
result: "installed",
service: buildDaemonServiceSnapshot(service, installed),
warnings: warnings.length ? warnings : undefined,
}); });
} }

View File

@ -79,3 +79,32 @@ export function createDaemonActionContext(params: { action: DaemonAction; json:
return { stdout, warnings, emit, fail }; return { stdout, warnings, emit, fail };
} }
export async function installDaemonServiceAndEmit(params: {
serviceNoun: string;
service: GatewayService;
warnings: string[];
emit: (payload: Omit<DaemonActionResponse, "action">) => void;
fail: (message: string, hints?: string[]) => void;
install: () => Promise<void>;
}) {
try {
await params.install();
} catch (err) {
params.fail(`${params.serviceNoun} install failed: ${String(err)}`);
return;
}
let installed = true;
try {
installed = await params.service.isLoaded({ env: process.env });
} catch {
installed = true;
}
params.emit({
ok: true,
result: "installed",
service: buildDaemonServiceSnapshot(params.service, installed),
warnings: params.warnings.length ? params.warnings : undefined,
});
}

View File

@ -22,7 +22,11 @@ import {
runServiceStop, runServiceStop,
runServiceUninstall, runServiceUninstall,
} from "../daemon-cli/lifecycle-core.js"; } from "../daemon-cli/lifecycle-core.js";
import { buildDaemonServiceSnapshot, createDaemonActionContext } from "../daemon-cli/response.js"; import {
buildDaemonServiceSnapshot,
createDaemonActionContext,
installDaemonServiceAndEmit,
} from "../daemon-cli/response.js";
import { formatRuntimeStatus, parsePort } from "../daemon-cli/shared.js"; import { formatRuntimeStatus, parsePort } from "../daemon-cli/shared.js";
type NodeDaemonInstallOptions = { type NodeDaemonInstallOptions = {
@ -160,31 +164,22 @@ export async function runNodeDaemonInstall(opts: NodeDaemonInstallOptions) {
}, },
}); });
try { await installDaemonServiceAndEmit({
await service.install({ serviceNoun: "Node",
env: process.env, service,
stdout, warnings,
programArguments, emit,
workingDirectory, fail,
environment, install: async () => {
description, await service.install({
}); env: process.env,
} catch (err) { stdout,
fail(`Node install failed: ${String(err)}`); programArguments,
return; workingDirectory,
} environment,
description,
let installed = true; });
try { },
installed = await service.isLoaded({ env: process.env });
} catch {
installed = true;
}
emit({
ok: true,
result: "installed",
service: buildDaemonServiceSnapshot(service, installed),
warnings: warnings.length ? warnings : undefined,
}); });
} }