refactor: share daemon install cli setup

This commit is contained in:
Peter Steinberger 2026-03-14 01:16:48 +00:00
parent 5197171d7a
commit 5b53481d1d
4 changed files with 54 additions and 33 deletions

View File

@ -84,8 +84,28 @@ vi.mock("../../commands/daemon-install-helpers.js", () => ({
vi.mock("./shared.js", () => ({
parsePort: parsePortMock,
createDaemonInstallActionContext: (jsonFlag: unknown) => {
const json = Boolean(jsonFlag);
return {
json,
stdout: process.stdout,
warnings: actionState.warnings,
emit: (payload: DaemonActionResponse) => {
actionState.emitted.push(payload);
},
fail: (message: string, hints?: string[]) => {
actionState.failed.push({ message, hints });
},
};
},
failIfNixDaemonInstallMode: (fail: (message: string, hints?: string[]) => void) => {
if (!resolveIsNixModeMock()) {
return false;
}
fail("Nix mode detected; service install is disabled.");
return true;
},
}));
vi.mock("../../commands/daemon-runtime.js", () => ({
DEFAULT_GATEWAY_DAEMON_RUNTIME: "node",
isGatewayDaemonRuntime: isGatewayDaemonRuntimeMock,
@ -97,16 +117,6 @@ vi.mock("../../daemon/service.js", () => ({
vi.mock("./response.js", () => ({
buildDaemonServiceSnapshot: vi.fn(),
createDaemonActionContext: vi.fn(() => ({
stdout: process.stdout,
warnings: actionState.warnings,
emit: (payload: DaemonActionResponse) => {
actionState.emitted.push(payload);
},
fail: (message: string, hints?: string[]) => {
actionState.failed.push({ message, hints });
},
})),
installDaemonServiceAndEmit: installDaemonServiceAndEmitMock,
}));

View File

@ -5,25 +5,21 @@ import {
} from "../../commands/daemon-runtime.js";
import { resolveGatewayInstallToken } from "../../commands/gateway-install-token.js";
import { readBestEffortConfig, resolveGatewayPort } from "../../config/config.js";
import { resolveIsNixMode } from "../../config/paths.js";
import { resolveGatewayService } from "../../daemon/service.js";
import { isNonFatalSystemdInstallProbeError } from "../../daemon/systemd.js";
import { defaultRuntime } from "../../runtime.js";
import { formatCliCommand } from "../command-format.js";
import { buildDaemonServiceSnapshot, installDaemonServiceAndEmit } from "./response.js";
import {
buildDaemonServiceSnapshot,
createDaemonActionContext,
installDaemonServiceAndEmit,
} from "./response.js";
import { parsePort } from "./shared.js";
createDaemonInstallActionContext,
failIfNixDaemonInstallMode,
parsePort,
} from "./shared.js";
import type { DaemonInstallOptions } from "./types.js";
export async function runDaemonInstall(opts: DaemonInstallOptions) {
const json = Boolean(opts.json);
const { stdout, warnings, emit, fail } = createDaemonActionContext({ action: "install", json });
if (resolveIsNixMode(process.env)) {
fail("Nix mode detected; service install is disabled.");
const { json, stdout, warnings, emit, fail } = createDaemonInstallActionContext(opts.json);
if (failIfNixDaemonInstallMode(fail)) {
return;
}

View File

@ -1,3 +1,4 @@
import { resolveIsNixMode } from "../../config/paths.js";
import {
resolveGatewayLaunchAgentLabel,
resolveGatewaySystemdServiceName,
@ -12,10 +13,30 @@ import { getResolvedLoggerSettings } from "../../logging.js";
import { colorize, isRich, theme } from "../../terminal/theme.js";
import { formatCliCommand } from "../command-format.js";
import { parsePort } from "../shared/parse-port.js";
import { createDaemonActionContext } from "./response.js";
export { formatRuntimeStatus };
export { parsePort };
export function createDaemonInstallActionContext(jsonFlag: unknown) {
const json = Boolean(jsonFlag);
return {
json,
...createDaemonActionContext({ action: "install", json }),
};
}
export function failIfNixDaemonInstallMode(
fail: (message: string, hints?: string[]) => void,
env: NodeJS.ProcessEnv = process.env,
): boolean {
if (!resolveIsNixMode(env)) {
return false;
}
fail("Nix mode detected; service install is disabled.");
return true;
}
export function createCliStatusTextStyles() {
const rich = isRich();
return {

View File

@ -3,7 +3,6 @@ import {
DEFAULT_NODE_DAEMON_RUNTIME,
isNodeDaemonRuntime,
} from "../../commands/node-daemon-runtime.js";
import { resolveIsNixMode } from "../../config/paths.js";
import {
resolveNodeLaunchAgentLabel,
resolveNodeSystemdServiceName,
@ -25,13 +24,11 @@ import {
runServiceStop,
runServiceUninstall,
} from "../daemon-cli/lifecycle-core.js";
import {
buildDaemonServiceSnapshot,
createDaemonActionContext,
installDaemonServiceAndEmit,
} from "../daemon-cli/response.js";
import { buildDaemonServiceSnapshot, installDaemonServiceAndEmit } from "../daemon-cli/response.js";
import {
createCliStatusTextStyles,
createDaemonInstallActionContext,
failIfNixDaemonInstallMode,
formatRuntimeStatus,
parsePort,
resolveRuntimeStatusColor,
@ -89,11 +86,8 @@ function resolveNodeDefaults(
}
export async function runNodeDaemonInstall(opts: NodeDaemonInstallOptions) {
const json = Boolean(opts.json);
const { stdout, warnings, emit, fail } = createDaemonActionContext({ action: "install", json });
if (resolveIsNixMode(process.env)) {
fail("Nix mode detected; service install is disabled.");
const { json, stdout, warnings, emit, fail } = createDaemonInstallActionContext(opts.json);
if (failIfNixDaemonInstallMode(fail)) {
return;
}