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