From bf61b1ef1f6405677c3327e80732493cf19fb0ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=BE=D1=80=D0=BE=D0=B4=D0=B8=D0=BD=20=D0=A0=D0=BE?= =?UTF-8?q?=D0=BC=D0=B0=D0=BD?= Date: Sat, 26 Sep 2026 00:17:33 +0300 Subject: [PATCH] feat: use storagePath config for plugin data directory All plugin files (lock, logs) now live under the configured storage path (default ~/.local/share/opencode-matrix-bot/plugin-data), matching matrix-client.ts behavior. No extra directories created. --- index.ts | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/index.ts b/index.ts index 8ea9925..9144e61 100644 --- a/index.ts +++ b/index.ts @@ -1,25 +1,34 @@ import fs from "fs" import os from "os" +import path from "path" import { MatrixBotClient } from "./matrix-client.js" import { SessionManager } from "./session-manager.js" import { loadConfig } from "./config-loader.js" import { log } from "./logger.js" -const PLUGIN_DATA_DIR = `${os.homedir()}/.opencode-matrix-plugin` -if (!fs.existsSync(PLUGIN_DATA_DIR)) { - fs.mkdirSync(PLUGIN_DATA_DIR, { recursive: true }) -} -const LOG_FILE = `${PLUGIN_DATA_DIR}/plugin.log` -const SETUP_STARTED_LOG = `${PLUGIN_DATA_DIR}/setup-started.log` -const SETUP_ERROR_LOG = `${PLUGIN_DATA_DIR}/setup-error.log` -const LOCK_FILE = `${PLUGIN_DATA_DIR}/lock` - log("Plugin module loaded") +// ============================================================================= +// Plugin data directory — uses the same default as matrix-client +// ============================================================================= + +function getPluginDataDir(options: any): string { + const storagePath = options.storagePath || path.join(os.homedir(), ".local", "share", "opencode-matrix-bot") + return path.join(storagePath, "plugin-data") +} + +function ensureDataDir(dir: string): void { + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }) + } +} + // ============================================================================= // Global mutex — prevents double startup (server + client both load plugins) // ============================================================================= +let LOCK_FILE = "" + function tryAcquireLock(): boolean { try { // Check if an existing lock is stale (process no longer running) @@ -59,6 +68,12 @@ function releaseLock(): void { // ============================================================================= async function runPlugin(ctx: any, options: any) { + const dataDir = getPluginDataDir(options) + ensureDataDir(dataDir) + LOCK_FILE = `${dataDir}/lock` + const SETUP_STARTED_LOG = `${dataDir}/setup-started.log` + const SETUP_ERROR_LOG = `${dataDir}/setup-error.log` + // Acquire global lock — only one instance (server or client) starts the bot const locked = tryAcquireLock() if (!locked) { @@ -289,7 +304,12 @@ async function v2Setup(ctx: any) { const { options } = await loadConfig(ctx.options) return await runPlugin(ctx, options) } catch (err) { - fs.appendFileSync(SETUP_ERROR_LOG, `[${new Date().toISOString()}] ${String(err)}\n`) + const rawStorage = (ctx.options as any)?.storagePath + const dataDir = rawStorage + ? path.join(rawStorage, "plugin-data") + : path.join(os.homedir(), ".local", "share", "opencode-matrix-bot", "plugin-data") + ensureDataDir(dataDir) + fs.appendFileSync(`${dataDir}/setup-error.log`, `[${new Date().toISOString()}] ${String(err)}\n`) log(`V2 setup error: ${String(err)}`) return () => {} }