This repository has been archived by the owner on Nov 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
main.js
319 lines (289 loc) · 9.64 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
const { app, BrowserWindow, ipcMain, dialog, Menu, globalShortcut } = require("electron");
const { Tray, nativeImage } = require("electron");
const logger = require("electron-timber");
const { autoUpdater } = require("electron-updater");
const serve = require("electron-serve");
const loadURL = serve({ directory: "public" });
const _ = require("lodash");
const isPortReachable = require("is-port-reachable");
const path = require("path");
const domain = ".stacks.run";
const wsl = require("./wsl");
const os = require("os");
const settings = require("electron-settings");
const { powerSaveBlocker } = require("electron");
let tray = null;
let mainWindow;
let forceQuit = false;
let proxy;
function isDev() {
return !app.isPackaged;
}
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
show: false,
width: 1280,
height: 720,
minWidth: 1280,
minHeight: 720,
// titleBarStyle: 'hidden',
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true,
devTools: true,
contextIsolation: false,
enableRemoteModule: true,
},
});
mainWindow.on("close", function (event) {
logger.log("App id quiting: ", forceQuit);
if (!forceQuit) {
logger.log("Preventing force quit");
event.preventDefault();
mainWindow.hide();
return false;
} else {
app.quit();
}
});
// win.maximize();
// and load the index.html of the app.
if (isDev()) {
mainWindow.loadURL("http://localhost:5000/");
} else {
loadURL(mainWindow);
}
mainWindow.once("ready-to-show", () => {
mainWindow.show();
autoUpdater.checkForUpdatesAndNotify();
});
mainWindow.once('focus', () => {
logger.log('mainWindow focused');
});
// Open the DevTools.
// mainWindow.webContents.openDevTools();
}
// 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.whenReady().then(function () {
// Add icons and context menus to the system's notification area.
initTray();
app.allowRendererProcessReuse = false;
createWindow();
});
// Quit when all windows are closed.
app.on("window-all-closed", () => {
logger.log("All windows closed");
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") {
// Unregister all shortcuts.
globalShortcut.unregisterAll()
app.quit();
}
});
app.on("activate", () => {
logger.log("Activating window...");
mainWindow.show();
// On macOS 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 (BrowserWindow.getAllWindows().length === 0) {
// logger.log("Showing window...")
// mainWindow.show();
// }
});
app.on("before-quit", function () {
logger.log("Force quit true");
forceQuit = true;
});
app.on("will-finish-launching", function () {
// Protocol handler for osx
app.on("open-url", function (event, url) {
event.preventDefault();
let deeplinkingUrl = url;
logEverywhere("open-url# " + deeplinkingUrl);
});
});
//Auto updater events
autoUpdater.on('checking-for-update', () => {
sentUpdatesToRenderer('Checking for update...');
})
autoUpdater.on('update-available', (info) => {
sentUpdatesToRenderer('Update available.');
})
autoUpdater.on('update-not-available', (info) => {
sentUpdatesToRenderer('Update not available.');
})
autoUpdater.on('error', (err) => {
sentUpdatesToRenderer('Error in auto-updater. ' + err);
})
autoUpdater.on('download-progress', (progressObj) => {
let log_message = "Download speed: " + progressObj.bytesPerSecond;
log_message = log_message + ' - Downloaded ' + progressObj.percent + '%';
log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
sentUpdatesToRenderer(log_message);
})
autoUpdater.on('update-downloaded', (info) => {
sentUpdatesToRenderer('Update downloaded');
});
function sentUpdatesToRenderer(message, event = 'auto-updater') {
mainWindow.webContents.send(event, message);
}
// Log both at dev console and at running node console instance
function logEverywhere(s) {
if (mainWindow && mainWindow.webContents) {
mainWindow.webContents.executeJavaScript(`console.log("${s}")`);
}
}
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
ipcMain.on("open-file-dialog", (event) => {
dialog
.showOpenDialog({
// defaultPath: "\\\\wsl$\\deck-app\\mnt\\wsl",
properties: ["openDirectory", "createDirectory"],
// message:
// "The selected path is where your codes are, use an existing project path or a new one",
})
.then((result) => {
event.sender.send("selected-directory", result.filePaths);
})
.catch((err) => { });
});
ipcMain.on("app_version", (event) => {
event.sender.send("app_version", { version: app.getVersion() });
});
ipcMain.on("restart-app", (event) => {
app.relaunch();
app.exit();
});
ipcMain.handle("power-saving", async (event, arg) => {
logger.log("Power save blocker", arg);
let powerSaving = false;
if (_.get(arg, "action", "") === "start") {
logger.log("Power save blocker going to start.");
powerSaving = powerSaveBlocker.start("prevent-display-sleep");
logger.log("Power save blocker going to start result.", powerSaving);
} else if (_.get(arg, "action", "") === "stop") {
logger.log("Power save blocker going to stop.");
powerSaving = _.get(arg, "powerSaving", false);
if (powerSaveBlocker.isStarted(powerSaving)) {
logger.log("Power save blocker going to stop result.", powerSaving);
powerSaveBlocker.stop(powerSaving);
}
}
return powerSaving;
});
ipcMain.on("register-proxy", async (event, data) => {
let dockerEngine = await settings.get("settings.dockerEngine");
logger.log("Receiving proxy args from renderer");
logger.log(data); // prints "ping"
logger.log(`http://${dockerEngine.host}:`);
if (_.has(data, "action") && data.action == "init") {
let http = await isPortReachable(80, { host: "localhost" });
let https = await isPortReachable(443, { host: "localhost" });
if (http === false && https === false) {
initialize(data.args.path);
register(data.args.stacks);
logger.log("Registering proxies");
} else {
logger.log("ERR: port 80 & 443 are blocked");
}
} else if (_.has(data, "action") && data.action == "register") {
let host = _.has(dockerEngine, "host")
? dockerEngine.host
: "localhost";
proxy.register(
data.args.stack + domain,
`http://${host}:${data.args.port}`
);
} else {
logger.log("Unknown message from parent: ", data);
}
logger.log(dockerEngine);
});
ipcMain.on("start-wsl", (event) => {
runWsl();
});
ipcMain.on("dock-bounce", (event, data) => {
if (os.platform() === 'darwin')
app.dock.bounce(data.type);
else if (os.platform() === 'win32') {
//https://www.electronjs.org/docs/latest/tutorial/windows-taskbar#flash-frame
logger.log('Flashing windows frame ...');
mainWindow.flashFrame(true);
//If it's not a critical alert in Windows
//then hide it after 10s
if (data.type !== 'critical') {
setTimeout(() => {
mainWindow.flashFrame(false);
}, 10000);
}
}
});
async function initialize(path) {
logger.log("Received path ", path);
proxy = require("redbird")({
port: 80,
ssl: {
port: 443,
key: `${path}/storage/certs/dev-key.pem`,
cert: `${path}/storage/certs/dev-cert.pem`,
},
bunyan: (isDev()) ? true : false,
});
}
async function register(data) {
logger.log("Received data ", data);
let dockerEngine = await settings.get("settings.dockerEngine");
let host = _.has(dockerEngine, "host") ? dockerEngine.host : "localhost";
_.forOwn(data, function (value, key) {
if (value.STACK_HTTP_PORT) {
proxy.register(
`${value.COMPOSE_PROJECT_NAME}.stacks.run`,
`http://${host}:${value.STACK_HTTP_PORT}`
);
}
});
}
/**
* WSL run on windows system
*/
async function runWsl() {
let dockerEngine = await settings.get("settings.dockerEngine");
if (dockerEngine.remoteEngine && os.platform() === "win32") {
wsl.runWSLDaemon("wsl -d deck-app dockerd");
}
}
/**
* Add icons and context menus to the system's notification area.
*/
function initTray() {
tray = new Tray(
nativeImage.createFromPath(
path.join(__dirname, "./assets/tray-icon/IconTemplate.png")
)
);
const contextMenu = Menu.buildFromTemplate([
{
label: "Open DECK",
click: function () {
mainWindow.show();
},
},
{
label: "Quit",
click: function () {
forceQuit = true;
app.quit();
},
},
]);
tray.on('click', () => {
mainWindow.show();
});
tray.setToolTip("Open DECK");
tray.setContextMenu(contextMenu);
}