Uses: Next JS static build
This commit is contained in:
parent
dccf169c37
commit
41b3b7df36
4 changed files with 24 additions and 70 deletions
33
app/main.ts
33
app/main.ts
|
|
@ -1,10 +1,10 @@
|
|||
require("dotenv").config();
|
||||
import { app, BrowserWindow } from "electron";
|
||||
import path from "path";
|
||||
import { findTwoUnusedPorts, killProcess, setupEnv, setUserConfig } from "./utils";
|
||||
import { startFastApiServer, startNextJsServer } from "./servers";
|
||||
import { findUnusedPorts, killProcess, setupEnv, setUserConfig } from "./utils";
|
||||
import { startFastApiServer } from "./utils/servers";
|
||||
import { ChildProcessByStdio } from "child_process";
|
||||
import { baseDir, fastapiDir, isDev, localhost, nextjsDir, tempDir, userConfigPath, userDataDir } from "./utils/constants";
|
||||
import { baseDir, fastapiDir, isDev, tempDir, userConfigPath, userDataDir } from "./utils/constants";
|
||||
import { setupIpcHandlers } from "./ipc";
|
||||
|
||||
var win: BrowserWindow | undefined;
|
||||
|
|
@ -18,12 +18,12 @@ const createWindow = () => {
|
|||
icon: path.join(baseDir, "resources/ui/assets/images/presenton_short_filled.png"),
|
||||
webPreferences: {
|
||||
webSecurity: false,
|
||||
preload: path.join(__dirname, 'preload.js'),
|
||||
preload: path.join(__dirname, 'preloads/index.js'),
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
async function startServers(fastApiPort: number, nextjsPort: number) {
|
||||
async function startServers(fastApiPort: number) {
|
||||
try {
|
||||
fastApiProcess = await startFastApiServer(
|
||||
fastapiDir,
|
||||
|
|
@ -40,17 +40,6 @@ async function startServers(fastApiPort: number, nextjsPort: number) {
|
|||
},
|
||||
isDev,
|
||||
);
|
||||
nextjsProcess = await startNextJsServer(
|
||||
nextjsDir,
|
||||
nextjsPort,
|
||||
{
|
||||
NEXT_PUBLIC_FAST_API: process.env.NEXT_PUBLIC_FAST_API,
|
||||
TEMP_DIRECTORY: process.env.TEMP_DIRECTORY,
|
||||
NEXT_PUBLIC_URL: process.env.NEXT_PUBLIC_URL,
|
||||
NEXT_PUBLIC_USER_CONFIG_PATH: process.env.NEXT_PUBLIC_USER_CONFIG_PATH,
|
||||
},
|
||||
isDev,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Server startup error:", error);
|
||||
}
|
||||
|
|
@ -76,15 +65,17 @@ app.whenReady().then(async () => {
|
|||
GOOGLE_API_KEY: process.env.GOOGLE_API_KEY,
|
||||
})
|
||||
|
||||
const [fastApiPort, nextjsPort] = await findTwoUnusedPorts();
|
||||
console.log(`FastAPI port: ${fastApiPort}, NextJS port: ${nextjsPort}`);
|
||||
const [fastApiPort] = await findUnusedPorts();
|
||||
console.log(`FastAPI port: ${fastApiPort}`);
|
||||
|
||||
//? Setup environment variables to be used in the preload.ts file
|
||||
setupEnv(fastApiPort, nextjsPort);
|
||||
setupEnv(fastApiPort);
|
||||
setupIpcHandlers();
|
||||
|
||||
await startServers(fastApiPort, nextjsPort);
|
||||
win?.loadURL(`${localhost}:${nextjsPort}`);
|
||||
await startServers(fastApiPort);
|
||||
|
||||
//? Load the NextJS UI
|
||||
win?.loadFile(path.join(baseDir, "resources/nextjs/index.html"));
|
||||
});
|
||||
|
||||
app.on("window-all-closed", async () => {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { app } from 'electron';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { userDataDir } from '../utils/constants';
|
||||
|
||||
|
||||
class SettingsStore {
|
||||
|
|
@ -8,7 +8,7 @@ class SettingsStore {
|
|||
private settings: { [key: string]: any };
|
||||
|
||||
constructor() {
|
||||
this.settingsPath = path.join(app.getPath('userData'), 'settings.json');
|
||||
this.settingsPath = path.join(userDataDir, 'settings.json');
|
||||
this.settings = {};
|
||||
this.loadSettings();
|
||||
}
|
||||
|
|
@ -18,11 +18,11 @@ class SettingsStore {
|
|||
if (fs.existsSync(this.settingsPath)) {
|
||||
const data = fs.readFileSync(this.settingsPath, 'utf-8');
|
||||
this.settings = JSON.parse(data);
|
||||
|
||||
|
||||
} else {
|
||||
this.settings = {};
|
||||
this.saveSettings();
|
||||
|
||||
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error loading settings:', error);
|
||||
|
|
@ -33,7 +33,7 @@ class SettingsStore {
|
|||
private saveSettings() {
|
||||
try {
|
||||
fs.writeFileSync(this.settingsPath, JSON.stringify(this.settings, null, 2));
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error saving settings:', error);
|
||||
throw error;
|
||||
|
|
@ -42,12 +42,12 @@ class SettingsStore {
|
|||
|
||||
get(key: string, defaultValue: any = null): any {
|
||||
const value = this.settings[key];
|
||||
|
||||
|
||||
return value || defaultValue;
|
||||
}
|
||||
|
||||
set(key: string, value: any): void {
|
||||
|
||||
|
||||
this.settings[key] = value;
|
||||
this.saveSettings();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,9 +27,8 @@ export function getUserConfig(): UserConfig {
|
|||
return JSON.parse(configData)
|
||||
}
|
||||
|
||||
export function setupEnv(fastApiPort: number, nextjsPort: number) {
|
||||
export function setupEnv(fastApiPort: number) {
|
||||
process.env.NEXT_PUBLIC_FAST_API = `${localhost}:${fastApiPort}`;
|
||||
process.env.NEXT_PUBLIC_URL = `${localhost}:${nextjsPort}`;
|
||||
process.env.TEMP_DIRECTORY = tempDir;
|
||||
process.env.NEXT_PUBLIC_USER_CONFIG_PATH = userConfigPath;
|
||||
}
|
||||
|
|
@ -49,7 +48,7 @@ export function killProcess(pid: number) {
|
|||
})
|
||||
}
|
||||
|
||||
export async function findTwoUnusedPorts(startPort: number = 40000): Promise<[number, number]> {
|
||||
export async function findUnusedPorts(startPort: number = 40000, count: number = 1): Promise<number[]> {
|
||||
const ports: number[] = [];
|
||||
|
||||
const isPortAvailable = (port: number): Promise<boolean> => {
|
||||
|
|
@ -67,13 +66,13 @@ export async function findTwoUnusedPorts(startPort: number = 40000): Promise<[nu
|
|||
};
|
||||
|
||||
let currentPort = startPort;
|
||||
while (ports.length < 2) {
|
||||
while (ports.length < count) {
|
||||
if (await isPortAvailable(currentPort)) {
|
||||
ports.push(currentPort);
|
||||
}
|
||||
currentPort++;
|
||||
}
|
||||
|
||||
return [ports[0], ports[1]];
|
||||
return ports;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -37,40 +37,4 @@ export async function startFastApiServer(
|
|||
// Wait for FastAPI server to start
|
||||
await execAsync(`npx wait-on ${localhost}:${port}/docs`);
|
||||
return fastApiProcess;
|
||||
}
|
||||
|
||||
export async function startNextJsServer(
|
||||
directory: string,
|
||||
port: number,
|
||||
env: NextJsEnv,
|
||||
isDev: boolean,
|
||||
) {
|
||||
// Start NextJS server
|
||||
const startCommand = isDev ? [
|
||||
"npm",
|
||||
["run", "dev", "--", "-p", port.toString()],
|
||||
] : [
|
||||
"npx",
|
||||
["serve@latest", "out", "-p", port.toString()],
|
||||
];
|
||||
|
||||
|
||||
const nextjsProcess = spawn(
|
||||
startCommand[0] as string,
|
||||
startCommand[1] as string[],
|
||||
{
|
||||
cwd: directory,
|
||||
stdio: ["inherit", "pipe", "pipe"],
|
||||
env: { ...process.env, ...env },
|
||||
}
|
||||
);
|
||||
nextjsProcess.stdout.on("data", (data: any) => {
|
||||
console.log(`NextJS: ${data}`);
|
||||
});
|
||||
nextjsProcess.stderr.on("data", (data: any) => {
|
||||
console.error(`NextJS Error: ${data}`);
|
||||
});
|
||||
// Wait for NextJS server to start
|
||||
await execAsync(`npx wait-on ${localhost}:${port}`);
|
||||
return nextjsProcess;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue