- Introduce a new command line utility in commandline.js to manage server status checks and OTP authentication. - Refactor index.js to utilize the new command line handling, improving clarity and separation of concerns. - Enhance localserver.js to include connection status in the server response, providing more detailed information about the server state.
93 lines
2.3 KiB
JavaScript
93 lines
2.3 KiB
JavaScript
import { fileURLToPath } from "node:url";
|
|
import path from "node:path";
|
|
import { loadConfig } from "./config.js";
|
|
import log4js from "log4js";
|
|
import { createElectronWindow } from "./electron/window.js";
|
|
import { setupIPC } from "./electron/ipc.js";
|
|
import { LocalServer } from "./localserver/localserver.js";
|
|
import { SocketClient } from "./socket/socketclient.js";
|
|
import {
|
|
createCliLogger,
|
|
isHeadless,
|
|
startWaiting,
|
|
stopWaiting,
|
|
} from "./commandlineutils.js";
|
|
import { handleCommandLine } from "./commandline.js";
|
|
|
|
if (!process.env.NODE_ENV) {
|
|
process.env.NODE_ENV = "production";
|
|
}
|
|
|
|
const config = loadConfig();
|
|
|
|
const logger = log4js.getLogger("App");
|
|
logger.level = config.logLevel;
|
|
|
|
const cli = createCliLogger(logger);
|
|
|
|
export async function init(options = {}) {
|
|
const headless = options.headless ?? isHeadless;
|
|
cli.blank();
|
|
|
|
const commandLineResult = await handleCommandLine(cli, logger);
|
|
if (commandLineResult.handled) {
|
|
return commandLineResult.result;
|
|
}
|
|
|
|
const runningServer = commandLineResult.runningServer;
|
|
|
|
if (runningServer != false) {
|
|
cli.warn("✘ Farm Control Server is already running.");
|
|
cli.blank();
|
|
return runningServer;
|
|
}
|
|
|
|
if (!headless) {
|
|
startWaiting("Creating electron window...", logger);
|
|
try {
|
|
await createElectronWindow().catch((err) => {
|
|
cli.warn("Failed to create Electron window:", err);
|
|
});
|
|
await stopWaiting();
|
|
} catch (err) {
|
|
await stopWaiting();
|
|
throw err;
|
|
}
|
|
|
|
setupIPC().catch((err) => {
|
|
cli.warn("Failed to setup IPC:", err);
|
|
});
|
|
} else {
|
|
cli.info("Running in headless mode. Skipping window creation.");
|
|
}
|
|
|
|
const socketClient = new SocketClient();
|
|
global.socketClient = socketClient;
|
|
|
|
const localServer = new LocalServer(socketClient);
|
|
await localServer.start();
|
|
|
|
socketClient.connect();
|
|
|
|
process.on("SIGINT", async () => {
|
|
startWaiting("Shutting down...", logger);
|
|
cli.blank();
|
|
socketClient.disconnect();
|
|
localServer.stop();
|
|
await stopWaiting();
|
|
process.exit(0);
|
|
});
|
|
}
|
|
|
|
const isMainModule =
|
|
process.argv[1] &&
|
|
fileURLToPath(import.meta.url) === path.resolve(process.argv[1]);
|
|
|
|
if (isMainModule && !process.env.FC_HEADLESS_ENTRY) {
|
|
init().catch((err) => {
|
|
logger.error(err.message);
|
|
cli.blank();
|
|
process.exit(1);
|
|
});
|
|
}
|