mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 19:54:32 +00:00
refactor: share daemon install plan runtime scaffolding
This commit is contained in:
@@ -3,26 +3,22 @@ import { collectConfigServiceEnvVars } from "../config/env-vars.js";
|
|||||||
import type { OpenClawConfig } from "../config/types.js";
|
import type { OpenClawConfig } from "../config/types.js";
|
||||||
import { resolveGatewayLaunchAgentLabel } from "../daemon/constants.js";
|
import { resolveGatewayLaunchAgentLabel } from "../daemon/constants.js";
|
||||||
import { resolveGatewayProgramArguments } from "../daemon/program-args.js";
|
import { resolveGatewayProgramArguments } from "../daemon/program-args.js";
|
||||||
import { resolvePreferredNodePath } from "../daemon/runtime-paths.js";
|
|
||||||
import { buildServiceEnvironment } from "../daemon/service-env.js";
|
import { buildServiceEnvironment } from "../daemon/service-env.js";
|
||||||
import {
|
import {
|
||||||
emitNodeRuntimeWarning,
|
emitDaemonInstallRuntimeWarning,
|
||||||
type DaemonInstallWarnFn,
|
resolveDaemonInstallRuntimeInputs,
|
||||||
} from "./daemon-install-runtime-warning.js";
|
} from "./daemon-install-plan.shared.js";
|
||||||
|
import type { DaemonInstallWarnFn } from "./daemon-install-runtime-warning.js";
|
||||||
import type { GatewayDaemonRuntime } from "./daemon-runtime.js";
|
import type { GatewayDaemonRuntime } from "./daemon-runtime.js";
|
||||||
|
|
||||||
|
export { resolveGatewayDevMode } from "./daemon-install-plan.shared.js";
|
||||||
|
|
||||||
export type GatewayInstallPlan = {
|
export type GatewayInstallPlan = {
|
||||||
programArguments: string[];
|
programArguments: string[];
|
||||||
workingDirectory?: string;
|
workingDirectory?: string;
|
||||||
environment: Record<string, string | undefined>;
|
environment: Record<string, string | undefined>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function resolveGatewayDevMode(argv: string[] = process.argv): boolean {
|
|
||||||
const entry = argv[1];
|
|
||||||
const normalizedEntry = entry?.replaceAll("\\", "/");
|
|
||||||
return Boolean(normalizedEntry?.includes("/src/") && normalizedEntry.endsWith(".ts"));
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function buildGatewayInstallPlan(params: {
|
export async function buildGatewayInstallPlan(params: {
|
||||||
env: Record<string, string | undefined>;
|
env: Record<string, string | undefined>;
|
||||||
port: number;
|
port: number;
|
||||||
@@ -34,23 +30,22 @@ export async function buildGatewayInstallPlan(params: {
|
|||||||
/** Full config to extract env vars from (env vars + inline env keys). */
|
/** Full config to extract env vars from (env vars + inline env keys). */
|
||||||
config?: OpenClawConfig;
|
config?: OpenClawConfig;
|
||||||
}): Promise<GatewayInstallPlan> {
|
}): Promise<GatewayInstallPlan> {
|
||||||
const devMode = params.devMode ?? resolveGatewayDevMode();
|
const { devMode, nodePath } = await resolveDaemonInstallRuntimeInputs({
|
||||||
const nodePath =
|
env: params.env,
|
||||||
params.nodePath ??
|
runtime: params.runtime,
|
||||||
(await resolvePreferredNodePath({
|
devMode: params.devMode,
|
||||||
env: params.env,
|
nodePath: params.nodePath,
|
||||||
runtime: params.runtime,
|
});
|
||||||
}));
|
|
||||||
const { programArguments, workingDirectory } = await resolveGatewayProgramArguments({
|
const { programArguments, workingDirectory } = await resolveGatewayProgramArguments({
|
||||||
port: params.port,
|
port: params.port,
|
||||||
dev: devMode,
|
dev: devMode,
|
||||||
runtime: params.runtime,
|
runtime: params.runtime,
|
||||||
nodePath,
|
nodePath,
|
||||||
});
|
});
|
||||||
await emitNodeRuntimeWarning({
|
await emitDaemonInstallRuntimeWarning({
|
||||||
env: params.env,
|
env: params.env,
|
||||||
runtime: params.runtime,
|
runtime: params.runtime,
|
||||||
nodeProgram: programArguments[0],
|
programArguments,
|
||||||
warn: params.warn,
|
warn: params.warn,
|
||||||
title: "Gateway runtime",
|
title: "Gateway runtime",
|
||||||
});
|
});
|
||||||
|
|||||||
31
src/commands/daemon-install-plan.shared.test.ts
Normal file
31
src/commands/daemon-install-plan.shared.test.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import {
|
||||||
|
resolveDaemonInstallRuntimeInputs,
|
||||||
|
resolveGatewayDevMode,
|
||||||
|
} from "./daemon-install-plan.shared.js";
|
||||||
|
|
||||||
|
describe("resolveGatewayDevMode", () => {
|
||||||
|
it("detects src ts entrypoints", () => {
|
||||||
|
expect(resolveGatewayDevMode(["node", "/Users/me/openclaw/src/cli/index.ts"])).toBe(true);
|
||||||
|
expect(resolveGatewayDevMode(["node", "C:\\Users\\me\\openclaw\\src\\cli\\index.ts"])).toBe(
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
expect(resolveGatewayDevMode(["node", "/Users/me/openclaw/dist/cli/index.js"])).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("resolveDaemonInstallRuntimeInputs", () => {
|
||||||
|
it("keeps explicit devMode and nodePath overrides", async () => {
|
||||||
|
await expect(
|
||||||
|
resolveDaemonInstallRuntimeInputs({
|
||||||
|
env: {},
|
||||||
|
runtime: "node",
|
||||||
|
devMode: false,
|
||||||
|
nodePath: "/custom/node",
|
||||||
|
}),
|
||||||
|
).resolves.toEqual({
|
||||||
|
devMode: false,
|
||||||
|
nodePath: "/custom/node",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
44
src/commands/daemon-install-plan.shared.ts
Normal file
44
src/commands/daemon-install-plan.shared.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import { resolvePreferredNodePath } from "../daemon/runtime-paths.js";
|
||||||
|
import {
|
||||||
|
emitNodeRuntimeWarning,
|
||||||
|
type DaemonInstallWarnFn,
|
||||||
|
} from "./daemon-install-runtime-warning.js";
|
||||||
|
import type { GatewayDaemonRuntime } from "./daemon-runtime.js";
|
||||||
|
|
||||||
|
export function resolveGatewayDevMode(argv: string[] = process.argv): boolean {
|
||||||
|
const entry = argv[1];
|
||||||
|
const normalizedEntry = entry?.replaceAll("\\", "/");
|
||||||
|
return Boolean(normalizedEntry?.includes("/src/") && normalizedEntry.endsWith(".ts"));
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function resolveDaemonInstallRuntimeInputs(params: {
|
||||||
|
env: Record<string, string | undefined>;
|
||||||
|
runtime: GatewayDaemonRuntime;
|
||||||
|
devMode?: boolean;
|
||||||
|
nodePath?: string;
|
||||||
|
}): Promise<{ devMode: boolean; nodePath?: string }> {
|
||||||
|
const devMode = params.devMode ?? resolveGatewayDevMode();
|
||||||
|
const nodePath =
|
||||||
|
params.nodePath ??
|
||||||
|
(await resolvePreferredNodePath({
|
||||||
|
env: params.env,
|
||||||
|
runtime: params.runtime,
|
||||||
|
}));
|
||||||
|
return { devMode, nodePath };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function emitDaemonInstallRuntimeWarning(params: {
|
||||||
|
env: Record<string, string | undefined>;
|
||||||
|
runtime: GatewayDaemonRuntime;
|
||||||
|
programArguments: string[];
|
||||||
|
warn?: DaemonInstallWarnFn;
|
||||||
|
title: string;
|
||||||
|
}): Promise<void> {
|
||||||
|
await emitNodeRuntimeWarning({
|
||||||
|
env: params.env,
|
||||||
|
runtime: params.runtime,
|
||||||
|
nodeProgram: params.programArguments[0],
|
||||||
|
warn: params.warn,
|
||||||
|
title: params.title,
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
import { formatNodeServiceDescription } from "../daemon/constants.js";
|
import { formatNodeServiceDescription } from "../daemon/constants.js";
|
||||||
import { resolveNodeProgramArguments } from "../daemon/program-args.js";
|
import { resolveNodeProgramArguments } from "../daemon/program-args.js";
|
||||||
import { resolvePreferredNodePath } from "../daemon/runtime-paths.js";
|
|
||||||
import { buildNodeServiceEnvironment } from "../daemon/service-env.js";
|
import { buildNodeServiceEnvironment } from "../daemon/service-env.js";
|
||||||
import { resolveGatewayDevMode } from "./daemon-install-helpers.js";
|
|
||||||
import {
|
import {
|
||||||
emitNodeRuntimeWarning,
|
emitDaemonInstallRuntimeWarning,
|
||||||
type DaemonInstallWarnFn,
|
resolveDaemonInstallRuntimeInputs,
|
||||||
} from "./daemon-install-runtime-warning.js";
|
} from "./daemon-install-plan.shared.js";
|
||||||
|
import type { DaemonInstallWarnFn } from "./daemon-install-runtime-warning.js";
|
||||||
import type { NodeDaemonRuntime } from "./node-daemon-runtime.js";
|
import type { NodeDaemonRuntime } from "./node-daemon-runtime.js";
|
||||||
|
|
||||||
export type NodeInstallPlan = {
|
export type NodeInstallPlan = {
|
||||||
@@ -29,13 +28,12 @@ export async function buildNodeInstallPlan(params: {
|
|||||||
nodePath?: string;
|
nodePath?: string;
|
||||||
warn?: DaemonInstallWarnFn;
|
warn?: DaemonInstallWarnFn;
|
||||||
}): Promise<NodeInstallPlan> {
|
}): Promise<NodeInstallPlan> {
|
||||||
const devMode = params.devMode ?? resolveGatewayDevMode();
|
const { devMode, nodePath } = await resolveDaemonInstallRuntimeInputs({
|
||||||
const nodePath =
|
env: params.env,
|
||||||
params.nodePath ??
|
runtime: params.runtime,
|
||||||
(await resolvePreferredNodePath({
|
devMode: params.devMode,
|
||||||
env: params.env,
|
nodePath: params.nodePath,
|
||||||
runtime: params.runtime,
|
});
|
||||||
}));
|
|
||||||
const { programArguments, workingDirectory } = await resolveNodeProgramArguments({
|
const { programArguments, workingDirectory } = await resolveNodeProgramArguments({
|
||||||
host: params.host,
|
host: params.host,
|
||||||
port: params.port,
|
port: params.port,
|
||||||
@@ -48,10 +46,10 @@ export async function buildNodeInstallPlan(params: {
|
|||||||
nodePath,
|
nodePath,
|
||||||
});
|
});
|
||||||
|
|
||||||
await emitNodeRuntimeWarning({
|
await emitDaemonInstallRuntimeWarning({
|
||||||
env: params.env,
|
env: params.env,
|
||||||
runtime: params.runtime,
|
runtime: params.runtime,
|
||||||
nodeProgram: programArguments[0],
|
programArguments,
|
||||||
warn: params.warn,
|
warn: params.warn,
|
||||||
title: "Node daemon runtime",
|
title: "Node daemon runtime",
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user