-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
98 lines (85 loc) · 2.73 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const { app, BrowserWindow, ipcMain, remote } = require("electron");
const path = require("path");
const fs = require("fs");
const psList = require("ps-list");
function createWindow() {
// Create the browser window.
const win = new BrowserWindow({
width: 800,
height: 900,
titleBarStyle: "hiddenInset",
webPreferences: {
preload: __dirname + "/preload.js",
},
icon: "tokspace.png",
title: "TokSpace",
});
// and load the index.html of the app.
win.loadURL("http://localhost:3000");
}
app.whenReady().then(createWindow);
// Listen for the front-end web app to send a request that asks for what
// processes are running on the user's machine.
ipcMain.handle("processesRequest", async () => psList());
class Store {
// opts is an object with the following structure:
//
// {
// // The name of the file on local storage where user configs are stored.
// configName: string,
//
// // Default user configs. To be used if something goes wrong reading the
// // local file on disk with these configs.
// defaults: {
// // Process names that the user has configred to set their
// // availability status to "away" when running.
// storedProcesses: [],
// },
// }
constructor(opts) {
// Renderer process has to get `app` module via `remote`, whereas the
// main process can get it directly.
const userDataPath = (app || remote.app).getPath("userData");
this.path = path.join(userDataPath, opts.configName + ".json");
console.log(this.path);
this.data = parseDataFile(this.path, opts.defaults);
}
get(key) {
return this.data[key];
}
set(key, val) {
this.data[key] = val;
try {
fs.writeFileSync(this.path, JSON.stringify(this.data));
} catch (err) {
// TODO: better error handling.
console.error(err);
}
}
}
function parseDataFile(filePath, defaults) {
try {
return JSON.parse(fs.readFileSync(filePath));
} catch (error) {
return defaults;
}
}
const store = new Store({
configName: "user-configs",
defaults: {
storedProcesses: [],
},
});
// Listen for the front-end web app to send a request that asks for what
// processes the user's already configured in the settings page.
ipcMain.handle("configuredProcessesRequest", () =>
store.get("storedProcesses"),
);
// Listen for the front-end web app to push up new configured processes that the
// user has configured.
ipcMain.handle(
"saveNewConfiguredProcesses",
(_event, newConfiguredProcesses) => {
store.set("storedProcesses", newConfiguredProcesses);
},
);