waveterm/emain/platform.ts

86 lines
2.3 KiB
TypeScript
Raw Normal View History

// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
import { app, ipcMain } from "electron";
import os from "os";
import path from "path";
2024-09-18 21:06:34 +02:00
import { WaveDevVarName, WaveDevViteVarName } from "../frontend/util/isdev";
import * as keyutil from "../frontend/util/keyutil";
const WaveHomeVarName = "WAVETERM_HOME";
const isDev = !app.isPackaged;
const isDevVite = isDev && process.env.ELECTRON_RENDERER_URL;
if (isDev) {
process.env[WaveDevVarName] = "1";
}
if (isDevVite) {
process.env[WaveDevViteVarName] = "1";
}
2024-09-19 23:04:47 +02:00
app.setName(isDev ? "Wave (Dev)" : "Wave");
const unamePlatform = process.platform;
const unameArch: string = process.arch;
keyutil.setKeyUtilPlatform(unamePlatform);
ipcMain.on("get-is-dev", (event) => {
event.returnValue = isDev;
});
ipcMain.on("get-platform", (event, url) => {
event.returnValue = unamePlatform;
});
2024-08-28 03:49:49 +02:00
ipcMain.on("get-user-name", (event) => {
const userInfo = os.userInfo();
event.returnValue = userInfo.username;
});
2024-09-06 03:54:12 +02:00
ipcMain.on("get-host-name", (event) => {
event.returnValue = os.hostname();
});
ipcMain.on("get-webview-preload", (event) => {
event.returnValue = path.join(getElectronAppBasePath(), "preload", "preload-webview.cjs");
});
// must match golang
function getWaveHomeDir() {
const override = process.env[WaveHomeVarName];
if (override) {
return override;
}
2024-09-06 00:01:28 +02:00
return path.join(os.homedir(), isDev ? ".waveterm-dev" : ".waveterm");
}
function getElectronAppBasePath(): string {
2024-09-17 00:58:35 +02:00
return path.dirname(import.meta.dirname);
}
function getElectronAppUnpackedBasePath(): string {
return getElectronAppBasePath().replace("app.asar", "app.asar.unpacked");
}
const wavesrvBinName = `wavesrv.${unameArch}`;
function getWaveSrvPath(): string {
if (process.platform === "win32") {
const winBinName = `${wavesrvBinName}.exe`;
const appPath = path.join(getElectronAppUnpackedBasePath(), "bin", winBinName);
return `${appPath}`;
}
return path.join(getElectronAppUnpackedBasePath(), "bin", wavesrvBinName);
}
function getWaveSrvCwd(): string {
return getWaveHomeDir();
}
export {
getElectronAppBasePath,
getElectronAppUnpackedBasePath,
getWaveHomeDir,
getWaveSrvCwd,
getWaveSrvPath,
isDev,
isDevVite,
unameArch,
unamePlatform,
};