forked from abdellaui/SoPlan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
executable file
·243 lines (206 loc) · 5.68 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import 'reflect-metadata';
import * as bodyParser from 'body-parser';
import { app, BrowserWindow, Menu, MenuItemConstructorOptions } from 'electron';
import * as express from 'express';
import * as path from 'path';
import * as url from 'url';
import { Database } from './backend/database';
import { DatabaseConfig } from './backend/models/configs.class';
import { setExpressServer } from './backend/slots';
let mainWindow: any = null;
let database: Database;
// detect mode
const args: any = process.argv.slice(1);
const serve: boolean = args.some(val => val === '--serve');
const test: boolean = args.some(val => val === '--test');
const mockPort: Number = 3030;
const testDatabaseConfigs: DatabaseConfig = {
host: 'localhost',
port: 3306,
username: 'tester',
password: 'password',
database: 'tests'
};
function init(): void {
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit(); // Quit second instance
} else {
if (test) { createMockServer(); } else { createWindow(); }
}
}
/**
* If someone tries to run a new instance, but we already have one, bring it to the front
*/
app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
}
});
app.on('ready', () => {
});
function createWindow(): void {
database = new Database();
mainWindow = new BrowserWindow({
width: 1000,
height: 800,
minWidth: 640,
minHeight: 360,
backgroundColor: '#ebeff5',
show: false,
webPreferences: {
plugins: true,
experimentalFeatures: true
}
});
const templatey: MenuItemConstructorOptions[] = [
{
label: 'SoPlan',
submenu: [
{ accelerator: 'CmdOrCtrl+Z', role: 'undo' },
{ accelerator: 'Shift+CmdOrCtrl+Z', role: 'redo' },
{ type: 'separator' },
{ accelerator: 'CmdOrCtrl+X', role: 'cut' },
{ accelerator: 'CmdOrCtrl+C', role: 'copy' },
{ accelerator: 'CmdOrCtrl+V', role: 'paste' },
{ accelerator: 'CmdOrCtrl+A', role: 'selectAll' }
]
}
];
const template: MenuItemConstructorOptions[] = [
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'pasteandmatchstyle' },
{ role: 'delete' },
{ role: 'selectall' }
]
},
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forcereload' },
// { role: 'toggledevtools' },
{ type: 'separator' },
{ role: 'resetzoom' },
{ role: 'zoomin' },
{ role: 'zoomout' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
},
{
role: 'window',
submenu: [
{ role: 'minimize' },
{ role: 'close' }
]
}
];
if (process.platform === 'darwin') {
template.unshift({
label: app.getName(),
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
});
// Window menu
template[3].submenu = [
{ role: 'close' },
{ role: 'minimize' },
{ role: 'zoom' },
{ type: 'separator' },
{ role: 'front' }
];
}
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
if (serve) {
// get dynamic version from localhost:4200
mainWindow.loadURL('http://localhost:4200');
} else {
// load the dist folder from Angular
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, '../', '/dist/index.html'),
protocol: 'file:',
slashes: true
})
);
}
// The following is optional and will open the DevTools:
// please comment it on compiling to a executable
// mainWindow.webContents.openDevTools();
// mainWindow.maximize();
// require('devtron').install();
// end devTools
mainWindow.on('closed', () => {
mainWindow = null;
database.close();
});
mainWindow.once('ready-to-show', () => {
mainWindow.show();
});
}
function createMockServer(): void {
const expressApp: any = express();
// @ts-ignore
expressApp.use(bodyParser.json());
expressApp.use((req: any, res: any, next: any) => {
console.log(req.method, req.url, req.body);
res.header('Content-Type', 'application/json');
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
setExpressServer(expressApp);
database = new Database(testDatabaseConfigs);
expressApp.use((err: any, req: any, res: any, next: any) => {
console.error(err.stack);
res.status(404).end();
});
expressApp.listen(mockPort, () => {
console.log(`mock server is running on http://localhost:${mockPort}/`);
});
mainWindow = true;
}
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', init);
// 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
if (process.platform !== 'darwin') {
database.close();
app.quit();
}
});
// initialize the app's main window
app.on('activate', () => {
if (mainWindow === null) {
init();
}
});
} catch (e) {
console.log(e);
}