Co-authored-by: codex <[email protected]> Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: cursor[bot] <206951365+cursor[bot]@users.noreply.github.com>
52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import * as FS from "node:fs";
|
|
import * as Path from "node:path";
|
|
import type { DesktopServerExposureMode } from "@t3tools/contracts";
|
|
|
|
export interface DesktopSettings {
|
|
readonly serverExposureMode: DesktopServerExposureMode;
|
|
}
|
|
|
|
export const DEFAULT_DESKTOP_SETTINGS: DesktopSettings = {
|
|
serverExposureMode: "local-only",
|
|
};
|
|
|
|
export function setDesktopServerExposurePreference(
|
|
settings: DesktopSettings,
|
|
requestedMode: DesktopServerExposureMode,
|
|
): DesktopSettings {
|
|
return settings.serverExposureMode === requestedMode
|
|
? settings
|
|
: {
|
|
...settings,
|
|
serverExposureMode: requestedMode,
|
|
};
|
|
}
|
|
|
|
export function readDesktopSettings(settingsPath: string): DesktopSettings {
|
|
try {
|
|
if (!FS.existsSync(settingsPath)) {
|
|
return DEFAULT_DESKTOP_SETTINGS;
|
|
}
|
|
|
|
const raw = FS.readFileSync(settingsPath, "utf8");
|
|
const parsed = JSON.parse(raw) as {
|
|
readonly serverExposureMode?: unknown;
|
|
};
|
|
|
|
return {
|
|
serverExposureMode:
|
|
parsed.serverExposureMode === "network-accessible" ? "network-accessible" : "local-only",
|
|
};
|
|
} catch {
|
|
return DEFAULT_DESKTOP_SETTINGS;
|
|
}
|
|
}
|
|
|
|
export function writeDesktopSettings(settingsPath: string, settings: DesktopSettings): void {
|
|
const directory = Path.dirname(settingsPath);
|
|
const tempPath = `${settingsPath}.${process.pid}.${Date.now()}.tmp`;
|
|
FS.mkdirSync(directory, { recursive: true });
|
|
FS.writeFileSync(tempPath, `${JSON.stringify(settings, null, 2)}\n`, "utf8");
|
|
FS.renameSync(tempPath, settingsPath);
|
|
}
|