Refactor headless mode initialization and enhance index.js structure
Some checks failed
farmcontrol/farmcontrol-server/pipeline/head There was a failure building this commit

Replace the previous headless mode implementation in headless.js with a streamlined init function in index.js that accepts options. This change improves modularity and error handling during initialization. The logger setup and socket client connection have been removed from headless.js, centralizing the logic in index.js.
This commit is contained in:
Tom Butcher 2026-07-26 23:21:07 +01:00
parent a62df6a5c6
commit 4d35df3051
2 changed files with 21 additions and 25 deletions

View File

@ -1,20 +1,6 @@
import { loadConfig } from './config.js' import { init } from "./index.js";
import log4js from 'log4js'
import { SocketClient } from './socket/socketclient.js'
const config = loadConfig() init({ headless: true }).catch((err) => {
console.error(err.message);
const logger = log4js.getLogger('App') process.exit(1);
logger.level = config.logLevel });
logger.info('Running in headless mode.')
const socketClient = new SocketClient()
global.socketClient = socketClient
socketClient.connect()
process.on('SIGINT', () => {
logger.info('Shutting down...')
socketClient.disconnect()
process.exit(0)
})

View File

@ -1,3 +1,5 @@
import { fileURLToPath } from "node:url";
import path from "node:path";
import { loadConfig } from "./config.js"; import { loadConfig } from "./config.js";
import axios from "axios"; import axios from "axios";
import log4js from "log4js"; import log4js from "log4js";
@ -81,7 +83,9 @@ async function authenticateWithOtp(otpCode) {
throw new Error("Failed to authenticate with OTP"); throw new Error("Failed to authenticate with OTP");
} }
} }
export async function init() { export async function init(options = {}) {
const headless = options.headless ?? isHeadless;
logger.info("⌛ Checking if Farm Control Server is running..."); logger.info("⌛ Checking if Farm Control Server is running...");
const runningServer = await checkRunning(); const runningServer = await checkRunning();
@ -127,7 +131,7 @@ export async function init() {
return runningServer; return runningServer;
} }
if (!isHeadless) { if (!headless) {
// Create Electron window first // Create Electron window first
logger.info("Creating electron window..."); logger.info("Creating electron window...");
await createElectronWindow().catch((err) => { await createElectronWindow().catch((err) => {
@ -159,7 +163,13 @@ export async function init() {
}); });
} }
init().catch((err) => { const isMainModule =
logger.error(err.message); process.argv[1] &&
process.exit(1); fileURLToPath(import.meta.url) === path.resolve(process.argv[1]);
});
if (isMainModule) {
init().catch((err) => {
logger.error(err.message);
process.exit(1);
});
}