// 37du Electron 壳:配置固化(.env 加载 + 路径 + 常量) // 对齐 Python webhost backend/core/config.py 的语义与路径约定。 "use strict"; const fs = require("fs"); const path = require("path"); // 打包态判定:Electron 运行时 + resources 下存在 app.asar(打包版独有,dev `electron .` 无 asar)。 // 不能依赖 ELECTRON_RENDERER_URL(那是 electron-vite 等 dev-server 工具的变量,本工程从不设置)。 const IS_FROZEN = !!process.versions && !!process.versions.electron && !!process.resourcesPath && !process.env.ELECTRON_RUN_AS_NODE && fs.existsSync(path.join(process.resourcesPath, "app.asar")); // 本模块目录:desktop/electron-shell/electron → parents const SHELL_ROOT = path.resolve(__dirname, ".."); // 仓库根:electron/../.. = 37duAutomation(dev 模式定位 plugins/ 与 webhost 参考实现) const REPO_ROOT = path.resolve(SHELL_ROOT, "..", ".."); const DESKTOP_ROOT = path.resolve(SHELL_ROOT, ".."); // ==================== 轻量 .env 加载(仅本模块目录,变量已存在则不覆盖) ==================== function loadDotEnv() { // 开发/CLI:读本模块 .env;打包态额外读 resources/.env(electron-builder extraResources 带入, // 位于 asar 外的真实文件,可随包分发或按机器覆盖) const candidates = [path.join(SHELL_ROOT, ".env")]; if (IS_FROZEN) candidates.push(path.join(process.resourcesPath, ".env")); for (const envPath of candidates) { if (!fs.existsSync(envPath)) continue; try { const text = fs.readFileSync(envPath, "utf-8"); for (const rawLine of text.split(/\r?\n/)) { const line = rawLine.trim(); if (!line || line.startsWith("#") || !line.includes("=")) continue; const idx = line.indexOf("="); const key = line.slice(0, idx).trim(); const value = line.slice(idx + 1).trim(); if (key && process.env[key] === undefined) process.env[key] = value; } } catch (e) { // 读取失败静默(与 Python 实现一致) } } } loadDotEnv(); // ==================== 基础常量 ==================== const PORT = 8799; // 数据根目录:与影刀版 / 旧 Python 底座共享 %LOCALAPPDATA%\37duAutomation(跨端契约) function localAppData() { return process.env.LOCALAPPDATA || process.env.APPDATA || require("os").homedir(); } const APP_DATA = path.join(localAppData(), "37duAutomation"); // 插件本地配置目录:per-plugin JSON 持久化 const PLUGIN_CONFIG_DIR = path.join(APP_DATA, "plugin_configs"); // 插件源码/安装根:dev = 仓库根 plugins/;打包 = exe 同级 plugins/ function pluginRoot() { if (process.env.SHELL_PLUGIN_ROOT) return process.env.SHELL_PLUGIN_ROOT; if (IS_FROZEN) { return path.join(path.dirname(process.execPath), "plugins"); } return path.join(REPO_ROOT, "plugins"); } // 运行时版本:打包态读 exe 同级 shell.version(发版文件);开发/CLI 回退 package.json 版本。 // 插件 plugin.json 可声明 minRuntimeVersion 做版本兼容校验(不满足则拒绝安装)。 function runtimeVersion() { try { if (!process.env.ELECTRON_RUN_AS_NODE && process.resourcesPath) { const vf = path.join(path.dirname(process.execPath), "shell.version"); if (fs.existsSync(vf)) { const v = fs.readFileSync(vf, "utf-8").trim(); if (v) return v; } } const pkg = JSON.parse(fs.readFileSync(path.join(SHELL_ROOT, "package.json"), "utf-8")); return String(pkg.version || "").trim(); } catch (e) { return ""; } } // ==================== 控制面配置 ==================== const CONTROLPLANE_URL = (process.env.CONTROLPLANE_URL || "").replace(/\/+$/, ""); const CONTROLPLANE_ACCOUNT = process.env.CONTROLPLANE_ACCOUNT || ""; const CONTROLPLANE_TOKEN = process.env.CONTROLPLANE_TOKEN || ""; // ==================== 插件执行子进程配置 ==================== // Python 解释器:优先 env,默认 python(PATH 中的 venv/系统 Python) const PYTHON_EXE = process.env.PYTHON_EXE || "python"; const RUN_TIMEOUT = parseInt(process.env.RUN_TIMEOUT || "1800", 10); const RUN_MAX_MEM_MB = parseInt(process.env.RUN_MAX_MEM_MB || "2048", 10); // ==================== 便携 Python 环境包(python-env) ==================== // 版本化目录(python-env//)+ current.json 指向当前版本,保留上一版供回滚。 // 目录位置:打包态 = %LOCALAPPDATA%\37duAutomation\python-env——NSIS 重装/升级会清空安装目录, // 放 exe 同级会导致环境包每次陪葬重新下载解压(其余持久化数据本就都在 APP_DATA); // 开发态 = electron-shell/python-env/(可本地构建联调)。 function envRootDir() { if (process.env.SHELL_PYTHON_ENV_ROOT) return path.resolve(process.env.SHELL_PYTHON_ENV_ROOT); if (IS_FROZEN) return path.join(APP_DATA, "python-env"); return path.join(SHELL_ROOT, "python-env"); } // 当前环境包目录(current.json → 版本目录);无环境包返回 "" function pythonEnvRoot() { const dir = envRootDir(); try { const raw = fs.readFileSync(path.join(dir, "current.json"), "utf-8").replace(/^\uFEFF/, ""); const cur = JSON.parse(raw); const ver = String((cur || {}).version || "").trim(); if (ver && fs.existsSync(path.join(dir, ver, "python.exe"))) return path.join(dir, ver); } catch (e) { /* 尚无环境包 */ } return ""; } // 环境包解析的 Python 解释器:显式 PYTHON_EXE 优先,其次环境包,最后系统 python function pythonExe() { if (process.env.PYTHON_EXE) return process.env.PYTHON_EXE; const envRoot = pythonEnvRoot(); if (envRoot) return path.join(envRoot, "python.exe"); return "python"; } // 运行时根(webhost 包所在目录):环境包模式 = 环境包根;否则 = desktop/(dev,webhost 在仓库内) function runtimeRoot() { const envRoot = pythonEnvRoot(); if (envRoot) return envRoot; return DESKTOP_ROOT; } // 插件执行入口:环境包模式 = 环境包内 plugin_runner.py;打包态 = exe 同级;dev = 仓库 webhost function runnerScript() { if (process.env.SHELL_RUNNER_SCRIPT) return process.env.SHELL_RUNNER_SCRIPT; const envRoot = pythonEnvRoot(); if (envRoot) return path.join(envRoot, "webhost", "backend", "core", "plugin_runner.py"); if (IS_FROZEN) { return path.join(path.dirname(process.execPath), "plugin_runner.py"); } return path.join(DESKTOP_ROOT, "webhost", "backend", "core", "plugin_runner.py"); } // Job Object 守护(环境包模式启用内存上限 + 整树托管):无环境包返回 "" function jobGuardScript() { const envRoot = pythonEnvRoot(); if (envRoot) return path.join(envRoot, "webhost", "backend", "core", "job_guard.py"); return ""; } // 前端静态资源:壳自带 renderer/dist(可单独拷贝);dev 兜底复用 webhost 构建产物 function frontendDist() { const own = path.join(SHELL_ROOT, "renderer", "dist"); if (fs.existsSync(path.join(own, "index.html"))) return own; const legacy = path.join(DESKTOP_ROOT, "webhost", "frontend", "dist"); if (fs.existsSync(path.join(legacy, "index.html"))) return legacy; return own; } // 平台登录子进程:spawn python (webhost.backend.shared.hr_login/oa_login) function sharedModule(moduleName) { return path.join(DESKTOP_ROOT, "webhost", "backend", "shared", `${moduleName}.py`); } // 敏感凭证中心化下发:进程级覆盖存储(唯一来源:中心机下发 / 本地加密缓存) const _CREDS_OVERRIDE = {}; function applyCreds(values) { if (values && typeof values === "object") { Object.assign(_CREDS_OVERRIDE, values); } } function getCred(name) { if (_CREDS_OVERRIDE[name] !== undefined) return _CREDS_OVERRIDE[name]; return process.env[name] !== undefined ? process.env[name] : ""; } module.exports = { SHELL_ROOT, REPO_ROOT, DESKTOP_ROOT, PORT, APP_DATA, PLUGIN_CONFIG_DIR, CONTROLPLANE_URL, CONTROLPLANE_ACCOUNT, CONTROLPLANE_TOKEN, PYTHON_EXE, RUN_TIMEOUT, RUN_MAX_MEM_MB, IS_FROZEN, pluginRoot, runnerScript, frontendDist, sharedModule, runtimeVersion, applyCreds, getCred, envRootDir, pythonEnvRoot, pythonExe, runtimeRoot, jobGuardScript, };