"use strict"; const fs = require("node:fs"); const path = require("node:path"); const dotenv = require("dotenv"); const AGENT_ENABLED_KEY = "ADPANEL_AGENT_ENABLED"; const PANEL_ENV_PATH = path.resolve(__dirname, "..", ".env"); function readPanelEnv(envPath = PANEL_ENV_PATH) { return dotenv.parse(fs.readFileSync(envPath)); } function readAgentFeatureConfig(envPath = PANEL_ENV_PATH) { let parsed; try { parsed = readPanelEnv(envPath); } catch (error) { return { enabled: false, reason: error.code === "ENOENT" ? "missing" : "unreadable" }; } const value = parsed[AGENT_ENABLED_KEY]; if (value === "ON") return { enabled: true, reason: "enabled" }; if (value === "OFF") return { enabled: false, reason: "disabled" }; if (value === "NOT-ASKED") return { enabled: false, reason: "not-asked" }; return { enabled: false, reason: value === undefined ? "missing" : "invalid" }; } function reportAgentFeatureConfig(config, logger = console) { if (config.reason === "missing") { logger.info(`[Agent] ${AGENT_ENABLED_KEY} is not set in the panel .env; the feature stays OFF until explicitly enabled.`); } else if (config.reason === "invalid") { logger.warn(`[Agent] ${AGENT_ENABLED_KEY} in the panel .env must be ON, OFF, or NOT-ASKED; treating it as OFF.`); } else if (config.reason === "unreadable") { logger.warn("[Agent] Unable to read the panel .env; treating the feature as OFF."); } } if (require.main === module) process.exitCode = readAgentFeatureConfig().enabled ? 0 : 1; module.exports = { AGENT_ENABLED_KEY, PANEL_ENV_PATH, readPanelEnv, readAgentFeatureConfig, reportAgentFeatureConfig };