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