2024-08-22 00:04:39 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
import { app, ipcMain } from "electron";
|
Update data and config paths to match platform defaults (#1047)
Going forward for new installations, config and data files will be
stored at the platform default paths, as defined by
[env-paths](https://www.npmjs.com/package/env-paths).
For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME`
directory exists and contains valid data, it will be used. If this check
fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be
used. If these are not defined, then `XDG_DATA_HOME` and
`XDG_CONFIG_HOME` will be used. Finally, if none of these are defined,
the [env-paths](https://www.npmjs.com/package/env-paths) defaults will
be used.
As with the existing app, dev instances will write to `waveterm-dev`
directories, while all others will write to `waveterm`.
2024-10-22 18:26:58 +02:00
|
|
|
import envPaths from "env-paths";
|
|
|
|
import { existsSync, mkdirSync } from "fs";
|
2024-08-22 00:04:39 +02:00
|
|
|
import os from "os";
|
|
|
|
import path from "path";
|
2024-09-18 21:06:34 +02:00
|
|
|
import { WaveDevVarName, WaveDevViteVarName } from "../frontend/util/isdev";
|
2024-08-22 00:04:39 +02:00
|
|
|
import * as keyutil from "../frontend/util/keyutil";
|
|
|
|
|
Update data and config paths to match platform defaults (#1047)
Going forward for new installations, config and data files will be
stored at the platform default paths, as defined by
[env-paths](https://www.npmjs.com/package/env-paths).
For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME`
directory exists and contains valid data, it will be used. If this check
fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be
used. If these are not defined, then `XDG_DATA_HOME` and
`XDG_CONFIG_HOME` will be used. Finally, if none of these are defined,
the [env-paths](https://www.npmjs.com/package/env-paths) defaults will
be used.
As with the existing app, dev instances will write to `waveterm-dev`
directories, while all others will write to `waveterm`.
2024-10-22 18:26:58 +02:00
|
|
|
// 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");
|
2024-09-26 01:16:07 +02:00
|
|
|
|
2024-08-22 00:04:39 +02:00
|
|
|
const isDev = !app.isPackaged;
|
|
|
|
const isDevVite = isDev && process.env.ELECTRON_RENDERER_URL;
|
2024-11-11 19:46:58 +01:00
|
|
|
console.log(`Running in ${isDev ? "development" : "production"} mode`);
|
2024-08-22 00:04:39 +02:00
|
|
|
if (isDev) {
|
|
|
|
process.env[WaveDevVarName] = "1";
|
|
|
|
}
|
|
|
|
if (isDevVite) {
|
|
|
|
process.env[WaveDevViteVarName] = "1";
|
|
|
|
}
|
|
|
|
|
Update data and config paths to match platform defaults (#1047)
Going forward for new installations, config and data files will be
stored at the platform default paths, as defined by
[env-paths](https://www.npmjs.com/package/env-paths).
For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME`
directory exists and contains valid data, it will be used. If this check
fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be
used. If these are not defined, then `XDG_DATA_HOME` and
`XDG_CONFIG_HOME` will be used. Finally, if none of these are defined,
the [env-paths](https://www.npmjs.com/package/env-paths) defaults will
be used.
As with the existing app, dev instances will write to `waveterm-dev`
directories, while all others will write to `waveterm`.
2024-10-22 18:26:58 +02:00
|
|
|
const waveDirNamePrefix = "waveterm";
|
|
|
|
const waveDirNameSuffix = isDev ? "dev" : "";
|
|
|
|
const waveDirName = `${waveDirNamePrefix}${waveDirNameSuffix ? `-${waveDirNameSuffix}` : ""}`;
|
|
|
|
|
|
|
|
const paths = envPaths("waveterm", { suffix: waveDirNameSuffix });
|
|
|
|
|
2024-09-19 23:04:47 +02:00
|
|
|
app.setName(isDev ? "Wave (Dev)" : "Wave");
|
2024-08-22 00:04:39 +02:00
|
|
|
const unamePlatform = process.platform;
|
2024-09-04 20:23:39 +02:00
|
|
|
const unameArch: string = process.arch;
|
2024-08-22 00:04:39 +02:00
|
|
|
keyutil.setKeyUtilPlatform(unamePlatform);
|
|
|
|
|
Update data and config paths to match platform defaults (#1047)
Going forward for new installations, config and data files will be
stored at the platform default paths, as defined by
[env-paths](https://www.npmjs.com/package/env-paths).
For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME`
directory exists and contains valid data, it will be used. If this check
fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be
used. If these are not defined, then `XDG_DATA_HOME` and
`XDG_CONFIG_HOME` will be used. Finally, if none of these are defined,
the [env-paths](https://www.npmjs.com/package/env-paths) defaults will
be used.
As with the existing app, dev instances will write to `waveterm-dev`
directories, while all others will write to `waveterm`.
2024-10-22 18:26:58 +02:00
|
|
|
const WaveConfigHomeVarName = "WAVETERM_CONFIG_HOME";
|
|
|
|
const WaveDataHomeVarName = "WAVETERM_DATA_HOME";
|
|
|
|
const WaveHomeVarName = "WAVETERM_HOME";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2024-10-23 06:23:24 +02:00
|
|
|
const homeDir = app.getPath("home");
|
Update data and config paths to match platform defaults (#1047)
Going forward for new installations, config and data files will be
stored at the platform default paths, as defined by
[env-paths](https://www.npmjs.com/package/env-paths).
For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME`
directory exists and contains valid data, it will be used. If this check
fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be
used. If these are not defined, then `XDG_DATA_HOME` and
`XDG_CONFIG_HOME` will be used. Finally, if none of these are defined,
the [env-paths](https://www.npmjs.com/package/env-paths) defaults will
be used.
As with the existing app, dev instances will write to `waveterm-dev`
directories, while all others will write to `waveterm`.
2024-10-22 18:26:58 +02:00
|
|
|
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;
|
2024-09-26 01:16:07 +02:00
|
|
|
if (override) {
|
Update data and config paths to match platform defaults (#1047)
Going forward for new installations, config and data files will be
stored at the platform default paths, as defined by
[env-paths](https://www.npmjs.com/package/env-paths).
For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME`
directory exists and contains valid data, it will be used. If this check
fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be
used. If these are not defined, then `XDG_DATA_HOME` and
`XDG_CONFIG_HOME` will be used. Finally, if none of these are defined,
the [env-paths](https://www.npmjs.com/package/env-paths) defaults will
be used.
As with the existing app, dev instances will write to `waveterm-dev`
directories, while all others will write to `waveterm`.
2024-10-22 18:26:58 +02:00
|
|
|
retVal = override;
|
|
|
|
} else if (xdgDataHome) {
|
|
|
|
retVal = path.join(xdgDataHome, waveDirName);
|
|
|
|
} else {
|
|
|
|
retVal = paths.data;
|
2024-09-26 01:16:07 +02:00
|
|
|
}
|
Update data and config paths to match platform defaults (#1047)
Going forward for new installations, config and data files will be
stored at the platform default paths, as defined by
[env-paths](https://www.npmjs.com/package/env-paths).
For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME`
directory exists and contains valid data, it will be used. If this check
fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be
used. If these are not defined, then `XDG_DATA_HOME` and
`XDG_CONFIG_HOME` will be used. Finally, if none of these are defined,
the [env-paths](https://www.npmjs.com/package/env-paths) defaults will
be used.
As with the existing app, dev instances will write to `waveterm-dev`
directories, while all others will write to `waveterm`.
2024-10-22 18:26:58 +02:00
|
|
|
return ensurePathExists(retVal);
|
2024-08-22 00:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getElectronAppBasePath(): string {
|
2024-09-17 00:58:35 +02:00
|
|
|
return path.dirname(import.meta.dirname);
|
2024-08-22 00:04:39 +02:00
|
|
|
}
|
|
|
|
|
2024-10-04 05:28:05 +02:00
|
|
|
function getElectronAppUnpackedBasePath(): string {
|
2024-08-22 00:04:39 +02:00
|
|
|
return getElectronAppBasePath().replace("app.asar", "app.asar.unpacked");
|
|
|
|
}
|
|
|
|
|
|
|
|
const wavesrvBinName = `wavesrv.${unameArch}`;
|
|
|
|
|
|
|
|
function getWaveSrvPath(): string {
|
|
|
|
if (process.platform === "win32") {
|
|
|
|
const winBinName = `${wavesrvBinName}.exe`;
|
2024-10-04 05:28:05 +02:00
|
|
|
const appPath = path.join(getElectronAppUnpackedBasePath(), "bin", winBinName);
|
2024-08-22 00:04:39 +02:00
|
|
|
return `${appPath}`;
|
|
|
|
}
|
2024-10-04 05:28:05 +02:00
|
|
|
return path.join(getElectronAppUnpackedBasePath(), "bin", wavesrvBinName);
|
2024-08-22 00:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getWaveSrvCwd(): string {
|
Update data and config paths to match platform defaults (#1047)
Going forward for new installations, config and data files will be
stored at the platform default paths, as defined by
[env-paths](https://www.npmjs.com/package/env-paths).
For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME`
directory exists and contains valid data, it will be used. If this check
fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be
used. If these are not defined, then `XDG_DATA_HOME` and
`XDG_CONFIG_HOME` will be used. Finally, if none of these are defined,
the [env-paths](https://www.npmjs.com/package/env-paths) defaults will
be used.
As with the existing app, dev instances will write to `waveterm-dev`
directories, while all others will write to `waveterm`.
2024-10-22 18:26:58 +02:00
|
|
|
return getWaveDataDir();
|
2024-08-22 00:04:39 +02:00
|
|
|
}
|
|
|
|
|
2024-10-22 01:51:18 +02:00
|
|
|
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");
|
|
|
|
});
|
Update data and config paths to match platform defaults (#1047)
Going forward for new installations, config and data files will be
stored at the platform default paths, as defined by
[env-paths](https://www.npmjs.com/package/env-paths).
For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME`
directory exists and contains valid data, it will be used. If this check
fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be
used. If these are not defined, then `XDG_DATA_HOME` and
`XDG_CONFIG_HOME` will be used. Finally, if none of these are defined,
the [env-paths](https://www.npmjs.com/package/env-paths) defaults will
be used.
As with the existing app, dev instances will write to `waveterm-dev`
directories, while all others will write to `waveterm`.
2024-10-22 18:26:58 +02:00
|
|
|
ipcMain.on("get-data-dir", (event) => {
|
|
|
|
event.returnValue = getWaveDataDir();
|
|
|
|
});
|
2024-10-22 01:51:18 +02:00
|
|
|
ipcMain.on("get-config-dir", (event) => {
|
Update data and config paths to match platform defaults (#1047)
Going forward for new installations, config and data files will be
stored at the platform default paths, as defined by
[env-paths](https://www.npmjs.com/package/env-paths).
For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME`
directory exists and contains valid data, it will be used. If this check
fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be
used. If these are not defined, then `XDG_DATA_HOME` and
`XDG_CONFIG_HOME` will be used. Finally, if none of these are defined,
the [env-paths](https://www.npmjs.com/package/env-paths) defaults will
be used.
As with the existing app, dev instances will write to `waveterm-dev`
directories, while all others will write to `waveterm`.
2024-10-22 18:26:58 +02:00
|
|
|
event.returnValue = getWaveConfigDir();
|
2024-10-22 01:51:18 +02:00
|
|
|
});
|
|
|
|
|
2024-08-22 00:04:39 +02:00
|
|
|
export {
|
|
|
|
getElectronAppBasePath,
|
2024-10-04 05:28:05 +02:00
|
|
|
getElectronAppUnpackedBasePath,
|
Update data and config paths to match platform defaults (#1047)
Going forward for new installations, config and data files will be
stored at the platform default paths, as defined by
[env-paths](https://www.npmjs.com/package/env-paths).
For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME`
directory exists and contains valid data, it will be used. If this check
fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be
used. If these are not defined, then `XDG_DATA_HOME` and
`XDG_CONFIG_HOME` will be used. Finally, if none of these are defined,
the [env-paths](https://www.npmjs.com/package/env-paths) defaults will
be used.
As with the existing app, dev instances will write to `waveterm-dev`
directories, while all others will write to `waveterm`.
2024-10-22 18:26:58 +02:00
|
|
|
getWaveConfigDir,
|
|
|
|
getWaveDataDir,
|
2024-08-22 00:04:39 +02:00
|
|
|
getWaveSrvCwd,
|
|
|
|
getWaveSrvPath,
|
|
|
|
isDev,
|
|
|
|
isDevVite,
|
|
|
|
unameArch,
|
|
|
|
unamePlatform,
|
Update data and config paths to match platform defaults (#1047)
Going forward for new installations, config and data files will be
stored at the platform default paths, as defined by
[env-paths](https://www.npmjs.com/package/env-paths).
For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME`
directory exists and contains valid data, it will be used. If this check
fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be
used. If these are not defined, then `XDG_DATA_HOME` and
`XDG_CONFIG_HOME` will be used. Finally, if none of these are defined,
the [env-paths](https://www.npmjs.com/package/env-paths) defaults will
be used.
As with the existing app, dev instances will write to `waveterm-dev`
directories, while all others will write to `waveterm`.
2024-10-22 18:26:58 +02:00
|
|
|
WaveConfigHomeVarName,
|
|
|
|
WaveDataHomeVarName,
|
2024-08-22 00:04:39 +02:00
|
|
|
};
|