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

DesktopEnvironment.ts

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