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.
This commit is contained in:
Бородин Роман 2026-09-26 00:17:33 +03:00
parent a5dda163ea
commit bf61b1ef1f
1 changed files with 30 additions and 10 deletions

View File

@ -1,25 +1,34 @@
import fs from "fs" import fs from "fs"
import os from "os" import os from "os"
import path from "path"
import { MatrixBotClient } from "./matrix-client.js" import { MatrixBotClient } from "./matrix-client.js"
import { SessionManager } from "./session-manager.js" import { SessionManager } from "./session-manager.js"
import { loadConfig } from "./config-loader.js" import { loadConfig } from "./config-loader.js"
import { log } from "./logger.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") 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) // Global mutex — prevents double startup (server + client both load plugins)
// ============================================================================= // =============================================================================
let LOCK_FILE = ""
function tryAcquireLock(): boolean { function tryAcquireLock(): boolean {
try { try {
// Check if an existing lock is stale (process no longer running) // 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) { 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 // Acquire global lock — only one instance (server or client) starts the bot
const locked = tryAcquireLock() const locked = tryAcquireLock()
if (!locked) { if (!locked) {
@ -289,7 +304,12 @@ async function v2Setup(ctx: any) {
const { options } = await loadConfig(ctx.options) const { options } = await loadConfig(ctx.options)
return await runPlugin(ctx, options) return await runPlugin(ctx, options)
} catch (err) { } 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)}`) log(`V2 setup error: ${String(err)}`)
return () => {} return () => {}
} }