Skip to content

Commit 9f1682c

Browse files
authored
Merge pull request #14 from el3um4s/test-udpate
Updated Electron to v20
2 parents 333de8a + e55f54f commit 9f1682c

File tree

11 files changed

+1621
-2577
lines changed

11 files changed

+1621
-2577
lines changed

electron/IPC/fileSystem.ts

+88-82
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,119 @@
1-
import { SendChannels } from "./General/channelsInterface";
2-
import IPC from "./General/IPC";
1+
// import { SendChannels } from "./General/channelsInterface";
2+
// import IPC from "./General/IPC";
3+
import { IPC, SendChannels } from "@el3um4s/ipc-for-electron";
4+
35
import { app, BrowserWindow } from "electron";
4-
import { access, writeFile, mkdir, readFile } from 'fs/promises';
6+
import { access, writeFile, mkdir, readFile } from "fs/promises";
57
import path from "path";
68

7-
89
const nameAPI = "fileSystem";
910

1011
// to Main
1112
const validSendChannel: SendChannels = {
12-
"readFile": readFileTodos,
13-
"saveFile": saveFile
13+
readFile: readFileTodos,
14+
saveFile: saveFile,
1415
};
1516

1617
// from Main
17-
const validReceiveChannel: string[] = [
18-
"getFile"
19-
];
18+
const validReceiveChannel: string[] = ["getFile"];
2019

