-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
175 lines (152 loc) · 3.31 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
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
const path = require('path');
const url = require('url');
const { app, BrowserWindow, ipcMain, Menu } = require('electron');
const connectDB = require('./config/db');
const Log = require('./models/Log');
//Connect to db
connectDB();
let mainWindow;
let isDev = false;
const isMac = process.platform === 'darwin';
if (
process.env.NODE_ENV !== undefined &&
process.env.NODE_ENV === 'development'
) {
isDev = true;
}
function createMainWindow() {
mainWindow = new BrowserWindow({
width: isDev ? 1400 : 1100,
height: 800,
show: false,
backgroundColor: '#fff',
icon: `${__dirname}/assets/icon.png`,
webPreferences: {
nodeIntegration: true,
},
});
let indexPath;
if (isDev && process.argv.indexOf('--noDevServer') === -1) {
indexPath = url.format({
protocol: 'http:',
host: 'localhost:8080',
pathname: 'index.html',
slashes: true,
});
} else {
indexPath = url.format({
protocol: 'file:',
pathname: path.join(__dirname, 'dist', 'index.html'),
slashes: true,
});
}
mainWindow.loadURL(indexPath);
// Don't show until we are ready and loaded
mainWindow.once('ready-to-show', () => {
mainWindow.show();
// Open devtools if dev
if (isDev) {
const {
default: installExtension,
REACT_DEVELOPER_TOOLS,
} = require('electron-devtools-installer');
installExtension(REACT_DEVELOPER_TOOLS).catch((err) =>
console.log('Error loading React DevTools: ', err)
);
mainWindow.webContents.openDevTools();
}
});
mainWindow.on('closed', () => (mainWindow = null));
}
app.on('ready', () => {
createMainWindow();
const mainMenu = Menu.buildFromTemplate(menu);
Menu.setApplicationMenu(mainMenu);
});
const menu = [
...(isMac
? [
{
role: 'appMenu',
},
]
: []),
{
role: 'fileMenu',
},
{
role: 'editMenu',
},
{
label: 'Logs',
submenu: [
{
label: 'Clear Logs',
click: () => clearLogs(),
},
],
},
...(isDev
? [
{
label: 'Developer',
submenu: [
{ role: 'reload' },
{ role: 'forcereload' },
{ type: 'separator' },
{ role: 'toggledevtools' },
],
},
]
: []),
];
//Load logs
ipcMain.on('logs:load', sendLogs);
//Create Log
ipcMain.on('logs:add', async (e, item) => {
try {
await Log.create(item);
sendLogs();
} catch (error) {
console.log(error);
}
});
//Delete Log
ipcMain.on('logs:delete', async (e, id) => {
try {
await Log.findOneAndDelete({ _id: id });
sendLogs();
} catch (error) {
console.log(error);
}
});
//Send Log Items
async function sendLogs() {
try {
const logs = await Log.find().sort({ created: 1 });
mainWindow.webContents.send('logs:get', JSON.stringify(logs));
} catch (error) {
console.log(error);
}
}
//Clear all logs
async function clearLogs() {
try {
await Log.deleteMany({});
mainWindow.webContents.send('logs:clear');
} catch (error) {
console.log(error);
}
}
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (mainWindow === null) {
createMainWindow();
}
});
// Stop error
app.allowRendererProcessReuse = true;