// Plugin-tree maintenance for the desktop profile (no Electron imports, so this // is unit-testable with plain node). // // The desktop may configure an operator-selected plugin directory, but it never // owns the files in that directory. In particular, upgrades must not seed, // replace, rename, or delete plugins. Plugin installation is an explicit // operator action (or an external package manager action). const fs = require("fs"); // ensurePluginDirConfigured back-fills `plugin_dir` into an existing config. // Appending a top-level YAML key is safe for the shapes this file can have // (flat mapping + a keys list). Returns "added" | "present" | "absent" | "error". function ensurePluginDirConfigured(configFile, pluginsDir, log) { try { if (!fs.existsSync(configFile)) return "absent"; const cur = fs.readFileSync(configFile, "utf-8"); if (/^\s*plugin_dir\s*:/m.test(cur)) return "present"; fs.writeFileSync( configFile, cur.replace(/\s*$/, "") + "\n# added by ModelRouter Desktop: enables the operator plugin directory\n" + "plugin_dir: " + JSON.stringify(pluginsDir) + "\n", "utf-8", ); if (log) log("[gui] plugin_dir back-filled into " + configFile); return "added"; } catch (e) { if (log) log("[gui] ensurePluginDirConfigured failed: " + e.message); return "error"; } } module.exports = { ensurePluginDirConfigured };