Skip to content

Commit

Permalink
feat: make the app work inside of electron
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminshafii committed May 17, 2024
1 parent 1545a93 commit eed1849
Show file tree
Hide file tree
Showing 9 changed files with 3,116 additions and 3,758 deletions.
10 changes: 5 additions & 5 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"sourceType": "module"
},
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", { "args": "none" }],
"@typescript-eslint/ban-ts-comment": "off",
"no-prototype-builtins": "off",
"@typescript-eslint/no-empty-function": "off"
"no-unused-vars": "warn",
"@typescript-eslint/no-unused-vars": ["warn", { "args": "none" }],
"@typescript-eslint/ban-ts-comment": "warn",
"no-prototype-builtins": "warn",
"@typescript-eslint/no-empty-function": "warn"
}
}
5 changes: 5 additions & 0 deletions app/.electronignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
dist
test
*.log
*.md
86 changes: 86 additions & 0 deletions app/desktop/electron.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const { app, BrowserWindow, dialog, ipcMain } = require('electron');
const path = require('path');
const fs = require('fs');
const { createServer } = require('http');
const next = require('next');

let mainWindow;
const nextApp = next({ dev: process.env.NODE_ENV !== 'production', dir: path.join(__dirname, '../') });
const nextHandler = nextApp.getRequestHandler();

async function createWindow() {
const isDev = (await import('electron-is-dev')).default;
await nextApp.prepare();

mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, './preload.js'),
contextIsolation: true, // Ensure contextIsolation is true
enableRemoteModule: false, // Disable remote module for security
},
});

const server = createServer((req, res) => {
nextHandler(req, res);
});

let port = process.env.PORT || 3000;
server.listen(port, () => {
const url = `http://localhost:${port}`;
mainWindow.loadURL(url);
}).on('error', (err) => {
if (err.code === 'EADDRINUSE') {
port += 1;
server.listen(port, () => {
const url = `http://localhost:${port}`;
mainWindow.loadURL(url);
});
} else {
throw err;
}
});

if (isDev) {
mainWindow.webContents.openDevTools();
}

mainWindow.on('closed', () => {
mainWindow = null;
});
}

app.on('ready', createWindow);

ipcMain.handle('select-folder', async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openDirectory'],
});

if (result.canceled) {
return null;
} else {
return result.filePaths[0];
}
});

ipcMain.on('watch-folder', (event, folderPath) => {
fs.watch(folderPath, (eventType, filename) => {
if (eventType === 'rename' && filename) {
event.sender.send('new-file', filename);
}
});
});

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});

app.on('activate', () => {
if (mainWindow === null) {
createWindow();
}
});
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit eed1849

Please sign in to comment.