-
Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathplatform.ts
273 lines (248 loc) · 9.93 KB
/
platform.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
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
// Copyright 2025, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
import { fireAndForget } from "@/util/util";
import { app, dialog, ipcMain, shell } from "electron";
import envPaths from "env-paths";
import { existsSync, mkdirSync } from "fs";
import os from "os";
import path from "path";
import { WaveDevVarName, WaveDevViteVarName } from "../frontend/util/isdev";
import * as keyutil from "../frontend/util/keyutil";
// This is a little trick to ensure that Electron puts all its runtime data into a subdirectory to avoid conflicts with our own data.
// On macOS, it will store to ~/Library/Application \Support/waveterm/electron
// On Linux, it will store to ~/.config/waveterm/electron
// On Windows, it will store to %LOCALAPPDATA%/waveterm/electron
app.setName("waveterm/electron");
const isDev = !app.isPackaged;
const isDevVite = isDev && process.env.ELECTRON_RENDERER_URL;
console.log(`Running in ${isDev ? "development" : "production"} mode`);
if (isDev) {
process.env[WaveDevVarName] = "1";
}
if (isDevVite) {
process.env[WaveDevViteVarName] = "1";
}
const waveDirNamePrefix = "waveterm";
const waveDirNameSuffix = isDev ? "dev" : "";
const waveDirName = `${waveDirNamePrefix}${waveDirNameSuffix ? `-${waveDirNameSuffix}` : ""}`;
const paths = envPaths("waveterm", { suffix: waveDirNameSuffix });
app.setName(isDev ? "Wave (Dev)" : "Wave");
const unamePlatform = process.platform;
const unameArch: string = process.arch;
keyutil.setKeyUtilPlatform(unamePlatform);
const WaveConfigHomeVarName = "WAVETERM_CONFIG_HOME";
const WaveDataHomeVarName = "WAVETERM_DATA_HOME";
const WaveHomeVarName = "WAVETERM_HOME";
export function checkIfRunningUnderARM64Translation(fullConfig: FullConfigType) {
if (!fullConfig.settings["app:dismissarchitecturewarning"] && app.runningUnderARM64Translation) {
console.log("Running under ARM64 translation, alerting user");
const dialogOpts: Electron.MessageBoxOptions = {
type: "warning",
buttons: ["Dismiss", "Learn More"],
title: "Wave has detected a performance issue",
message: `Wave is running in ARM64 translation mode which may impact performance.\n\nRecommendation: Download the native ARM64 version from our website for optimal performance.`,
};
const choice = dialog.showMessageBoxSync(null, dialogOpts);
if (choice === 1) {
// Open the documentation URL
console.log("User chose to learn more");
fireAndForget(() =>
shell.openExternal(
"https://docs.waveterm.dev/faq#why-does-wave-warn-me-about-arm64-translation-when-it-launches"
)
);
throw new Error("User redirected to docsite to learn more about ARM64 translation, exiting");
} else {
console.log("User dismissed the dialog");
}
}
}
/**
* Gets the path to the old Wave home directory (defaults to `~/.waveterm`).
* @returns The path to the directory if it exists and contains valid data for the current app, otherwise null.
*/
function getWaveHomeDir(): string {
let home = process.env[WaveHomeVarName];
if (!home) {
const homeDir = app.getPath("home");
if (homeDir) {
home = path.join(homeDir, `.${waveDirName}`);
}
}
// If home exists and it has `wave.lock` in it, we know it has valid data from Wave >=v0.8. Otherwise, it could be for WaveLegacy (<v0.8)
if (home && existsSync(home) && existsSync(path.join(home, "wave.lock"))) {
return home;
}
return null;
}
/**
* Ensure the given path exists, creating it recursively if it doesn't.
* @param path The path to ensure.
* @returns The same path, for chaining.
*/
function ensurePathExists(path: string): string {
if (!existsSync(path)) {
mkdirSync(path, { recursive: true });
}
return path;
}
/**
* Gets the path to the directory where Wave configurations are stored. Creates the directory if it does not exist.
* Handles backwards compatibility with the old Wave Home directory model, where configurations and data were stored together.
* @returns The path where configurations should be stored.
*/
function getWaveConfigDir(): string {
// If wave home dir exists, use it for backwards compatibility
const waveHomeDir = getWaveHomeDir();
if (waveHomeDir) {
return path.join(waveHomeDir, "config");
}
const override = process.env[WaveConfigHomeVarName];
const xdgConfigHome = process.env.XDG_CONFIG_HOME;
let retVal: string;
if (override) {
retVal = override;
} else if (xdgConfigHome) {
retVal = path.join(xdgConfigHome, waveDirName);
} else {
retVal = path.join(app.getPath("home"), ".config", waveDirName);
}
return ensurePathExists(retVal);
}
/**
* Gets the path to the directory where Wave data is stored. Creates the directory if it does not exist.
* Handles backwards compatibility with the old Wave Home directory model, where configurations and data were stored together.
* @returns The path where data should be stored.
*/
function getWaveDataDir(): string {
// If wave home dir exists, use it for backwards compatibility
const waveHomeDir = getWaveHomeDir();
if (waveHomeDir) {
return waveHomeDir;
}
const override = process.env[WaveDataHomeVarName];
const xdgDataHome = process.env.XDG_DATA_HOME;
let retVal: string;
if (override) {
retVal = override;
} else if (xdgDataHome) {
retVal = path.join(xdgDataHome, waveDirName);
} else {
retVal = paths.data;
}
return ensurePathExists(retVal);
}
function getElectronAppBasePath(): string {
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 getWaveDataDir();
}
ipcMain.on("get-is-dev", (event) => {
event.returnValue = isDev;
});
ipcMain.on("get-platform", (event, url) => {
event.returnValue = unamePlatform;
});
ipcMain.on("get-user-name", (event) => {
const userInfo = os.userInfo();
event.returnValue = userInfo.username;
});
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");
});
ipcMain.on("get-data-dir", (event) => {
event.returnValue = getWaveDataDir();
});
ipcMain.on("get-config-dir", (event) => {
event.returnValue = getWaveConfigDir();
});
/**
* Gets the value of the XDG_CURRENT_DESKTOP environment variable. If ORIGINAL_XDG_CURRENT_DESKTOP is set, it will be returned instead.
* This corrects for a strange behavior in Electron, where it sets its own value for XDG_CURRENT_DESKTOP to improve Chromium compatibility.
* @see https://www.electronjs.org/docs/latest/api/environment-variables#original_xdg_current_desktop
* @returns The value of the XDG_CURRENT_DESKTOP environment variable, or ORIGINAL_XDG_CURRENT_DESKTOP if set, or undefined if neither are set.
*/
function getXdgCurrentDesktop(): string {
if (process.env.ORIGINAL_XDG_CURRENT_DESKTOP) {
return process.env.ORIGINAL_XDG_CURRENT_DESKTOP;
} else if (process.env.XDG_CURRENT_DESKTOP) {
return process.env.XDG_CURRENT_DESKTOP;
} else {
return undefined;
}
}
/**
* Calls the given callback with the value of the XDG_CURRENT_DESKTOP environment variable set to ORIGINAL_XDG_CURRENT_DESKTOP if it is set.
* @see https://www.electronjs.org/docs/latest/api/environment-variables#original_xdg_current_desktop
* @param callback The callback to call.
*/
function callWithOriginalXdgCurrentDesktop(callback: () => void) {
const currXdgCurrentDesktopDefined = "XDG_CURRENT_DESKTOP" in process.env;
const currXdgCurrentDesktop = process.env.XDG_CURRENT_DESKTOP;
const originalXdgCurrentDesktop = getXdgCurrentDesktop();
if (originalXdgCurrentDesktop) {
process.env.XDG_CURRENT_DESKTOP = originalXdgCurrentDesktop;
}
callback();
if (originalXdgCurrentDesktop) {
if (currXdgCurrentDesktopDefined) {
process.env.XDG_CURRENT_DESKTOP = currXdgCurrentDesktop;
} else {
delete process.env.XDG_CURRENT_DESKTOP;
}
}
}
/**
* Calls the given async callback with the value of the XDG_CURRENT_DESKTOP environment variable set to ORIGINAL_XDG_CURRENT_DESKTOP if it is set.
* @see https://www.electronjs.org/docs/latest/api/environment-variables#original_xdg_current_desktop
* @param callback The async callback to call.
*/
async function callWithOriginalXdgCurrentDesktopAsync(callback: () => Promise<void>) {
const currXdgCurrentDesktopDefined = "XDG_CURRENT_DESKTOP" in process.env;
const currXdgCurrentDesktop = process.env.XDG_CURRENT_DESKTOP;
const originalXdgCurrentDesktop = getXdgCurrentDesktop();
if (originalXdgCurrentDesktop) {
process.env.XDG_CURRENT_DESKTOP = originalXdgCurrentDesktop;
}
await callback();
if (originalXdgCurrentDesktop) {
if (currXdgCurrentDesktopDefined) {
process.env.XDG_CURRENT_DESKTOP = currXdgCurrentDesktop;
} else {
delete process.env.XDG_CURRENT_DESKTOP;
}
}
}
export {
callWithOriginalXdgCurrentDesktop,
callWithOriginalXdgCurrentDesktopAsync,
getElectronAppBasePath,
getElectronAppUnpackedBasePath,
getWaveConfigDir,
getWaveDataDir,
getWaveSrvCwd,
getWaveSrvPath,
getXdgCurrentDesktop,
isDev,
isDevVite,
unameArch,
unamePlatform,
WaveConfigHomeVarName,
WaveDataHomeVarName,
};