diff --git a/src/cli/daemon-cli/install.ts b/src/cli/daemon-cli/install.ts index 2ac374b957d..112b4fa2743 100644 --- a/src/cli/daemon-cli/install.ts +++ b/src/cli/daemon-cli/install.ts @@ -16,40 +16,12 @@ import { resolveGatewayService } from "../../daemon/service.js"; import { resolveGatewayAuth } from "../../gateway/auth.js"; import { defaultRuntime } from "../../runtime.js"; import { formatCliCommand } from "../command-format.js"; -import { buildDaemonServiceSnapshot, createNullWriter, emitDaemonActionJson } from "./response.js"; +import { buildDaemonServiceSnapshot, createDaemonActionContext } from "./response.js"; import { parsePort } from "./shared.js"; export async function runDaemonInstall(opts: DaemonInstallOptions) { const json = Boolean(opts.json); - const warnings: string[] = []; - const stdout = json ? createNullWriter() : process.stdout; - const emit = (payload: { - ok: boolean; - result?: string; - message?: string; - error?: string; - service?: { - label: string; - loaded: boolean; - loadedText: string; - notLoadedText: string; - }; - hints?: string[]; - warnings?: string[]; - }) => { - if (!json) { - return; - } - emitDaemonActionJson({ action: "install", ...payload }); - }; - const fail = (message: string) => { - if (json) { - emit({ ok: false, error: message, warnings: warnings.length ? warnings : undefined }); - } else { - defaultRuntime.error(message); - } - defaultRuntime.exit(1); - }; + const { stdout, warnings, emit, fail } = createDaemonActionContext({ action: "install", json }); if (resolveIsNixMode(process.env)) { fail("Nix mode detected; service install is disabled."); @@ -88,7 +60,6 @@ export async function runDaemonInstall(opts: DaemonInstallOptions) { result: "already-installed", message: `Gateway service already ${service.loadedText}.`, service: buildDaemonServiceSnapshot(service, loaded), - warnings: warnings.length ? warnings : undefined, }); if (!json) { defaultRuntime.log(`Gateway service already ${service.loadedText}.`); diff --git a/src/cli/daemon-cli/response.ts b/src/cli/daemon-cli/response.ts index cab26213d67..13dd4f2606d 100644 --- a/src/cli/daemon-cli/response.ts +++ b/src/cli/daemon-cli/response.ts @@ -40,3 +40,42 @@ export function createNullWriter(): Writable { }, }); } + +export function createDaemonActionContext(params: { action: DaemonAction; json: boolean }): { + stdout: Writable; + warnings: string[]; + emit: (payload: Omit) => void; + fail: (message: string, hints?: string[]) => void; +} { + const warnings: string[] = []; + const stdout = params.json ? createNullWriter() : process.stdout; + const emit = (payload: Omit) => { + if (!params.json) { + return; + } + emitDaemonActionJson({ + action: params.action, + ...payload, + warnings: payload.warnings ?? (warnings.length ? warnings : undefined), + }); + }; + const fail = (message: string, hints?: string[]) => { + if (params.json) { + emit({ + ok: false, + error: message, + hints, + }); + } else { + defaultRuntime.error(message); + if (hints?.length) { + for (const hint of hints) { + defaultRuntime.log(`Tip: ${hint}`); + } + } + } + defaultRuntime.exit(1); + }; + + return { stdout, warnings, emit, fail }; +} diff --git a/src/cli/node-cli/daemon.ts b/src/cli/node-cli/daemon.ts index c82a317e86b..2c2418fb246 100644 --- a/src/cli/node-cli/daemon.ts +++ b/src/cli/node-cli/daemon.ts @@ -22,11 +22,7 @@ import { runServiceStop, runServiceUninstall, } from "../daemon-cli/lifecycle-core.js"; -import { - buildDaemonServiceSnapshot, - createNullWriter, - emitDaemonActionJson, -} from "../daemon-cli/response.js"; +import { buildDaemonServiceSnapshot, createDaemonActionContext } from "../daemon-cli/response.js"; import { formatRuntimeStatus, parsePort } from "../daemon-cli/shared.js"; type NodeDaemonInstallOptions = { @@ -100,45 +96,7 @@ function resolveNodeDefaults( export async function runNodeDaemonInstall(opts: NodeDaemonInstallOptions) { const json = Boolean(opts.json); - const warnings: string[] = []; - const stdout = json ? createNullWriter() : process.stdout; - const emit = (payload: { - ok: boolean; - result?: string; - message?: string; - error?: string; - service?: { - label: string; - loaded: boolean; - loadedText: string; - notLoadedText: string; - }; - hints?: string[]; - warnings?: string[]; - }) => { - if (!json) { - return; - } - emitDaemonActionJson({ action: "install", ...payload }); - }; - const fail = (message: string, hints?: string[]) => { - if (json) { - emit({ - ok: false, - error: message, - hints, - warnings: warnings.length ? warnings : undefined, - }); - } else { - defaultRuntime.error(message); - if (hints?.length) { - for (const hint of hints) { - defaultRuntime.log(`Tip: ${hint}`); - } - } - } - defaultRuntime.exit(1); - }; + const { stdout, warnings, emit, fail } = createDaemonActionContext({ action: "install", json }); if (resolveIsNixMode(process.env)) { fail("Nix mode detected; service install is disabled.");