commit 1255d4cfdf5dc1352dea9857eeee4579305dabc3
parent 3270d40b987ac7ada2b2d9b9b20deed80f380be3
Author: Henry Unite <unitehenry@gmail.com>
Date: Sat, 24 Oct 2026 22:58:24 +0000
refactor: extract setup logic into context/ directory
- Move registerSetup() and OpenpodContext definition into context/index.ts as init()
- Extract writeLog logic to context/log.ts (exported as createLog)
- Extract isCommandAvailable to context/command.ts
- Extract cleanupOpenpod to context/cleanup.ts (exported as createCleanup)
- Delete setup.ts
Diffstat:
5 files changed, 120 insertions(+), 100 deletions(-)
diff --git a/context/cleanup.ts b/context/cleanup.ts
@@ -0,0 +1,51 @@
+import * as fs from "node:fs";
+import { spawnSync } from "node:child_process";
+import * as path from "node:path";
+
+export type Cleanup = (sessionID: string) => Promise<void>;
+
+export function createCleanup(
+ openpodRoot: string,
+ log: (message: string, level?: string) => void,
+): Cleanup {
+ return async function cleanup(sessionID: string): Promise<void> {
+ if (!sessionID || typeof sessionID !== "string") return;
+
+ const limaName = sessionID;
+ const podDir = path.join(openpodRoot, sessionID);
+
+ log(`[cleanup] starting for ${sessionID}`);
+
+ // best-effort stop (ignore errors)
+ try {
+ spawnSync("limactl", ["stop", limaName, "--force"], { stdio: "ignore" });
+ log(`[cleanup] stopped ${limaName} (if running)`);
+ } catch {
+ // ignore
+ }
+
+ // delete the lima instance
+ try {
+ const result = spawnSync("limactl", ["delete", limaName, "--force"], {
+ stdio: "ignore",
+ });
+ log(
+ `[cleanup] limactl delete ${limaName} exited with ${result.status}`,
+ );
+ } catch (err) {
+ log(`[cleanup] limactl delete error for ${limaName}: ${err}`);
+ }
+
+ // remove the openpod directory
+ try {
+ if (fs.existsSync(podDir)) {
+ fs.rmSync(podDir, { recursive: true, force: true });
+ log(`[cleanup] removed directory ${podDir}`);
+ } else {
+ log(`[cleanup] directory did not exist: ${podDir}`);
+ }
+ } catch (err) {
+ log(`[cleanup] failed to remove ${podDir}: ${err}`);
+ }
+ };
+}
diff --git a/context/command.ts b/context/command.ts
@@ -0,0 +1,17 @@
+import { execSync } from "node:child_process";
+
+export 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;
+ }
+}
diff --git a/context/index.ts b/context/index.ts
@@ -0,0 +1,31 @@
+import * as os from "node:os";
+import * as path from "node:path";
+
+import { createLog } from "./log";
+import { isCommandAvailable } from "./command";
+import { createCleanup } from "./cleanup";
+
+export type OpenpodContext = {
+ log: (message: string, level?: string) => void;
+ openpodRoot: string;
+ cleanup: (sessionID: string) => Promise<void>;
+};
+
+export async function init(): Promise<OpenpodContext> {
+ const openpodRoot = path.join(os.homedir(), ".local", "share", "openpod");
+
+ const log = createLog(openpodRoot);
+ const cleanup = createCleanup(openpodRoot, log);
+
+ if (!isCommandAvailable("limactl")) {
+ log(
+ "`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 { log, openpodRoot, cleanup };
+}
diff --git a/context/log.ts b/context/log.ts
@@ -0,0 +1,21 @@
+import * as fs from "node:fs";
+import * as path from "node:path";
+
+export function createLog(openpodRoot: string) {
+ const logDir = path.join(openpodRoot, ".log");
+ const logFile = path.join(logDir, "openpod.log");
+
+ return function log(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}\n`,
+ );
+ } catch {
+ // ignore logging failures
+ }
+ };
+}
diff --git a/setup.ts b/setup.ts
@@ -1,100 +0,0 @@
-import { execSync, spawnSync } from "node:child_process";
-import * as fs from "node:fs";
-import * as os from "node:os";
-import * as path from "node:path";
-
-export type OpenpodContext = {
- writeLog: (message: string, level?: string) => void;
- openpodRoot: string;
- cleanupOpenpod: (sessionID: string) => Promise<void>;
-};
-
-export async function registerSetup(): Promise<OpenpodContext> {
- 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
- }
- }
-
- 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;
- }
- }
-
- async function cleanupOpenpod(sessionID: string): Promise<void> {
- if (!sessionID || typeof sessionID !== "string") return;
-
- const limaName = sessionID;
- const podDir = path.join(openpodRoot, sessionID);
-
- writeLog(`[cleanup] starting for ${sessionID}`);
-
- // best-effort stop (ignore errors)
- try {
- spawnSync("limactl", ["stop", limaName, "--force"], { stdio: "ignore" });
- writeLog(`[cleanup] stopped ${limaName} (if running)`);
- } catch {
- // ignore
- }
-
- // delete the lima instance
- try {
- const result = spawnSync("limactl", ["delete", limaName, "--force"], {
- stdio: "ignore",
- });
- writeLog(
- `[cleanup] limactl delete ${limaName} exited with ${result.status}`,
- );
- } catch (err) {
- writeLog(`[cleanup] limactl delete error for ${limaName}: ${err}`);
- }
-
- // remove the openpod directory
- try {
- if (fs.existsSync(podDir)) {
- fs.rmSync(podDir, { recursive: true, force: true });
- writeLog(`[cleanup] removed directory ${podDir}`);
- } else {
- writeLog(`[cleanup] directory did not exist: ${podDir}`);
- }
- } catch (err) {
- writeLog(`[cleanup] failed to remove ${podDir}: ${err}`);
- }
- }
-
- 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, cleanupOpenpod };
-}