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
+
3
5
import { app , BrowserWindow } from "electron" ;
4
- import { access , writeFile , mkdir , readFile } from ' fs/promises' ;
6
+ import { access , writeFile , mkdir , readFile } from " fs/promises" ;
5
7
import path from "path" ;
6
8
7
-
8
9
const nameAPI = "fileSystem" ;
9
10
10
11
// to Main
11
12
const validSendChannel : SendChannels = {
12
- " readFile" : readFileTodos ,
13
- " saveFile" : saveFile
13
+ readFile : readFileTodos ,
14
+ saveFile : saveFile ,
14
15
} ;
15
16
16
17
// from Main
17
- const validReceiveChannel : string [ ] = [
18
- "getFile"
19
- ] ;
18
+ const validReceiveChannel : string [ ] = [ "getFile" ] ;
20
19
21
- const fileSystem = new IPC ( {
22
- nameAPI,
23
- validSendChannel,
24
- validReceiveChannel
20
+ const fileSystem = new IPC ( {
21
+ nameAPI,
22
+ validSendChannel,
23
+ validReceiveChannel,
25
24
} ) ;
26
25
27
26
export default fileSystem ;
28
27
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 ) ;
38
41
}
39
42
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 ) ;
50
57
}
51
58
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
+ }
63
70
}
64
71
65
72
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
+ }
74
81
}
75
82
76
83
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 ) {
80
98
try {
81
- await access ( pathDir ) ;
82
- dirExists = true ;
99
+ await mkdir ( pathDir ) ;
83
100
} 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 ) ;
97
103
}
104
+ }
98
105
}
99
106
100
-
101
107
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
+ }
0 commit comments