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