Skip to content

Commit

Permalink
Add electron build script
Browse files Browse the repository at this point in the history
  • Loading branch information
spaaaacccee committed Sep 21, 2023
1 parent e626952 commit ff09484
Show file tree
Hide file tree
Showing 7 changed files with 1,890 additions and 61 deletions.
1 change: 1 addition & 0 deletions client/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# production
/build
/bin
/dist

# misc
Expand Down
71 changes: 71 additions & 0 deletions client/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const electron = require("electron");
const fs = require("fs");
const http = require("http");
const path = require("path");
const url = require("url");

const map = {
".css": "text/css",
".html": "text/html",
".ico": "image/x-icon",
".jpg": "image/jpeg",
".js": "text/javascript",
".json": "application/json",
".png": "image/png",
".svg": "image/svg+xml",
};

const server = http.createServer((req, res) => {
// extract URL path
let pathname = `./${
url.parse(
path.join(
electron.app.isPackaged ? "resources/app/dist" : "dist",
req.url
)
).pathname
}`;
// based on the URL path, extract the file extension. e.g. .js, .doc, ...
const ext = path.parse(pathname).ext;

if (fs.existsSync(pathname)) {
// if is a directory search for index file matching the extension
if (fs.statSync(pathname).isDirectory()) pathname += "/index" + ext;
try {
const data = fs.readFileSync(pathname);
// if the file is found, set Content-type and send data
res.setHeader("Content-type", map[ext] || "text/plain");
res.end(data);
} catch (e) {
res.statusCode = 500;
res.end(`Error getting the file: ${e}.`);
}
} else {
// if the file is not found, return 404
res.statusCode = 404;
res.end(`File ${pathname} not found!`);
}
});
server.listen(0, () => {
const port = server.address().port;
console.log("Listening on port:", port);
const createWindow = () => {
const win = new electron.BrowserWindow({
autoHideMenuBar: true,
center: true,
show: false,
});
win.loadURL(`http://localhost:${port}/index.html`);
win.maximize();
win.show();
win.webContents.on("did-finish-load", () => {
win.webContents.setZoomFactor(0.9);
});
};
electron.app.whenReady().then(() => {
createWindow();
});
electron.app.on("window-all-closed", () => {
electron.app.quit();
});
});
Loading

0 comments on commit ff09484

Please sign in to comment.