-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
258 lines (237 loc) · 6.24 KB
/
Copy pathmain.js
File metadata and controls
258 lines (237 loc) · 6.24 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"use strict";
const { app, BrowserWindow, ipcMain } = require("electron/main");
const path = require("path");
const si = require("systeminformation");
const { exec } = require("child_process");
const fs = require("fs");
const csv = require("csv-parser");
// creates csv data storage
(() => {
try {
const data = [
{
firstname: "firstname",
lastname: "lastname",
username: "username",
password: "password",
email: "email",
phone: "phone",
},
{
firstname: "John",
lastname: "Doe",
username: "Admin",
password: "Admin",
email: "john.doe@gmail.com",
phone: "+234 456 342 323234",
},
];
fs.access("./data/data.csv", fs.constants.F_OK, async (err) => {
if (err) {
// Convert data array to CSV string
const csvData = data
.map(
({ firstname, lastname, username, password, email, phone }) =>
`${firstname},${lastname},${username},${password},${email},${phone}`
)
.join("\n");
// Write CSV string to file
await new Promise((resolve, reject) =>
fs.writeFile("./data/data.csv", csvData, (err) => {
if (err) reject(err);
else {
console.log("CSV file has been saved.");
resolve(1);
}
})
);
}
});
} catch (err) {
console.error(err);
}
})();
app.whenReady().then(() => {
// creates browser window
(() => {
const mainWindow = new BrowserWindow({
width: 750,
height: 500,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, "./preload.js"),
},
});
// Use loadFile method to load the HTML file
mainWindow.loadURL(path.join(__dirname, "./renderer/index.html"));
// handles naviation
ipcMain.on("load-next-page", (event, data) => {
mainWindow.loadURL(path.join(__dirname, `./renderer/${data}.html`));
});
})();
// updates user's profile
ipcMain.on("updateCSV", async (e, newData) => {
// Write CSV string to file
const csvData = newData
.map(
({ firstname, lastname, username, password, email, phone }) =>
`${firstname},${lastname},${username},${password},${email},${phone}`
)
.join("\n");
const resp = await new Promise((resolve, reject) =>
fs.writeFile("./data/data.csv", csvData, (er) => {
if (er)
reject({
success: false,
error: er.message,
});
else {
console.log("CSV file has been updated.");
resolve({ success: true });
}
})
);
e.sender.send("updateCSVResponse", resp);
});
// gets user's data
ipcMain.on("reqUserData", async (e) => {
const data = [];
const resp = await new Promise((resolve, reject) =>
fs
.createReadStream("./data/data.csv")
.pipe(csv())
.on("data", (e) => data.push(e))
.on("end", () => resolve(data))
.on("error", (er) => reject({ error: er }))
);
e.sender.send("respUserData", resp);
});
// gets system information
ipcMain.on("reqSystemInfo", async (e) => {
const resp = await si
.osInfo()
.then(({ arch, distro, hostname, kernel, platform, release, build }) => ({
systemInfo: {
arch,
distro,
hostname,
kernel,
platform,
release,
build,
},
}))
.catch((er) => ({ error: er.message }));
e.sender.send("respSystemInfo", resp);
});
// gets memory information
ipcMain.on("reqMemInfo", async (e) => {
const resp = await si
.mem()
.then(({ total, free, used, active, available, buffers, cached }) => ({
memInfo: {
total,
free,
used,
active,
available,
buffers,
cached,
},
}))
.catch((er) => ({ error: er.message }));
e.sender.send("respMemInfo", resp);
});
// gets graphics information
ipcMain.on("reqGraphicsInfo", async (e) => {
const resp = await si
.graphics()
.then(
({
displays: [
{
model,
connection,
resolutionX,
resolutionY,
currentResX,
currentResY,
vendor,
},
],
}) => ({
graphicsInfo: {
model,
connection,
resolutionX,
resolutionY,
currentResX,
currentResY,
vendor,
},
})
)
.catch((er) => ({ error: er.message }));
e.sender.send("respGraphicsInfo", resp);
});
// gets CPU information
ipcMain.on("reqCPUInfo", async (e) => {
const resp = await si
.cpu()
.then(
({
manufacturer,
brand,
vendor,
family,
speed,
cores,
processors,
}) => ({
prosInfo: {
manufacturer,
brand,
vendor,
family,
speed,
cores,
processors,
},
})
)
.catch((er) => ({ error: er.message }));
e.sender.send("respCPUInfo", resp);
});
// gets connected devices
ipcMain.on("reqConnDevice", async (event) => {
exec("arp -a", (er, stdout, stderr) => {
try {
if (er) throw er;
const lines = stdout.split("\n").map((line) => line.trim());
const data = [];
lines.slice(2).forEach((line) => {
const [IPAddress, physicalAddress, type] = line.split(/\s+/);
if (type?.toLowerCase() === "dynamic")
data.push({
IPAddress,
physicalAddress,
type,
});
});
event.sender.send("respConnDevice", {
networkInfo: data,
});
} catch (er) {
event.sender.send("respConnDevice", { error: { er, stderr } });
}
});
});
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") app.quit();
});