98 lines
3 KiB
TypeScript
Executable file
98 lines
3 KiB
TypeScript
Executable file
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { app, ipcMain } from "electron";
|
|
import log from "electron-log/main";
|
|
import serve from "electron-serve";
|
|
import BrowserWinHandler from "./BrowserWinHandler";
|
|
import UnzipData from "./UnzipData";
|
|
|
|
|
|
// The built directory structure
|
|
//
|
|
// ├─┬ dist-electron
|
|
// │ ├─┬ main
|
|
// │ │ └── index.js
|
|
// │ ├─┬ preload
|
|
// │ │ └── index.js
|
|
// │ ├─┬ renderer
|
|
// │ │ └── index.html
|
|
|
|
process.env.ROOT = path.join(__dirname, "..");
|
|
process.env.DIST = path.join(process.env.ROOT, "dist-electron");
|
|
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = "true";
|
|
|
|
// TODO: take inspiration form https://github.com/michalzaq12/electron-nuxt/tree/master/template/src/main.
|
|
// Or https://github.com/EternalC0der/electron-nuxt3
|
|
// Or https://github.com/caoxiemeihao/nuxt-electron/tree/main
|
|
|
|
const packageJson = JSON.parse(fs.readFileSync(
|
|
path.join(__dirname, "../package.json"),
|
|
{ encoding: "utf8", flag: "r" })
|
|
);
|
|
|
|
export const appInfo = {
|
|
version: String(packageJson.version),
|
|
name: String(packageJson.name),
|
|
licence: String(packageJson.licence)
|
|
};
|
|
|
|
log.initialize();
|
|
|
|
// ** Close Part
|
|
const handleProcessExit = () => {
|
|
app.exit(0);
|
|
process.exit(0);
|
|
};
|
|
process.on("SIGINT", handleProcessExit);
|
|
process.on("SIGQUIT", handleProcessExit);
|
|
process.on("SIGTERM", handleProcessExit);
|
|
|
|
// Quit when all windows are closed.
|
|
app.on("window-all-closed", function () {
|
|
// On macOS it is common for applications and their menu bar
|
|
// to stay active until the user quits explicitly with Cmd + Q
|
|
if (process.platform !== "darwin") handleProcessExit();
|
|
});
|
|
|
|
app.once("browser-window-created", (_, browserWindow) => {
|
|
browserWindow.webContents.once("did-frame-finish-load", () => {
|
|
browserWindow.webContents.openDevTools();
|
|
});
|
|
});
|
|
|
|
const isDev = process.env.NODE_ENV === "development";
|
|
const root = path.join(__dirname, "..");
|
|
const publicPath = isDev ? path.join(root, "public") : path.join(root, ".output/public");
|
|
const loadURL = serve({ directory: publicPath });
|
|
|
|
app.whenReady().then(() => {
|
|
// * Run an Electron window ?
|
|
const winHandler = new BrowserWinHandler({
|
|
// width: 1280,
|
|
// height: 900,
|
|
center: true,
|
|
kiosk: process.env.NODE_ENV !== "development",
|
|
closable: false,
|
|
alwaysOnTop: true,
|
|
fullscreen: process.env.NODE_ENV !== "development",
|
|
autoHideMenuBar: true,
|
|
webPreferences: {
|
|
preload: path.join(__dirname, "preload.js"),
|
|
devTools: process.env.NODE_ENV === "development",
|
|
}
|
|
});
|
|
|
|
winHandler.onCreated(_browserWindow => {
|
|
try {
|
|
const appData = UnzipData.loadJsonDataFile();
|
|
|
|
winHandler.loadMainPage(loadURL);
|
|
ipcMain.handle("appInfo", () => appInfo);
|
|
ipcMain.handle("channel", () => appData);
|
|
winHandler.openDevTool();
|
|
} catch (err) {
|
|
log.error(err);
|
|
winHandler.quit();
|
|
}
|
|
});
|
|
});
|