This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
55 lines (51 loc) · 1.42 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//handle setupevents as quickly as possible
const setupEvents = require('./installers/windows/setupEvents')
if (setupEvents.handleSquirrelEvent()) {
// squirrel event handled and app will exit in 1000ms, so don't do anything else
return;
}
const { app, BrowserWindow, ipcMain } = require("electron");
const path = require("path");
const fs = require("fs");
const ipc = ipcMain
const userPath = path.join(app.getPath("userData"), "save.json");
function requestSave() {
let data = []
try {
data = JSON.parse(fs.readFileSync(userPath))
} catch(error) {
data = []
} finally {
//console.log(data);
fs.writeFileSync(userPath, JSON.stringify(data))
}
return data
}
function saveData(data) {
//console.log(data);
fs.writeFileSync(userPath, JSON.stringify(data.data))
//console.log("Data Written");
}
app.on("ready", () => {
ipc.on("requestSave", (event, message) => {
//console.log("got an IPC message", message);
const data = requestSave();
event.reply("data", data);
})
ipc.on("saveData", (event, message) => {
saveData(message);
});
//console.log(app.getPath("userData"));
const mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, './preload.js'),
nodeIntegration: false,
contextIsolation: true
}
});
mainWindow.loadFile(path.join(__dirname, "public/index.html"));
//mainWindow.webContents.openDevTools();
});
app.on("window-all-closed", () => {
app.quit();
});