Skip to content

Commit

Permalink
Commented checkServers
Browse files Browse the repository at this point in the history
  • Loading branch information
truemiller committed Feb 16, 2024
1 parent 1360b27 commit c9c8226
Showing 1 changed file with 55 additions and 52 deletions.
107 changes: 55 additions & 52 deletions electron/main.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const { app, BrowserWindow, Tray, Menu, shell } = require("electron");
const path = require("path");
const { exec } = require("child_process");
const { isPortAvailable, portRange, findAvailablePort } = require("./ports");
const { isPortAvailable, portRange, findAvailablePort } = require("./ports")

let tray, mainWindow, splashWindow;
let backendProcess, frontendProcess, hardhatProcess;
let processList = [];

const DEFAULT_PORTS = {
backend: 8000,
frontend: 3000,
hardhat: 8545,

let processes = {
backend: {port: 8000, ready: false},
frontend: {port: 3000, ready: false},
hardhat: {port: 8545, ready: false}, //temporary
};

const killAllProcesses = () =>
Expand All @@ -19,16 +19,19 @@ const killAllProcesses = () =>
p.kill();
});

const launchProcesses = async () => {
let backendPort = DEFAULT_PORTS.backend,
frontendPort = DEFAULT_PORTS.frontend,
hardhatPort = DEFAULT_PORTS.hardhat;
const checkServersReadyThenMain = () => {
const allReady = Object.values(processes).every((p) => p.ready);
if (allReady) {
createMainWindow();
}
};

const launchProcesses = async () => {
// backend
try {
const backendPortAvailable = await isPortAvailable(backendPort);
const backendPortAvailable = await isPortAvailable(processes.backend.port);
if (!backendPortAvailable) {
backendPort = await findAvailablePort(
processes.backend.port = await findAvailablePort(
portRange.startPort,
portRange.endPort,
);
Expand All @@ -38,45 +41,54 @@ const launchProcesses = async () => {
app.quit();
}

backendProcess = exec(`yarn dev:backend`);
const backendProcess = exec(`yarn dev:backend`);
processList.push(backendProcess);
backendProcess.stdout.on("data", (data) =>
console.log("[BACKEND]: ", data.toString()),
);
backendProcess.stderr.on("data", (data) =>
backendProcess.stdout?.on("data", (data) => {
// if(data.toString().includes("Uvicorn running on")) {
// processes.backend.ready = true;
// checkServersReadyThenMain()
// };
console.log("[BACKEND]: ", data.toString())
});
backendProcess.stderr?.on("data", (data) =>
console.error("[BACKEND]: ", data.toString()),
);

// frontend
try {
const frontendPortAvailable = await isPortAvailable(DEFAULT_PORTS.frontend);
const frontendPortAvailable = await isPortAvailable(processes.frontend.port);
if (!frontendPortAvailable) {
frontendPort = await findAvailablePort(
processes.frontend.port = await findAvailablePort(
portRange.startPort,
portRange.endPort,
);
}
} catch (error) {
console.error("Error checking Next port: ", error);
console.error("Error checking Frontend port: ", error);
app.quit();
}

frontendProcess = exec(
`cross-env NEXT_PUBLIC_BACKEND_PORT=${backendPort} yarn dev:frontend --port=${frontendPort}`,
const frontendProcess = exec(
`cross-env NEXT_PUBLIC_BACKEND_PORT=${processes.backend.port} yarn dev:frontend --port=${processes.frontend.port}`,
);
processList.push(frontendProcess);
frontendProcess.stdout.on("data", (data) =>
console.log("[FRONTEND]: ", data.toString()),
frontendProcess.stdout?.on("data", (data) => {
// if (data.toString().includes("Ready in")) {
// processes.frontend.ready = true;
// checkServersReadyThenMain()
// };
console.log("[FRONTEND]: ", data.toString())
}
);
frontendProcess.stderr.on("data", (data) =>
frontendProcess.stderr?.on("data", (data) =>
console.error("[FRONTEND]: ", data.toString()),
);

// hardhat
try {
const hardhatPortAvailable = await isPortAvailable(DEFAULT_PORTS.hardhat);
const hardhatPortAvailable = await isPortAvailable(processes.hardhat.port);
if (!hardhatPortAvailable) {
hardhatPort = await findAvailablePort(
processes.hardhat.port = await findAvailablePort(
portRange.startPort,
portRange.endPort,
);
Expand All @@ -86,25 +98,20 @@ const launchProcesses = async () => {
app.quit();
}

hardhatProcess = exec(
`yarn dev:hardhat --port ${hardhatPort}`,
const hardhatProcess = exec(
`yarn dev:hardhat --port ${processes.hardhat.port}`,
);
processList.push(hardhatProcess);
hardhatProcess.stdout.on("data", (data) =>
console.log("[HARDHAT]: ", data.toString()),
);
hardhatProcess.stderr.on("data", (data) =>
hardhatProcess.stdout?.on("data", (data) => {
// if (data.toString().includes("Started HTTP and WebSocket JSON-RPC server at")) {
// processes.hardhat.ready = true;
// checkServersReadyThenMain();
// }
console.log("[HARDHAT]: ", data.toString())
});
hardhatProcess.stderr?.on("data", (data) =>
console.error("[HARDHAT]: ", data.toString()),
);

return {
backendProcess,
frontendProcess,
hardhatProcess,
backendPort,
frontendPort,
hardhatPort,
};
};

const createSplashWindow = () => {
Expand All @@ -115,10 +122,10 @@ const createSplashWindow = () => {
show: false,
title: "Olas Operate",
});
splashWindow.loadURL("file://" + __dirname + "/loading.html").then(()=>splashWindow.show());
splashWindow.loadURL("file://" + __dirname + "/loading.html").then();
};

const createMainWindow = (frontendPort) => {
const createMainWindow = () => {
mainWindow = new BrowserWindow({
width: 856,
height: 1321,
Expand All @@ -134,7 +141,7 @@ const createMainWindow = (frontendPort) => {
});

mainWindow.setMenuBarVisibility(false);
mainWindow.loadURL(`http://localhost:${frontendPort}`);
mainWindow.loadURL(`http://localhost:${processes.frontend.port}`);

mainWindow.webContents.openDevTools();

Expand All @@ -153,11 +160,8 @@ const createMainWindow = (frontendPort) => {
});

mainWindow.on("close", function (event) {
if (!app.isQuiting) {
event.preventDefault();
mainWindow.hide();
}
return false;
});
};

Expand All @@ -176,8 +180,8 @@ process.on("SIGINT", () => {

app.on("ready", async () => {
createSplashWindow();
const { frontendPort } = await launchProcesses();
createMainWindow(frontendPort);
await launchProcesses();
createMainWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createMainWindow();
Expand All @@ -192,12 +196,11 @@ app.on("window-all-closed", () => {
});

app.on("before-quit", () => {
app.isQuiting = true;
killAllProcesses();
});

app.whenReady().then(() => {
tray = new Tray(path.join(__dirname, "icons/robot-head.png"));
tray = new Tray(path.join(__dirname, "assets/icons/robot-head.png"));
const contextMenu = Menu.buildFromTemplate([
{
label: "Show App",
Expand Down

0 comments on commit c9c8226

Please sign in to comment.