This repository has been archived by the owner on Jul 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
134 lines (117 loc) · 2.83 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
const {app, BrowserWindow, Menu, MenuItem} = require('electron');
const path = require('path');
const url = require('url');
const appName = "drawr"
let win = null;
let serverProc = null;
// open the application window and load the index.html from the ASAR
function createWindow() {
win = new BrowserWindow({
width: 800,
height: 600
});
win.loadURL(url.format({
pathname: path.join(__dirname, 'dist/basic/index.html'),
protocol: 'file',
slashes: true
}));
win.on('closed', () => {
win = null;
});
}
// spawn a async subprocess with the server
function startServer(serverPort) {
const execFile = require('child_process').execFile;
serverProc = execFile(path.join(__dirname, 'dist/server/drawr-server'), [ '-p', serverPort ], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
console.log(stderr);
});
console.log('drawr-server started.')
}
// stop the server process by sending SIGHUP
function stopServer() {
console.log('stopping drawr-server...', serverProc.pid);
if (serverProc.pid !== null) {
serverProc.kill('SIGHUP');
}
console.log('drawr-server stopped.')
}
// drawr is a single window application
const shouldQuit = app.makeSingleInstance((commandLint, workingDir) => {
// someone tried to run a second instance, focus our window
if (win) {
if (win.isMinimized()) win.restore();
win.focus();
}
});
if (shouldQuit) {
app.quit();
}
// create the rest of the app
app.on('ready', () => {
app.setName(appName)
// do macOS specific stuff
if (process.platform === 'darwin') {
// app.dock.setIcon(appIcon)
// app.dock.setMenu(menu)
}
createWindow();
const menuTempl = [
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'toggledevtools' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
},
{
label: 'Server',
submenu: [
new MenuItem({
label: 'Start',
click() { startServer(8080) }
}),
new MenuItem({
label: 'Stop',
click() { stopServer() }
}),
],
}]
// create the macOS specific menu layout
if (process.platform === 'darwin' ) {
menuTempl.unshift({
label: app.getName(),
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services', submenu: [] },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
})
}
Menu.setApplicationMenu(Menu.buildFromTemplate(menuTempl));
});
app.on('window-all-closed', () => {
// we don't want that on macOS
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (win === null) {
createWindow();
}
});
app.on('quit', () => {
// stopServer();
})