-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.ts
143 lines (121 loc) · 3.85 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
import {
app,
BrowserWindow,
dialog,
ipcMain,
type OpenDialogOptions,
} from "electron";
import path from "node:path";
import log from "electron-log";
import { Setting } from "./setting";
import { getOuterbaseDir, isMac } from "./utils";
import { ConnectionPool } from "./connection-pool";
import { MainWindow } from "./window/main-window";
import electronUpdater, { type AppUpdater } from "electron-updater";
import { type ConnectionStoreItem } from "@/lib/conn-manager-store";
import { createDatabaseWindow } from "./window/create-database";
import { bindMenuIpc, bindDockerIpc, bindSavedDocIpc } from "./ipc";
import { bindAnalyticIpc } from "./ipc/analytics";
export function getAutoUpdater(): AppUpdater {
// Using destructuring to access autoUpdater due to the CommonJS module of 'electron-updater'.
// It is a workaround for ESM compatibility issues, see https://github.com/electron-userland/electron-builder/issues/7976.
const { autoUpdater } = electronUpdater;
return autoUpdater;
}
const autoUpdater = getAutoUpdater();
log.transports.file.level = "info";
autoUpdater.logger = log;
autoUpdater.autoDownload = false;
const dirname = getOuterbaseDir();
// The built directory structure
//
// ├─┬─┬ dist
// │ │ └── index.html
// │ │
// │ ├─┬ dist-electron
// │ │ ├── main.js
// │ │ └── preload.mjs
// │
process.env.APP_ROOT = path.join(dirname, "..");
// 🚧 Use ['ENV_NAME'] avoid vite:define plugin - [email protected]
export const VITE_DEV_SERVER_URL = process.env["VITE_DEV_SERVER_URL"];
export const MAIN_DIST = path.join(process.env.APP_ROOT, "dist-electron");
export const RENDERER_DIST = path.join(process.env.APP_ROOT, "dist");
process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL
? path.join(process.env.APP_ROOT, "public")
: RENDERER_DIST;
export const settings = new Setting();
settings.load();
const mainWindow = new MainWindow();
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", () => {
mainWindow.remove();
if (!isMac) {
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 (BrowserWindow.getAllWindows().length === 0) {
mainWindow.init();
bindMenuIpc(mainWindow);
}
});
app
.whenReady()
.then(() => mainWindow.init())
.finally(() => {
bindDockerIpc(mainWindow);
bindMenuIpc(mainWindow);
});
ipcMain.handle("query", async (_, connectionId, query) => {
const r = await ConnectionPool.query(connectionId, query);
return r;
});
ipcMain.handle(
"transaction",
async (_, connectionId: string, query: string[]) => {
return await ConnectionPool.batch(connectionId, query);
},
);
ipcMain.handle("close", async (event) => {
event.sender.close({
waitForBeforeUnload: true,
});
});
ipcMain.handle("connect", (_, conn: ConnectionStoreItem, enableDebug) => {
createDatabaseWindow({
conn,
main: mainWindow,
enableDebug,
});
if (mainWindow.getWindow()) {
mainWindow.hide();
}
});
ipcMain.handle("test-connection", async (_, conn: ConnectionStoreItem) => {
return await ConnectionPool.testConnection(conn);
});
ipcMain.handle("download-update", () => {
autoUpdater.downloadUpdate();
});
ipcMain.handle("restart", () => {
autoUpdater.quitAndInstall();
});
ipcMain.handle("open-file-dialog", async (_, options: OpenDialogOptions) => {
return await dialog.showOpenDialog(options);
});
ipcMain.handle("get-setting", (_, key) => {
return settings.get(key);
});
ipcMain.handle("set-setting", (_, key, value) => {
settings.set(key, value);
});
ipcMain.on("navigate", (event, route: string) => {
event.sender.send("navigate-to", route);
});
bindSavedDocIpc();
bindAnalyticIpc();