commit eb65f83a072bc0dcc4c7d3f8a04aa44961be03f4
parent bb52686ed6f8b146bc2a81db6381de11edf09c15
Author: Henry Unite <unitehenry@gmail.com>
Date: Mon, 19 Oct 2026 20:56:10 +0000
refactor: extract setup logic into setup.ts and introduce OpenpodContext
- Move limactl availability check + helpers (writeLog, openpodRoot) out of index.ts
- registerSetup now returns an OpenpodContext object
- Update prompt/shell/controller/command to consume OpenpodContext
- Add error-level writeLog before the limactl installation error
- Enhance writeLog with optional level (defaults to info, no output change for normal logs)
Diffstat:
6 files changed, 77 insertions(+), 54 deletions(-)
diff --git a/command.ts b/command.ts
@@ -3,9 +3,11 @@ import { spawnSync } from "node:child_process";
import * as fs from "node:fs";
import * as path from "node:path";
+import type { OpenpodContext } from "./setup";
+
export async function cleanupOpenpod(
sessionID: string,
- deps: { writeLog: (message: string) => void; openpodRoot: string },
+ deps: OpenpodContext,
): Promise<void> {
const { writeLog, openpodRoot } = deps;
if (!sessionID || typeof sessionID !== "string") return;
@@ -49,7 +51,7 @@ export async function cleanupOpenpod(
}
export async function clearAllOpenpod(
- deps: { writeLog: (message: string) => void; openpodRoot: string },
+ deps: OpenpodContext,
): Promise<void> {
const { writeLog, openpodRoot } = deps;
writeLog("[clear] starting full openpod cleanup");
@@ -79,7 +81,7 @@ export async function clearAllOpenpod(
export async function registerCommand(
ctx: Plugin.Context,
- deps: { writeLog: (message: string) => void; openpodRoot: string },
+ deps: OpenpodContext,
): Promise<void> {
writeLog("registering commands");
diff --git a/controller.ts b/controller.ts
@@ -1,13 +1,13 @@
import type { Plugin } from "@opencode/plugin";
import { cleanupOpenpod, registerCommand } from "./command";
+import type { OpenpodContext } from "./setup";
+
export async function registerController(
ctx: Plugin.Context,
- deps: { writeLog: (message: string) => void; openpodRoot: string },
+ deps: OpenpodContext,
): Promise<() => void> {
- const { writeLog, openpodRoot } = deps;
-
- await registerCommand(ctx, { writeLog, openpodRoot });
+ await registerCommand(ctx, deps);
// Listen for session removal (via events, since there is no ctx.session.hook("remove"))
// and clean up the corresponding openpod directory + lima instance.
@@ -25,13 +25,13 @@ export async function registerController(
props.info?.id ??
(ev as any).sessionID;
if (sessionID) {
- void cleanupOpenpod(sessionID, { writeLog, openpodRoot });
+ void cleanupOpenpod(sessionID, deps);
}
}
}
} catch (err: any) {
if (err?.name !== "AbortError") {
- writeLog(`event subscription error: ${err}`);
+ deps.writeLog(`event subscription error: ${err}`);
}
}
})();
diff --git a/index.ts b/index.ts
@@ -1,56 +1,18 @@
import { Plugin } from "@opencode/plugin";
-import { execSync } from "node:child_process";
-import * as fs from "node:fs";
-import * as os from "node:os";
-import * as path from "node:path";
import { registerPrompt } from "./prompt";
import { registerShell } from "./shell";
import { registerController } from "./controller";
-
-const openpodRoot = path.join(os.homedir(), ".local", "share", "openpod");
-const logDir = path.join(openpodRoot, ".log");
-const logFile = path.join(logDir, "openpod.log");
-
-function writeLog(message: string): void {
- try {
- fs.mkdirSync(logDir, { recursive: true });
- const timestamp = new Date().toISOString();
- fs.appendFileSync(
- logFile,
- `[${timestamp}] [openpod] ${message}
-`,
- );
- } catch {
- // ignore logging failures
- }
-}
-
-function isCommandAvailable(command: string): boolean {
- try {
- if (process.platform === "win32") {
- execSync(`where ${command}`, { stdio: "ignore" });
- } else {
- execSync(`command -v ${command}`, { stdio: "ignore", shell: "/bin/sh" });
- }
- return true;
- } catch {
- return false;
- }
-}
+import { registerSetup } from "./setup";
export default Plugin.define({
id: "openpod",
async setup(ctx) {
- if (!isCommandAvailable("limactl")) {
- throw new Error(
- "`limactl` is not installed. Lima is required for openpod. See https://lima-vm.io/",
- );
- }
+ const openpod = await registerSetup();
- await registerPrompt(ctx, { writeLog, openpodRoot });
- await registerShell(ctx, { writeLog, openpodRoot });
+ await registerPrompt(ctx, openpod);
+ await registerShell(ctx, openpod);
- return await registerController(ctx, { writeLog, openpodRoot });
+ return await registerController(ctx, openpod);
},
});
diff --git a/prompt.ts b/prompt.ts
@@ -3,9 +3,11 @@ import { spawnSync } from "node:child_process";
import * as fs from "node:fs";
import * as path from "node:path";
+import type { OpenpodContext } from "./setup";
+
export async function registerPrompt(
ctx: Plugin.Context,
- deps: { writeLog: (message: string) => void; openpodRoot: string },
+ deps: OpenpodContext,
): Promise<void> {
const { writeLog, openpodRoot } = deps;
diff --git a/setup.ts b/setup.ts
@@ -0,0 +1,55 @@
+import { execSync } from "node:child_process";
+import * as fs from "node:fs";
+import * as os from "node:os";
+import * as path from "node:path";
+
+const openpodRoot = path.join(os.homedir(), ".local", "share", "openpod");
+const logDir = path.join(openpodRoot, ".log");
+const logFile = path.join(logDir, "openpod.log");
+
+function writeLog(message: string, level: string = "info"): void {
+ try {
+ fs.mkdirSync(logDir, { recursive: true });
+ const timestamp = new Date().toISOString();
+ const levelPart = level === "info" ? "" : `[${level}] `;
+ fs.appendFileSync(
+ logFile,
+ `[${timestamp}] [openpod] ${levelPart}${message}
+`,
+ );
+ } catch {
+ // ignore logging failures
+ }
+}
+
+export type OpenpodContext = {
+ writeLog: (message: string, level?: string) => void;
+ openpodRoot: string;
+};
+
+function isCommandAvailable(command: string): boolean {
+ try {
+ if (process.platform === "win32") {
+ execSync(`where ${command}`, { stdio: "ignore" });
+ } else {
+ execSync(`command -v ${command}`, { stdio: "ignore", shell: "/bin/sh" });
+ }
+ return true;
+ } catch {
+ return false;
+ }
+}
+
+export async function registerSetup(): Promise<OpenpodContext> {
+ if (!isCommandAvailable("limactl")) {
+ writeLog(
+ "`limactl` is not installed. Lima is required for openpod. See https://lima-vm.io/",
+ "error",
+ );
+ throw new Error(
+ "`limactl` is not installed. Lima is required for openpod. See https://lima-vm.io/",
+ );
+ }
+
+ return { writeLog, openpodRoot };
+}
diff --git a/shell.ts b/shell.ts
@@ -1,9 +1,11 @@
import type { Plugin } from "@opencode/plugin";
import * as path from "node:path";
+import type { OpenpodContext } from "./setup";
+
export async function registerShell(
ctx: Plugin.Context,
- deps: { writeLog: (message: string) => void; openpodRoot: string },
+ deps: OpenpodContext,
): Promise<void> {
const { writeLog, openpodRoot } = deps;