Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DesktopConfig.ts

58 lines2.3 KB

1import * as Config from "effect/Config";
2import * as ConfigProvider from "effect/ConfigProvider";
3import * as Option from "effect/Option";
4​
5const trimNonEmptyOption = (value: string): Option.Option<string> => {
6 const trimmed = value.trim();
7 return trimmed.length > 0 ? Option.some(trimmed) : Option.none();
8};
9​
10const trimmedString = (name: string) =>
11 Config.string(name).pipe(Config.option, Config.map(Option.flatMap(trimNonEmptyOption)));
12​
13const optionalBoolean = (name: string) =>
14 Config.boolean(name).pipe(Config.option, Config.map(Option.getOrElse(() => false)));
15​
16const commaSeparatedStrings = (name: string) =>
17 trimmedString(name).pipe(
18 Config.map(
19 Option.match({
20 onNone: () => [],
21 onSome: (value) =>
22 value
23 .split(",")
24 .map((entry) => entry.trim())
25 .filter((entry) => entry.length > 0),
26 }),
27 ),
28 );
29​
30const compactEnv = (env: Readonly<Record<string, string | undefined>>): Record<string, string> =>
31 Object.fromEntries(
32 Object.entries(env).filter((entry): entry is [string, string] => entry[1] !== undefined),
33 );
34​
35export const DesktopConfig = Config.all({
36 appDataDirectory: trimmedString("APPDATA"),
37 xdgConfigHome: trimmedString("XDG_CONFIG_HOME"),
38 t3Home: trimmedString("T3CODE_HOME"),
39 devServerUrl: Config.url("VITE_DEV_SERVER_URL").pipe(Config.option),
40 devRemoteT3ServerEntryPath: trimmedString("T3CODE_DEV_REMOTE_T3_SERVER_ENTRY_PATH"),
41 configuredBackendPort: Config.port("T3CODE_PORT").pipe(Config.option),
42 commitHashOverride: trimmedString("T3CODE_COMMIT_HASH"),
43 desktopLanHostOverride: trimmedString("T3CODE_DESKTOP_LAN_HOST"),
44 desktopHttpsEndpointUrls: commaSeparatedStrings("T3CODE_DESKTOP_HTTPS_ENDPOINTS"),
45 otlpTracesUrl: trimmedString("T3CODE_OTLP_TRACES_URL"),
46 otlpExportIntervalMs: Config.int("T3CODE_OTLP_EXPORT_INTERVAL_MS").pipe(
47 Config.withDefault(10_000),
48 ),
49 appImagePath: trimmedString("APPIMAGE"),
50 disableAutoUpdate: optionalBoolean("T3CODE_DISABLE_AUTO_UPDATE"),
51 mockUpdates: optionalBoolean("T3CODE_DESKTOP_MOCK_UPDATES"),
52 mockUpdateServerPort: Config.port("T3CODE_DESKTOP_MOCK_UPDATE_SERVER_PORT").pipe(
53 Config.withDefault(3000),
54 ),
55});
56​
57export const layerTest = (env: Readonly<Record<string, string | undefined>>) =>
58 ConfigProvider.layer(ConfigProvider.fromEnv({ env: compactEnv(env) }));