gui/modules/desktop/electron/libs/ipc.ts
Neil cbe19fa138
refactor main thread (#304)
* move auto-updater to its own module

* #276 move ipc to its own mobule

---------

Co-authored-by: neil <neil@neils-MacBook-Pro.local>
2023-03-16 17:45:06 +08:00

57 lines
1.5 KiB
TypeScript

import { ipcMain } from "electron";
import { getInstalledPackages } from "./tea-dir";
import { readSessionData, writeSessionData } from "./auth";
import type { Session } from "../../src/libs/types";
import { installPackage, openTerminal } from "./cli";
import { getUpdater } from "./auto-updater";
let teaProtocolPath = ""; // this should be empty string
export const setProtocolPath = (path: string) => {
teaProtocolPath = path;
};
export default function initializeHandlers() {
ipcMain.handle("get-installed-packages", async () => {
const pkgs = await getInstalledPackages();
return pkgs;
});
ipcMain.handle("get-session", async () => {
const session = await readSessionData();
return session;
});
ipcMain.handle("update-session", async (_, data) => {
await writeSessionData(data as Session);
});
ipcMain.handle("install-package", async (_, data) => {
const result = await installPackage(data.full_name);
return result;
});
ipcMain.handle("open-terminal", async (_, data) => {
const { cmd } = data as { cmd: string };
try {
// TODO: detect if mac or linux
// current openTerminal is only design for Mac
await openTerminal(cmd);
} catch (error) {
console.error("elast:", error);
}
});
ipcMain.handle("relaunch", async () => {
const autoUpdater = getUpdater();
await autoUpdater.quitAndInstall();
});
ipcMain.handle("get-protocol-path", async () => {
const path = teaProtocolPath;
teaProtocolPath = "";
return path;
});
}