-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
120 lines (97 loc) · 3.04 KB
/
main.ts
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
import { app, BrowserWindow, ipcMain, shell } from 'electron';
import * as logger from 'electron-log';
import * as path from 'path'
import * as url from 'url'
let win: BrowserWindow;
// Command line arguments
const args = process.argv.slice(1);
const serve = args.some(val => val.includes('serve'));
const isTestnet = args.some(val => val.includes('testnet'));
const SERVE_URL = url.format({
pathname: path.join(__dirname, '../../dist/opdex-desktop/index.html'),
protocol: 'file:',
slashes: true
});
// Allow right click inspect element during local development
require('electron-context-menu')({
showInspectElement: serve
});
////////////////////////////////////
// APP State //
////////////////////////////////////
app.whenReady().then(() => {
createWindow();
// Open browser links in new window
win.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: 'deny' };
});
app.on('activate', () => {
// Open a window if none are open (macOS)
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
app.quit();
});
app.on('activate', () => {
// On Mac it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (!win) createWindow();
});
////////////////////////////////////
// IPC Calls //
////////////////////////////////////
ipcMain.on('getNetwork', event => {
// Network args provided must match the Network enum key in @enums
const network = isTestnet ? 'Testnet' : 'Mainnet';
event.sender.send('getNetworkResponse', network);
});
ipcMain.on('log', (event: any, arg: any) => {
logger[arg.level](arg.data);
});
////////////////////////////////////
// Private helper methods //
////////////////////////////////////
const createWindow = () => {
setupLogger();
win = new BrowserWindow({
width: 1280,
height: 800,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
// Running locally with --serve we want hot reloads
if (serve) {
win.webContents.openDevTools();
// hot reloads
require('electron-reload')(__dirname, {
electron: require(path.join(__dirname, '../../node_modules/electron'))
});
// Look to localhost
win.loadURL('http://localhost:4200');
}
// else running in production
else {
// Load initial page
win.loadURL(SERVE_URL);
// Window refresh, reload home page
win.webContents.on('did-fail-load', () => {
console.log('did-fail-load');
win.loadURL(SERVE_URL);
});
}
};
const setupLogger = () => {
logger.transports.file.level = 'info';
const d = new Date();
const dateString = `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`;
// Linux: ~/.config/{app name}/logs/{process type}.log
// macOS: ~/Library/Logs/{app name}/{process type}.log
// Windows: %USERPROFILE%\AppData\Roaming\{app name}\logs\{process type}.log
logger.transports.file.fileName = `${dateString}.log`;
}