-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
100 lines (80 loc) · 2 KB
/
index.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
import os from "os";
import fs from "fs";
import net from "net";
import { app, BrowserWindow, ipcMain } from "electron";
import serve from "electron-serve";
import type { Api } from "./src/api";
import createApi from "./lib/createApi";
import * as config from "./lib/config";
const addApiHandlers = (api: Api) => {
for (const key in api) {
// get params, excluding first event variable
// @ts-ignore
ipcMain.handle(key, (...params: any[]) => api[key](...params.slice(1)));
}
};
const loadURL = serve({ directory: `${__dirname}/dist` });
(async () => {
await app.whenReady();
const mainWindow = new BrowserWindow({
width: 700,
height: 500,
minWidth: 700,
minHeight: 500,
maxWidth: 700,
maxHeight: 500,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: false,
preload: `${__dirname}/preload.js`
}
});
const api = createApi({
mainWindow
});
const { credentials, resticPassphrase } = await config.get();
if (
typeof credentials === "object" &&
typeof resticPassphrase === "string"
) {
const response = await api.setup({
...credentials,
resticPassphrase
});
console.log("auto login response", response);
}
addApiHandlers(api);
app.setLoginItemSettings({
openAtLogin: true
});
if (process.env.STORJ_BACKUP_DEV !== "true") {
// load from directory
await loadURL(mainWindow);
await mainWindow.loadURL("app://-");
await mainWindow.focus();
} else {
const vueServePort = 8080;
// wait for dev server to start
for (;;) {
const ready = await new Promise((resolve) => {
const socket = net.createConnection({
port: vueServePort
});
socket.on("connect", () => {
resolve(true);
});
socket.on("error", () => {
resolve(false);
});
});
if (ready === true) {
break;
}
await new Promise((resolve) => setTimeout(resolve, 500));
}
// load from vue serve
await mainWindow.loadURL(`http://127.0.0.1:${vueServePort}`);
await mainWindow.focus();
mainWindow.webContents.openDevTools();
}
})();