-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
144 lines (123 loc) · 3.05 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
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
const { app, BrowserWindow, ipcMain } = require('electron')
const fs = require('fs');
let localStorage = __dirname +'/storage/';
let win;
let windows = [];
function createWindow() {
// Create the browser window.
win = new BrowserWindow({
width: 1080,
height: 600,
minWidth: 800,
minHeight: 500,
backgroundColor: '#ffffff',
frame: false
})
win.loadURL(`file://${__dirname}/www/index.html`)
//// uncomment below to open the DevTools.
win.webContents.openDevTools()
// Event when the window is closed.
win.on('closed', function () {
win = null
})
}
function initializeLocalFile(file) {
return new Promise((res, rej) => {
if (!fs.existsSync(file)) {
console.log(file);
fs.writeFile(file, '', 'utf8', (err) => {
if (err) {
rej(err);
} else {
res(true);
}
});
}
});
}
function initializeDirectory(){
if (!fs.existsSync(localStorage)){
fs.mkdirSync(localStorage);
}
}
ipcMain.on('open-window', (e, data) => {
let win = new BrowserWindow({
width: 800,
height: 450,
minWidth: 300,
minHeight: 200,
backgroundColor: '#ffffff',
frame: false
})
win.loadURL(`file://${__dirname}/www/index.html`)
//// uncomment below to open the DevTools.
win.webContents.openDevTools()
// Event when the window is closed.
win.on('closed', () => {
win = null;
});
windows.push(win);
});
ipcMain.on('local-store', (e, obj) => {
let fullFile = localStorage + obj.file;
initializeLocalFile(fullFile).then(() => {
fs.readFile(fullFile, 'utf8', (err, data) => {
if (err) {
console.log(err);
}
if (!data) {
let array = []
array.push(obj.data);
json = JSON.stringify(array);
fs.writeFile(fullFile, json, 'utf8', (callback) => {
e.sender.send('local-store-reply', array);
});
}
else {
let file = JSON.parse(data); //get full db file
let fLen = Object.keys(file).length;
file[fLen] = obj.data;
console.log(file);
json = JSON.stringify(file);
fs.writeFile(fullFile, json, 'utf8', (callback) => {
e.sender.send('local-store-reply', file);
});
}
});
});
});
ipcMain.on('local-get', (e, obj) => {
let fullFile = localStorage + obj.file;
initializeLocalFile(fullFile).then(() => {
fs.readFile(fullFile, 'utf8', (err, data) => {
if (err || !data) {
e.sender.send('local-get-reply', err);
}
else {
let reply = JSON.parse(data);
e.sender.send('local-get-reply', reply);
}
});
});
});
function toFile(file) {
let splitFile = file
}
// Create window on electron intialization
app.on('ready', () => {
createWindow();
initializeDirectory();
})
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On macOS specific close process
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// macOS specific close process
if (win === null) {
createWindow()
}
})