-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.ts
187 lines (167 loc) · 5.53 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
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
176
177
178
179
180
181
182
183
184
185
186
187
import { app, BrowserWindow, screen, ipcMain, Notification, Menu } from 'electron';
import * as path from 'path';
import * as url from 'url';
import * as fs from 'fs';
let win, serve, debug;
const args = process.argv.slice(1);
serve = args.some(val => val === '--serve');
debug = args.some(val => val === '--debug');
const globalTS: any = global;
globalTS.vars = {};
let applicationQuitCalled = false;
// set application data path
const defaultCSCPath = path.join(app.getPath('home'), '.casinocoin-wlt');
if (!fs.existsSync(defaultCSCPath)) {
fs.mkdirSync(defaultCSCPath);
}
app.setPath('userData', defaultCSCPath);
// create application backup path
globalTS.vars.backupLocation = path.join(app.getPath('documents'), 'CSC');
if (!fs.existsSync(globalTS.vars.backupLocation)) {
fs.mkdirSync(globalTS.vars.backupLocation);
}
// configure Winston File loggging
const winston = require('winston');
if (serve || debug) {
globalTS.loglevel = 'debug';
} else {
globalTS.loglevel = 'info';
}
const logFolder = path.join(app.getPath('home'), '.casinocoin-wlt', 'logs');
if (!fs.existsSync(logFolder)) {
fs.mkdirSync(logFolder);
}
const logFilename = new Date().toISOString().replace(/:/g, '.') + '.log';
const logFile = path.join(logFolder, logFilename);
winston.add(new winston.transports.File(
{ filename: logFile,
level: globalTS.loglevel,
handleExceptions: true,
maxsize: 10485760, // 10 MB
format: winston.format.combine(winston.format.timestamp(), winston.format.printf(i => `${i.timestamp} | [${i.level}] ${i.message}`)),
maxFiles: 5,
colorize: false
})
);
winston.remove(winston.transports.Console);
winston.level = globalTS.loglevel;
globalTS.logger = winston;
function createWindow() {
winston.log('info', 'Create Native Window');
const electronScreen = screen;
const size = electronScreen.getPrimaryDisplay().workAreaSize;
const minimalWidth = Math.min(size.width, 1024);
const minimalHeight = Math.min(size.height, 720);
// Create the browser window.
win = new BrowserWindow({
x: 0,
y: 0,
width: minimalWidth,
minWidth: minimalWidth,
height: minimalHeight,
minHeight: minimalHeight,
icon: __dirname + '/favicon.ico',
webPreferences: {
nodeIntegration: true,
webSecurity: false
}
});
if (serve) {
require('electron-reload')(__dirname, {
electron: require(`${__dirname}/node_modules/electron`)
});
win.loadURL('http://localhost:4200');
} else {
win.loadURL(url.format({
pathname: path.join(__dirname, 'dist/index.html'),
protocol: 'file:',
slashes: true
}));
}
if (serve) {
win.webContents.openDevTools();
}
win.on('close', (event) => {
winston.log('info', 'Window close');
if (win == null) {
winston.log('info', 'Window == null');
} else if (!applicationQuitCalled) {
event.preventDefault();
applicationQuitCalled = true;
if (win !== null) {
ipcMain.on('wallet-closed', (ipcevent, arg) => {
winston.log('info', 'Saved Wallet Before Quit Finished, now we close the app');
win.close();
});
// Send event to renderer to save and close wallet
win.webContents.send('action', 'quit-wallet');
} else {
// window is already null so walletService got already killed
winston.log('info', 'Window == null');
}
} else {
winston.log('info', 'Window close - final exit');
}
});
// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store window
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
winston.log('info', 'Window closed');
win = null;
});
// push notification using electron-notification-desktop
ipcMain.on('push-notification', (event, arg) => {
winston.log('info', 'Notification: ' + arg.title);
const notification = new Notification( {
title: arg.title,
body: arg.body,
icon: path.join(__dirname, 'assets/brand/casinocoin-icon-256x256.png')
});
notification.show();
});
// Create the OSX Application's main menu
const template: Electron.MenuItemConstructorOptions[] = [{
label: 'CasinoCoin Wallet',
submenu: [
{label: 'Cut', accelerator: 'Command+X', role: 'cut'},
{label: 'Copy', accelerator: 'Command+C', role: 'copy'},
{label: 'Paste', accelerator: 'Command+V', role: 'paste'},
{type: 'separator'},
{label: 'Quit', accelerator: 'Command+Q', click: function() { app.quit(); }}
]
}];
const menu = Menu.buildFromTemplate(template);
if (process.platform === 'darwin') {
Menu.setApplicationMenu(menu);
}
}
try {
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
app.on('before-quit', (event) => {
winston.log('info', 'before-quit');
});
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
winston.log('info', 'window-all-closed');
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On OS X 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 === null) {
createWindow();
}
});
} catch (e) {
// Catch Error
// throw e;
}