Files
mrs/client/src/services/environment.mjs
T
2026-04-06 21:43:08 -04:00

39 lines
1.2 KiB
JavaScript

import dotenv from 'dotenv';
import * as fs from 'node:fs';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import { execSync } from "child_process";
dotenv.config({path: ['.env','.env.local'], debug: false, quiet: true});
export const environment = {
config: process.env.CONFIG || 'config.json',
hostname: process.env.HOSTNAME || execSync('hostname', { encoding: "utf-8" }) || 'localhost',
gpsd: process.env.GPSD || 2947,
port: process.env.PORT || 80,
root: dirname(dirname(fileURLToPath(import.meta.url))),
settings: {},
}
if(!environment.config.startsWith('/')) environment.config = `${environment.root}/${environment.config}`;
export function loadSettings() {
if(!fs.existsSync(environment.config)) fs.writeFileSync(environment.config, '', { flag: 'a' });
const value = fs.readFileSync(environment.config, 'utf-8');
try { return JSON.parse(value); }
catch (e) {
return {
lora: {
mode: 'MESH',
telemetry: true,
}
};
}
}
export function saveSettings() {
fs.writeFileSync(environment.config, JSON.stringify(environment.settings, null, 4));
}
loadSettings();