Open History to see the last commit
1import type {
2 DesktopAppBranding,
3 DesktopAppStageLabel,
4 DesktopRuntimeArch,
5 DesktopRuntimeInfo,
6} from "@t3tools/contracts";
7import * as Config from "effect/Config";
8import * as Context from "effect/Context";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11import * as Option from "effect/Option";
12import * as Path from "effect/Path";
13
14import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
15import * as DesktopConfig from "./DesktopConfig.ts";
16import { resolveLinuxDesktopEntryName } from "./DesktopEarlyElectronStartup.ts";
17import { resolveDesktopBaseDir, resolveDesktopStateDir } from "./DesktopStatePaths.ts";
18import {
19 isNightlyDesktopVersion,
20 resolveDefaultDesktopUpdateChannel,
21} from "../updates/updateChannels.ts";
22import type { OtlpProtocol } from "@t3tools/shared/observability";
23
24export interface MakeDesktopEnvironmentInput {
25 readonly dirname: string;
26 readonly homeDirectory: string;
27 readonly platform: NodeJS.Platform;
28 readonly processArch: string;
29 readonly appVersion: string;
30 readonly appPath: string;
31 readonly isPackaged: boolean;
32 readonly resourcesPath: string;
33 readonly runningUnderArm64Translation: boolean;
34}
35
36export class DesktopEnvironment extends Context.Service<
37 DesktopEnvironment,
38 {
39 readonly path: Path.Path;
40 readonly dirname: string;
41 readonly platform: NodeJS.Platform;
42 readonly processArch: string;
43 readonly isPackaged: boolean;
44 readonly isDevelopment: boolean;
45 readonly appVersion: string;
46 readonly appPath: string;
47 readonly resourcesPath: string;
48 readonly homeDirectory: string;
49 readonly appDataDirectory: string;
50 readonly baseDir: string;
51 readonly stateDir: string;
52 readonly desktopSettingsPath: string;
53 readonly clientSettingsPath: string;
54 readonly savedEnvironmentRegistryPath: string;
55 readonly serverSettingsPath: string;
56 readonly logDir: string;
57 readonly browserArtifactsDir: string;
58 readonly rootDir: string;
59 readonly appRoot: string;
60 readonly serverRoot: string;
61 readonly backendEntryPath: string;
62 readonly clientAssetsDir: string;
63 readonly backendCwd: string;
64 readonly preloadPath: string;
65 readonly compileCachePath: string;
66 readonly appUpdateYmlPath: string;
67 readonly devServerUrl: Option.Option<URL>;
68 readonly devRemoteT3ServerEntryPath: Option.Option<string>;
69 readonly configuredBackendPort: Option.Option<number>;
70 readonly commitHashOverride: Option.Option<string>;
71 readonly otlpTracesUrl: Option.Option<string>;
72 readonly otlpMetricsUrl: Option.Option<string>;
73 readonly otlpLogsUrl: Option.Option<string>;
74 readonly otlpExportIntervalMs: number;
75 readonly otlpHeaders: Option.Option<Record<string, string>>;
76 readonly otlpProtocol: OtlpProtocol;
77 readonly branding: DesktopAppBranding;
78 readonly displayName: string;
79 readonly appUserModelId: string;
80 readonly linuxDesktopEntryName: string;
81 readonly linuxWmClass: string;
82 readonly linuxApplicationsDir: string;
83 readonly appImagePath: Option.Option<string>;
84 readonly defaultDesktopSettings: DesktopAppSettings.DesktopSettings;
85 readonly runtimeInfo: DesktopRuntimeInfo;
86 readonly resolvePickFolderDefaultPath: (rawOptions: unknown) => Option.Option<string>;
87 readonly resolveResourcePathCandidates: (fileName: string) => readonly string[];
88 }
89>()("@t3tools/desktop/app/DesktopEnvironment") {}
90
91const APP_BASE_NAME = "T3 Code";
92
93function resolveDesktopAppStageLabel(input: {
94 readonly isDevelopment: boolean;
95 readonly appVersion: string;
96}): DesktopAppStageLabel {
97 if (input.isDevelopment) {
98 return "Dev";
99 }
100
101 return isNightlyDesktopVersion(input.appVersion) ? "Nightly" : "Beta";
102}
103
104export function resolveDesktopAppBranding(input: {
105 readonly isDevelopment: boolean;
106 readonly appVersion: string;
107}): DesktopAppBranding {
108 const stageLabel = resolveDesktopAppStageLabel(input);
109 return {
110 baseName: APP_BASE_NAME,
111 stageLabel,
112 displayName: `${APP_BASE_NAME} (${stageLabel})`,
113 };
114}
115
116// Keep Chromium's encryption identity on the names shipped before branding changed.
117export function resolveDesktopSafeStorageName(appVersion: string): string {
118 return resolveDefaultDesktopUpdateChannel(appVersion) === "nightly"
119 ? "T3 Code (Nightly)"
120 : "T3 Code (Alpha)";
121}
122
123function normalizeDesktopArch(arch: string): DesktopRuntimeArch {
124 if (arch === "arm64") return "arm64";
125 if (arch === "x64") return "x64";
126 return "other";
127}
128
129function resolveDesktopRuntimeInfo(input: {
130 readonly platform: NodeJS.Platform;
131 readonly processArch: string;
132 readonly runningUnderArm64Translation: boolean;
133}): DesktopRuntimeInfo {
134 const appArch = normalizeDesktopArch(input.processArch);
135
136 if (input.platform !== "darwin") {
137 return {
138 hostArch: appArch,
139 appArch,
140 runningUnderArm64Translation: false,
141 };
142 }
143
144 const hostArch = appArch === "arm64" || input.runningUnderArm64Translation ? "arm64" : appArch;
145
146 return {
147 hostArch,
148 appArch,
149 runningUnderArm64Translation: input.runningUnderArm64Translation,
150 };
151}
152
153const make = Effect.fn("desktop.environment.make")(function* (
154 input: MakeDesktopEnvironmentInput,
155): Effect.fn.Return<DesktopEnvironment["Service"], Config.ConfigError, Path.Path> {
156 const path = yield* Path.Path;
157 const config = yield* DesktopConfig.DesktopConfig;
158 const homeDirectory = input.homeDirectory;
159 const devServerUrl = config.devServerUrl;
160 const isDevelopment = Option.isSome(devServerUrl);
161 const appDataDirectory =
162 input.platform === "win32"
163 ? Option.getOrElse(config.appDataDirectory, () =>
164 path.join(homeDirectory, "AppData", "Roaming"),
165 )
166 : input.platform === "darwin"
167 ? path.join(homeDirectory, "Library", "Application Support")
168 : Option.getOrElse(config.xdgConfigHome, () => path.join(homeDirectory, ".config"));
169 const baseDir = resolveDesktopBaseDir({
170 homeDirectory,
171 joinPath: path.join,
172 t3Home: config.t3Home,
173 });
174 const rootDir = path.resolve(input.dirname, "../../..");
175 const appRoot = input.isPackaged ? input.appPath : rootDir;
176 const serverRoot =
177 input.isPackaged && input.platform === "win32"
178 ? path.join(input.resourcesPath, "server.asar")
179 : appRoot;
180 const branding = resolveDesktopAppBranding({
181 isDevelopment,
182 appVersion: input.appVersion,
183 });
184 const displayName = branding.displayName;
185 const stateDir = resolveDesktopStateDir({
186 baseDir,
187 isDevelopment,
188 joinPath: path.join,
189 t3Home: config.t3Home,
190 });
191 const linuxApplicationsDir = path.join(
192 Option.getOrElse(config.xdgDataHome, () => path.join(homeDirectory, ".local", "share")),
193 "applications",
194 );
195 const resourcesPath = input.resourcesPath;
196
197 return DesktopEnvironment.of({
198 path,
199 dirname: input.dirname,
200 platform: input.platform,
201 processArch: input.processArch,
202 isPackaged: input.isPackaged,
203 isDevelopment,
204 appVersion: input.appVersion,
205 appPath: input.appPath,
206 resourcesPath,
207 homeDirectory,
208 appDataDirectory,
209 baseDir,
210 stateDir,
211 desktopSettingsPath: path.join(stateDir, "desktop-settings.json"),
212 clientSettingsPath: path.join(stateDir, "client-settings.json"),
213 savedEnvironmentRegistryPath: path.join(stateDir, "saved-environments.json"),
214 serverSettingsPath: path.join(stateDir, "settings.json"),
215 logDir: path.join(stateDir, "logs"),
216 browserArtifactsDir: path.join(stateDir, "browser-artifacts"),
217 rootDir,
218 appRoot,
219 serverRoot,
220 backendEntryPath: path.join(serverRoot, "apps/server/dist/bin.mjs"),
221 clientAssetsDir: path.join(serverRoot, "apps/server/dist/client"),
222 backendCwd: input.isPackaged ? homeDirectory : appRoot,
223 preloadPath: path.join(input.dirname, "preload.cjs"),
224 compileCachePath: path.join(input.dirname, "compileCache.cjs"),
225 appUpdateYmlPath: input.isPackaged
226 ? path.join(resourcesPath, "app-update.yml")
227 : path.join(input.appPath, "dev-app-update.yml"),
228 devServerUrl,
229 devRemoteT3ServerEntryPath: config.devRemoteT3ServerEntryPath,
230 configuredBackendPort: config.configuredBackendPort,
231 commitHashOverride: config.commitHashOverride,
232 otlpTracesUrl: config.otlpTracesUrl,
233 otlpMetricsUrl: config.otlpMetricsUrl,
234 otlpLogsUrl: config.otlpLogsUrl,
235 otlpExportIntervalMs: config.otlpExportIntervalMs,
236 otlpHeaders: config.otlpHeaders,
237 otlpProtocol: config.otlpProtocol,
238 branding,
239 displayName,
240 appUserModelId: Option.getOrElse(config.appUserModelIdOverride, () =>
241 isDevelopment ? "com.t3tools.t3code.dev" : "com.t3tools.t3code",
242 ),
243 linuxDesktopEntryName: resolveLinuxDesktopEntryName(isDevelopment),
244 linuxWmClass: isDevelopment ? "t3code-dev" : "t3code",
245 linuxApplicationsDir,
246 appImagePath: config.appImagePath,
247 defaultDesktopSettings: DesktopAppSettings.resolveDefaultDesktopSettings(input.appVersion),
248 runtimeInfo: resolveDesktopRuntimeInfo({
249 platform: input.platform,
250 processArch: input.processArch,
251 runningUnderArm64Translation: input.runningUnderArm64Translation,
252 }),
253 resolvePickFolderDefaultPath: (rawOptions) => {
254 if (typeof rawOptions !== "object" || rawOptions === null) {
255 return Option.none();
256 }
257
258 const { initialPath } = rawOptions as { initialPath?: unknown };
259 if (typeof initialPath !== "string") {
260 return Option.none();
261 }
262
263 const trimmedPath = initialPath.trim();
264 if (trimmedPath.length === 0) {
265 return Option.none();
266 }
267
268 if (trimmedPath === "~") {
269 return Option.some(homeDirectory);
270 }
271
272 if (trimmedPath.startsWith("~/") || trimmedPath.startsWith("~\\")) {
273 return Option.some(path.join(homeDirectory, trimmedPath.slice(2)));
274 }
275
276 return Option.some(path.resolve(trimmedPath));
277 },
278 resolveResourcePathCandidates: (fileName) => [
279 path.join(input.dirname, "../resources", fileName),
280 path.join(input.dirname, "../prod-resources", fileName),
281 path.join(resourcesPath, "resources", fileName),
282 path.join(resourcesPath, fileName),
283 ],
284 });
285});
286
287export const layer = (input: MakeDesktopEnvironmentInput) =>
288 Layer.effect(DesktopEnvironment, make(input));