21-
const fileSystem = new IPC ({
22-
nameAPI,
23-
validSendChannel,
24-
validReceiveChannel
20+
const fileSystem = new IPC({
21+
nameAPI,
22+
validSendChannel,
23+
validReceiveChannel,
2524
});
2625

2726
export default fileSystem;
2827

29-
async function readFileTodos(mainWindow: BrowserWindow, event: Electron.IpcMainEvent, fileName: any) {
30-
const fileExists = await checkFileTodosExists(fileName);
31-
if (!fileExists) {
32-
await createDir();
33-
const data = `[]`;
34-
await writeTodos(fileName, data);
35-
}
36-
const todos = await loadTodos(fileName);
37-
mainWindow.webContents.send("getFile", todos);
28+
async function readFileTodos(
29+
mainWindow: BrowserWindow,
30+
event: Electron.IpcMainEvent,
31+
fileName: any
32+
) {
33+
const fileExists = await checkFileTodosExists(fileName);
34+
if (!fileExists) {
35+
await createDir();
36+
const data = `[]`;
37+
await writeTodos(fileName, data);
38+
}
39+
const todos = await loadTodos(fileName);
40+
mainWindow.webContents.send("getFile", todos);
3841
}
3942

40-
async function saveFile(mainWindow: BrowserWindow, event: Electron.IpcMainEvent, data: { fileName: string, todo: string }) {
41-
const { fileName, todo } = {...data};
42-
console.log(fileName);
43-
console.log(todo);
44-
45-
const fileExists = await checkFileTodosExists(fileName);
46-
if (!fileExists) {
47-
await createDir();
48-
}
49-
await writeTodos(fileName, todo);
43+
async function saveFile(
44+
mainWindow: BrowserWindow,
45+
event: Electron.IpcMainEvent,
46+
data: { fileName: string; todo: string }
47+
) {
48+
const { fileName, todo } = { ...data };
49+
console.log(fileName);
50+
console.log(todo);
51+
52+
const fileExists = await checkFileTodosExists(fileName);
53+
if (!fileExists) {
54+
await createDir();
55+
}
56+
await writeTodos(fileName, todo);
5057
}
5158

52-
async function checkFileTodosExists(fileName:string) {
53-
const userData = app.getPath('userData');
54-
const pathFile = path.join(userData, 'todos' ,fileName);
55-
try {
56-
await access(pathFile);
57-
return true;
58-
} catch (error) {
59-
console.log("DOES NOT exist:", pathFile);
60-
console.error(error);
61-
return false;
62-
}
59+
async function checkFileTodosExists(fileName: string) {
60+
const userData = app.getPath("userData");
61+
const pathFile = path.join(userData, "todos", fileName);
62+
try {
63+
await access(pathFile);
64+
return true;
65+
} catch (error) {
66+
console.log("DOES NOT exist:", pathFile);
67+
console.error(error);
68+
return false;
69+
}
6370
}
6471

6572
async function writeTodos(fileName: string, data: string) {
66-
const userData = app.getPath('userData');
67-
const pathFile = path.join(userData, 'todos' ,fileName);
68-
try {
69-
await writeFile(pathFile, data);
70-
} catch (error) {
71-
console.log("await writeFile(pathFile, data);", pathFile);
72-
console.error(error);
73-
}
73+
const userData = app.getPath("userData");
74+
const pathFile = path.join(userData, "todos", fileName);
75+
try {
76+
await writeFile(pathFile, data);
77+
} catch (error) {
78+
console.log("await writeFile(pathFile, data);", pathFile);
79+
console.error(error);
80+
}
7481
}
7582

7683
async function createDir() {
77-
const userData = app.getPath('userData');
78-
const pathDir = path.join(userData, 'todos');
79-
let dirExists = false;
84+
const userData = app.getPath("userData");
85+
const pathDir = path.join(userData, "todos");
86+
let dirExists = false;
87+
try {
88+
await access(pathDir);
89+
dirExists = true;
90+
} catch (error) {
91+
// The check failed
92+
console.log("DOES NOT exist:", pathDir);
93+
console.error(error);
94+
dirExists = false;
95+
}
96+
97+
if (!dirExists) {
8098
try {
81-
await access(pathDir);
82-
dirExists = true;
99+
await mkdir(pathDir);
83100
} catch (error) {
84-
// The check failed
85-
console.log("DOES NOT exist:", pathDir);
86-
console.error(error);
87-
dirExists = false;
88-
}
89-
90-
if (!dirExists) {
91-
try {
92-
await mkdir(pathDir);
93-
} catch (error) {
94-
console.log(" await mkdir(pathDir);", pathDir);
95-
console.error(error);
96-
}
101+
console.log(" await mkdir(pathDir);", pathDir);
102+
console.error(error);
97103
}
104+
}
98105
}
99106

100-
101107
async function loadTodos(fileName: string) {
102-
const userData = app.getPath('userData');
103-
const pathDir = path.join(userData, 'todos', fileName);
104-
let result = [];
105-
try {
106-
const rawData = await readFile(pathDir, 'utf-8');
107-
result = JSON.parse(rawData);
108-
} catch (err) {
109-
console.error(err);
110-
}
111-
112-
return result;
113-
}
108+
const userData = app.getPath("userData");
109+
const pathDir = path.join(userData, "todos", fileName);
110+
let result = [];
111+
try {
112+
const rawData = await readFile(pathDir, "utf-8");
113+
result = JSON.parse(rawData);
114+
} catch (err) {
115+
console.error(err);
116+
}
117+
118+
return result;
119+
}

electron/IPC/systemInfo.ts

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
1-
import { SendChannels } from "./General/channelsInterface";
2-
import IPC from "./General/IPC";
1+
// import { SendChannels } from "./General/channelsInterface";
2+
// import IPC from "./General/IPC";
3+
import { IPC, SendChannels } from "@el3um4s/ipc-for-electron";
34
import { BrowserWindow } from "electron";
45

56
const nameAPI = "systemInfo";
67

78
// to Main
89
const validSendChannel: SendChannels = {
9-
"requestSystemInfo": requestSystemInfo
10+
requestSystemInfo: requestSystemInfo,
1011
};
1112

1213
// from Main
13-
const validReceiveChannel: string[] = [
14-
"getSystemInfo",
15-
];
14+
const validReceiveChannel: string[] = ["getSystemInfo"];
1615

17-
const systemInfo = new IPC ({
18-
nameAPI,
19-
validSendChannel,
20-
validReceiveChannel
16+
const systemInfo = new IPC({
17+
nameAPI,
18+
validSendChannel,
19+
validReceiveChannel,
2120
});
2221

2322
export default systemInfo;
2423

2524
// Enter here the functions for ElectronJS
2625

27-
function requestSystemInfo(mainWindow: BrowserWindow, event: Electron.IpcMainEvent, message: any) {
28-
const versionChrome = process.versions.chrome;
29-
const versionNode = process.versions.node;
30-
const versionElectron = process.versions.electron;
31-
const result = {
32-
chrome: versionChrome,
33-
node: versionNode,
34-
electron: versionElectron
35-
}
36-
mainWindow.webContents.send("getSystemInfo", result);
26+
function requestSystemInfo(
27+
mainWindow: BrowserWindow,
28+
event: Electron.IpcMainEvent,
29+
message: any
30+
) {
31+
const versionChrome = process.versions.chrome;
32+
const versionNode = process.versions.node;
33+
const versionElectron = process.versions.electron;
34+
const result = {
35+
chrome: versionChrome,
36+
node: versionNode,
37+
electron: versionElectron,
38+
};
39+
mainWindow.webContents.send("getSystemInfo", result);
3740
}
38-

0 commit comments

Comments
 (0)