Tom Butcher 997b374e0e
All checks were successful
farmcontrol/farmcontrol-server/pipeline/head This commit looks good
Add before-remove script for package uninstallation and streamline after-remove script
Introduce a new before-remove.sh script to stop and disable the farmcontrol-server service before package removal. Update after-remove.sh to only reload systemd, simplifying the uninstallation process. Modify build-linux-packages.sh to include the new before-remove script in the packaging process.
2026-07-26 22:56:42 +01:00

167 lines
4.1 KiB
JavaScript

// config.js - Configuration handling
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import log4js from "log4js";
// Determine environment
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const isPackaged = __dirname.includes("app.asar");
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = "production";
}
const NODE_ENV = process.env.NODE_ENV;
// Configure paths
function getProductionConfigPath() {
const homeDir = process.env.HOME || process.env.USERPROFILE || "";
switch (process.platform) {
case "win32":
return path.join(
process.env.PROGRAMDATA || "C:\\ProgramData",
"farmcontrol-server",
"config.json"
);
case "darwin":
return path.join(
homeDir,
"Library",
"Application Support",
"farmcontrol-server",
"config.json"
);
case "linux":
default:
return "/etc/farmcontrol-server/config.json";
}
}
function getDevelopmentConfigPath() {
// If we're in a packaged app (asar), we can't write to the app directory.
// We should use a writable user data directory instead.
if (isPackaged) {
const homeDir = process.env.HOME || process.env.USERPROFILE || "";
switch (process.platform) {
case "win32":
return path.join(
process.env.APPDATA || path.join(homeDir, "AppData", "Roaming"),
"farmcontrol-server",
"config.json"
);
case "darwin":
return path.join(
homeDir,
"Library",
"Application Support",
"farmcontrol-server",
"config.json"
);
default:
return path.join(
homeDir,
".config",
"farmcontrol-server",
"config.json"
);
}
}
return path.resolve(__dirname, "../config.json");
}
const CONFIG_PATH =
NODE_ENV === "production"
? getProductionConfigPath()
: getDevelopmentConfigPath();
const logger = log4js.getLogger("Config");
logger.level = "info";
const DEFAULT_CONFIG = {
development: {
logLevel: "debug",
url: "https://dev-wss.tombutcher.work",
apiUrl: "https://dev.tombutcher.work/api",
host: {
id: "",
authCode: "",
},
},
production: {
logLevel: "info",
url: "https://ws.farmcontrol.app",
apiUrl: "https://api.farmcontrol.app",
host: {
id: "",
authCode: "",
},
},
};
// Load config file
export function loadConfig() {
try {
if (!fs.existsSync(CONFIG_PATH)) {
logger.info(
`Configuration file not found at ${CONFIG_PATH}. Creating a new one.`
);
const configDir = path.dirname(CONFIG_PATH);
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
fs.writeFileSync(
CONFIG_PATH,
JSON.stringify(DEFAULT_CONFIG, null, 2),
"utf8"
);
}
const configData = fs.readFileSync(CONFIG_PATH, "utf8");
const config = JSON.parse(configData);
if (!config[NODE_ENV]) {
throw new Error(
`Configuration for environment '${NODE_ENV}' not found in config.json`
);
}
return config[NODE_ENV];
} catch (err) {
logger.error("Error loading config:", err);
throw err;
}
}
// Save config file
export function saveConfig(newConfig) {
try {
logger.info("Saving...");
let config = {};
if (fs.existsSync(CONFIG_PATH)) {
const configData = fs.readFileSync(CONFIG_PATH, "utf8");
config = JSON.parse(configData);
} else {
const configDir = path.dirname(CONFIG_PATH);
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
}
// Update current environment
config[NODE_ENV] = newConfig;
// Write back to file with 2-space indentation
fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), "utf8");
logger.info(`Configuration for '${NODE_ENV}' saved successfully.`);
} catch (err) {
logger.error("Error saving config:", err);
throw err;
}
}
// Get current environment
export function getEnvironment() {
return NODE_ENV;
}