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

DesktopEnvironment.ts

278 lines9.8 KB

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;
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 readonly developmentDockIconPath: string;
86 }
87>()("@t3tools/desktop/app/DesktopEnvironment") {}
88​
89const APP_BASE_NAME = "T3 Code";
90​
91function resolveDesktopAppStageLabel(input: {
92 readonly isDevelopment: boolean;
93 readonly appVersion: string;
94}): DesktopAppStageLabel {
95 if (input.isDevelopment) {
96 return "Dev";
97 }
98​
99 return isNightlyDesktopVersion(input.appVersion) ? "Nightly" : "Alpha";
101​
102function resolveDesktopAppBranding(input: {
103 readonly isDevelopment: boolean;
104 readonly appVersion: string;
105}): DesktopAppBranding {
106 const stageLabel = resolveDesktopAppStageLabel(input);
107 return {
108 baseName: APP_BASE_NAME,
109 stageLabel,
110 displayName: `${APP_BASE_NAME} (${stageLabel})`,
111 };
113​
114function normalizeDesktopArch(arch: string): DesktopRuntimeArch {
115 if (arch === "arm64") return "arm64";
116 if (arch === "x64") return "x64";
117 return "other";
119​
120function resolveDesktopRuntimeInfo(input: {
121 readonly platform: NodeJS.Platform;
122 readonly processArch: string;
123 readonly runningUnderArm64Translation: boolean;
124}): DesktopRuntimeInfo {
125 const appArch = normalizeDesktopArch(input.processArch);
126​
127 if (input.platform !== "darwin") {
128 return {
129 hostArch: appArch,
130 appArch,
131 runningUnderArm64Translation: false,
132 };
133 }
134​
135 const hostArch = appArch === "arm64" || input.runningUnderArm64Translation ? "arm64" : appArch;
136​
137 return {
138 hostArch,
139 appArch,
140 runningUnderArm64Translation: input.runningUnderArm64Translation,
141 };
143​
144const make = Effect.fn("desktop.environment.make")(function* (
145 input: MakeDesktopEnvironmentInput,
146): Effect.fn.Return<DesktopEnvironment["Service"], Config.ConfigError, Path.Path> {
147 const path = yield* Path.Path;
148 const config = yield* DesktopConfig.DesktopConfig;
149 const homeDirectory = input.homeDirectory;
150 const devServerUrl = config.devServerUrl;
151 const isDevelopment = Option.isSome(devServerUrl);
152 const appDataDirectory =
153 input.platform === "win32"
154 ? Option.getOrElse(config.appDataDirectory, () =>
155 path.join(homeDirectory, "AppData", "Roaming"),
156 )
157 : input.platform === "darwin"
158 ? path.join(homeDirectory, "Library", "Application Support")
159 : Option.getOrElse(config.xdgConfigHome, () => path.join(homeDirectory, ".config"));
160 const baseDir = resolveDesktopBaseDir({
161 homeDirectory,
162 joinPath: path.join,
163 t3Home: config.t3Home,
164 });
165 const rootDir = path.resolve(input.dirname, "../../..");
166 const appRoot = input.isPackaged ? input.appPath : rootDir;
167 const serverRoot =
168 input.isPackaged && input.platform === "win32"
169 ? path.join(input.resourcesPath, "server.asar")
170 : appRoot;
171 const branding = resolveDesktopAppBranding({
172 isDevelopment,
173 appVersion: input.appVersion,
174 });
175 const displayName = branding.displayName;
176 const stateDir = resolveDesktopStateDir({
177 baseDir,
178 isDevelopment,
179 joinPath: path.join,
180 t3Home: config.t3Home,
181 });
182 const userDataDirName = isDevelopment ? "t3code-dev" : "t3code";
183 const legacyUserDataDirName = isDevelopment ? "T3 Code (Dev)" : "T3 Code (Alpha)";
184 const linuxApplicationsDir = path.join(
185 Option.getOrElse(config.xdgDataHome, () => path.join(homeDirectory, ".local", "share")),
186 "applications",
187 );
188 const resourcesPath = input.resourcesPath;
189​
190 return DesktopEnvironment.of({
191 path,
192 dirname: input.dirname,
193 platform: input.platform,
194 processArch: input.processArch,
195 isPackaged: input.isPackaged,
196 isDevelopment,
197 appVersion: input.appVersion,
198 appPath: input.appPath,
199 resourcesPath,
200 homeDirectory,
201 appDataDirectory,
202 baseDir,
203 stateDir,
204 desktopSettingsPath: path.join(stateDir, "desktop-settings.json"),
205 clientSettingsPath: path.join(stateDir, "client-settings.json"),
206 savedEnvironmentRegistryPath: path.join(stateDir, "saved-environments.json"),
207 serverSettingsPath: path.join(stateDir, "settings.json"),
208 logDir: path.join(stateDir, "logs"),
209 browserArtifactsDir: path.join(stateDir, "browser-artifacts"),
210 rootDir,
211 appRoot,
212 serverRoot,
213 backendEntryPath: path.join(serverRoot, "apps/server/dist/bin.mjs"),
214 backendCwd: input.isPackaged ? homeDirectory : appRoot,
215 preloadPath: path.join(input.dirname, "preload.cjs"),
216 appUpdateYmlPath: input.isPackaged
217 ? path.join(resourcesPath, "app-update.yml")
218 : path.join(input.appPath, "dev-app-update.yml"),
219 devServerUrl,
220 devRemoteT3ServerEntryPath: config.devRemoteT3ServerEntryPath,
221 configuredBackendPort: config.configuredBackendPort,
222 commitHashOverride: config.commitHashOverride,
223 otlpTracesUrl: config.otlpTracesUrl,
224 otlpExportIntervalMs: config.otlpExportIntervalMs,
225 branding,
226 displayName,
227 appUserModelId: Option.getOrElse(config.appUserModelIdOverride, () =>
228 isDevelopment ? "com.t3tools.t3code.dev" : "com.t3tools.t3code",
229 ),
230 linuxDesktopEntryName: isDevelopment ? "t3code-dev.desktop" : "t3code.desktop",
231 linuxWmClass: isDevelopment ? "t3code-dev" : "t3code",
232 linuxApplicationsDir,
233 appImagePath: config.appImagePath,
234 userDataDirName,
235 legacyUserDataDirName,
236 defaultDesktopSettings: DesktopAppSettings.resolveDefaultDesktopSettings(input.appVersion),
237 runtimeInfo: resolveDesktopRuntimeInfo({
238 platform: input.platform,
239 processArch: input.processArch,
240 runningUnderArm64Translation: input.runningUnderArm64Translation,
241 }),
242 resolvePickFolderDefaultPath: (rawOptions) => {
243 if (typeof rawOptions !== "object" || rawOptions === null) {
244 return Option.none();
245 }
246​
247 const { initialPath } = rawOptions as { initialPath?: unknown };
248 if (typeof initialPath !== "string") {
249 return Option.none();
250 }
251​
252 const trimmedPath = initialPath.trim();
253 if (trimmedPath.length === 0) {
254 return Option.none();
255 }
256​
257 if (trimmedPath === "~") {
258 return Option.some(homeDirectory);
259 }
260​
261 if (trimmedPath.startsWith("~/") || trimmedPath.startsWith("~\\")) {
262 return Option.some(path.join(homeDirectory, trimmedPath.slice(2)));
263 }
264​
265 return Option.some(path.resolve(trimmedPath));
266 },
267 resolveResourcePathCandidates: (fileName) => [
268 path.join(input.dirname, "../resources", fileName),
269 path.join(input.dirname, "../prod-resources", fileName),
270 path.join(resourcesPath, "resources", fileName),
271 path.join(resourcesPath, fileName),
272 ],
273 developmentDockIconPath: path.join(rootDir, "assets", "dev", "blueprint-macos-1024.png"),
274 });
275});
276​
277export const layer = (input: MakeDesktopEnvironmentInput) =>
278 Layer.effect(DesktopEnvironment, make(input));