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