Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions electron/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { app, BrowserWindow, ipcMain, screen, shell, dialog, Tray, Menu, powerSaveBlocker } = require('electron');
const path = require('path');
const fs = require('fs').promises;
const { existsSync } = require('fs');

let mainWindow;
let tray;
Expand Down Expand Up @@ -121,13 +120,15 @@ ipcMain.on('save-log', (event, data, customPath) => {

let logs = {};
try {
if (existsSync(filePath)) {
const content = await fs.readFile(filePath, 'utf8');
if (content && content.trim()) logs = JSON.parse(content);
}
const content = await fs.readFile(filePath, 'utf8');
if (content && content.trim()) logs = JSON.parse(content);
} catch (e) {
console.error("Corrupted logs recovered:", e);
if (existsSync(filePath)) await fs.copyFile(filePath, `${filePath}.corrupt.bak`);
if (e.code !== 'ENOENT') {
console.error("Corrupted logs recovered:", e);
try {
await fs.copyFile(filePath, `${filePath}.corrupt.bak`);
} catch(err) {}
}
logs = {};
}

Expand All @@ -142,7 +143,7 @@ ipcMain.on('save-log', (event, data, customPath) => {

try {
const dir = path.dirname(filePath);
if (!existsSync(dir)) await fs.mkdir(dir, { recursive: true });
await fs.mkdir(dir, { recursive: true }).catch(() => {});

const tmpPath = `${filePath}.${Date.now()}.tmp`;
await fs.writeFile(tmpPath, JSON.stringify(logs, null, 2));
Expand All @@ -165,10 +166,8 @@ ipcMain.on('save-log', (event, data, customPath) => {
ipcMain.handle('get-logs', async (event, customPath) => {
const filePath = getLogPath(customPath);
try {
if (existsSync(filePath)) {
const content = await fs.readFile(filePath, 'utf8');
return JSON.parse(content);
}
const content = await fs.readFile(filePath, 'utf8');
return JSON.parse(content);
} catch (e) { }
return {};
});
Expand All @@ -179,7 +178,7 @@ ipcMain.handle('update-logs', async (event, updatedLogs, customPath) => {
const filePath = getLogPath(customPath);
try {
const dir = path.dirname(filePath);
if (!existsSync(dir)) await fs.mkdir(dir, { recursive: true });
await fs.mkdir(dir, { recursive: true }).catch(() => {});
const tmpPath = `${filePath}.${Date.now()}.tmp`;
await fs.writeFile(tmpPath, JSON.stringify(updatedLogs, null, 2));
await fs.rename(tmpPath, filePath);
Expand Down Expand Up @@ -248,7 +247,10 @@ ipcMain.on('force-restore', () => {
ipcMain.on('open-logs-folder', async (event, customPath) => {
const filePath = getLogPath(customPath);
const dirPath = path.dirname(filePath);
if (existsSync(dirPath)) shell.openPath(dirPath);
try {
await fs.access(dirPath);
shell.openPath(dirPath);
} catch (e) { }
});

ipcMain.on('set-enforcement', (event, config) => {
Expand Down