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 {
15 type DesktopSettings,
16 resolveDefaultDesktopSettings,
17} from "../settings/DesktopAppSettings.ts";
18import * as DesktopConfig from "./DesktopConfig.ts";
19import { isNightlyDesktopVersion } from "../updates/updateChannels.ts";
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 interface DesktopEnvironmentShape {
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 rootDir: string;
53 readonly appRoot: string;
54 readonly backendEntryPath: string;
55 readonly backendCwd: string;
56 readonly preloadPath: string;
57 readonly appUpdateYmlPath: string;
58 readonly devServerUrl: Option.Option<URL>;
59 readonly devRemoteT3ServerEntryPath: Option.Option<string>;
60 readonly configuredBackendPort: Option.Option<number>;
61 readonly commitHashOverride: Option.Option<string>;
62 readonly otlpTracesUrl: Option.Option<string>;
63 readonly otlpExportIntervalMs: number;
64 readonly branding: DesktopAppBranding;
65 readonly displayName: string;
66 readonly appUserModelId: string;
67 readonly linuxDesktopEntryName: string;
68 readonly linuxWmClass: string;
69 readonly userDataDirName: string;
70 readonly legacyUserDataDirName: string;
71 readonly defaultDesktopSettings: DesktopSettings;
72 readonly runtimeInfo: DesktopRuntimeInfo;
73 readonly resolvePickFolderDefaultPath: (rawOptions: unknown) => Option.Option<string>;
74 readonly resolveResourcePathCandidates: (fileName: string) => readonly string[];
75 readonly developmentDockIconPath: string;
76}
77
78export class DesktopEnvironment extends Context.Service<
79 DesktopEnvironment,
80 DesktopEnvironmentShape
81>()("t3/desktop/Environment") {}
82
83const APP_BASE_NAME = "T3 Code";
84
85function resolveDesktopAppStageLabel(input: {
86 readonly isDevelopment: boolean;
87 readonly appVersion: string;
88}): DesktopAppStageLabel {
89 if (input.isDevelopment) {
90 return "Dev";
91 }
92
93 return isNightlyDesktopVersion(input.appVersion) ? "Nightly" : "Bscholar";
94}
95
96function resolveDesktopAppBranding(input: {
97 readonly isDevelopment: boolean;
98 readonly appVersion: string;
99}): DesktopAppBranding {
100 const stageLabel = resolveDesktopAppStageLabel(input);
101 return {
102 baseName: APP_BASE_NAME,
103 stageLabel,
104 displayName: `${APP_BASE_NAME} (${stageLabel})`,
105 };
106}
107
108function normalizeDesktopArch(arch: string): DesktopRuntimeArch {
109 if (arch === "arm64") return "arm64";
110 if (arch === "x64") return "x64";
111 return "other";
112}
113
114function resolveDesktopRuntimeInfo(input: {
115 readonly platform: NodeJS.Platform;
116 readonly processArch: string;
117 readonly runningUnderArm64Translation: boolean;
118}): DesktopRuntimeInfo {
119 const appArch = normalizeDesktopArch(input.processArch);
120
121 if (input.platform !== "darwin") {
122 return {
123 hostArch: appArch,
124 appArch,
125 runningUnderArm64Translation: false,
126 };
127 }
128
129 const hostArch = appArch === "arm64" || input.runningUnderArm64Translation ? "arm64" : appArch;
130
131 return {
132 hostArch,
133 appArch,
134 runningUnderArm64Translation: input.runningUnderArm64Translation,
135 };
136}
137
138const makeDesktopEnvironment = Effect.fn("desktop.environment.make")(function* (
139 input: MakeDesktopEnvironmentInput,
140): Effect.fn.Return<DesktopEnvironmentShape, Config.ConfigError, Path.Path> {
141 const path = yield* Path.Path;
142 const config = yield* DesktopConfig.DesktopConfig;
143 const homeDirectory = input.homeDirectory;
144 const devServerUrl = config.devServerUrl;
145 const isDevelopment = Option.isSome(devServerUrl);
146 const appDataDirectory =
147 input.platform === "win32"
148 ? Option.getOrElse(config.appDataDirectory, () =>
149 path.join(homeDirectory, "AppData", "Roaming"),
150 )
151 : input.platform === "darwin"
152 ? path.join(homeDirectory, "Library", "Application Support")
153 : Option.getOrElse(config.xdgConfigHome, () => path.join(homeDirectory, ".config"));
154 const baseDir = Option.getOrElse(config.t3Home, () => path.join(homeDirectory, ".t3"));
155 const rootDir = path.resolve(input.dirname, "../../..");
156 const appRoot = input.isPackaged ? input.appPath : rootDir;
157 const branding = resolveDesktopAppBranding({
158 isDevelopment,
159 appVersion: input.appVersion,
160 });
161 const displayName = branding.displayName;
162 const stateDir = path.join(baseDir, isDevelopment ? "dev" : "userdata");
163 const userDataDirName = isDevelopment ? "t3code-dev" : "t3code";
164 const legacyUserDataDirName = isDevelopment ? "T3 Code (Dev)" : "T3 Code (Bscholar)";
165 const resourcesPath = input.resourcesPath;
166
167 return DesktopEnvironment.of({
168 path,
169 dirname: input.dirname,
170 platform: input.platform,
171 processArch: input.processArch,
172 isPackaged: input.isPackaged,
173 isDevelopment,
174 appVersion: input.appVersion,
175 appPath: input.appPath,
176 resourcesPath,
177 homeDirectory,
178 appDataDirectory,
179 baseDir,
180 stateDir,
181 desktopSettingsPath: path.join(stateDir, "desktop-settings.json"),
182 clientSettingsPath: path.join(stateDir, "client-settings.json"),
183 savedEnvironmentRegistryPath: path.join(stateDir, "saved-environments.json"),
184 serverSettingsPath: path.join(stateDir, "settings.json"),
185 logDir: path.join(stateDir, "logs"),
186 rootDir,
187 appRoot,
188 backendEntryPath: path.join(appRoot, "apps/server/dist/bin.mjs"),
189 backendCwd: input.isPackaged ? homeDirectory : appRoot,
190 preloadPath: path.join(input.dirname, "preload.cjs"),
191 appUpdateYmlPath: input.isPackaged
192 ? path.join(resourcesPath, "app-update.yml")
193 : path.join(input.appPath, "dev-app-update.yml"),
194 devServerUrl,
195 devRemoteT3ServerEntryPath: config.devRemoteT3ServerEntryPath,
196 configuredBackendPort: config.configuredBackendPort,
197 commitHashOverride: config.commitHashOverride,
198 otlpTracesUrl: config.otlpTracesUrl,
199 otlpExportIntervalMs: config.otlpExportIntervalMs,
200 branding,
201 displayName,
202 appUserModelId: isDevelopment ? "com.t3tools.t3code.dev" : "com.t3tools.t3code",
203 linuxDesktopEntryName: isDevelopment ? "t3code-dev.desktop" : "t3code.desktop",
204 linuxWmClass: isDevelopment ? "t3code-dev" : "t3code",
205 userDataDirName,
206 legacyUserDataDirName,
207 defaultDesktopSettings: resolveDefaultDesktopSettings(input.appVersion),
208 runtimeInfo: resolveDesktopRuntimeInfo({
209 platform: input.platform,
210 processArch: input.processArch,
211 runningUnderArm64Translation: input.runningUnderArm64Translation,
212 }),
213 resolvePickFolderDefaultPath: (rawOptions) => {
214 if (typeof rawOptions !== "object" || rawOptions === null) {
215 return Option.none();
216 }
217
218 const { initialPath } = rawOptions as { initialPath?: unknown };
219 if (typeof initialPath !== "string") {
220 return Option.none();
221 }
222
223 const trimmedPath = initialPath.trim();
224 if (trimmedPath.length === 0) {
225 return Option.none();
226 }
227
228 if (trimmedPath === "~") {
229 return Option.some(homeDirectory);
230 }
231
232 if (trimmedPath.startsWith("~/") || trimmedPath.startsWith("~\\")) {
233 return Option.some(path.join(homeDirectory, trimmedPath.slice(2)));
234 }
235
236 return Option.some(path.resolve(trimmedPath));
237 },
238 resolveResourcePathCandidates: (fileName) => [
239 path.join(input.dirname, "../resources", fileName),
240 path.join(input.dirname, "../prod-resources", fileName),
241 path.join(resourcesPath, "resources", fileName),
242 path.join(resourcesPath, fileName),
243 ],
244 developmentDockIconPath: path.join(rootDir, "assets", "dev", "blueprint-macos-1024.png"),
245 });
246});
247
248export const layer = (input: MakeDesktopEnvironmentInput) =>
249 Layer.effect(DesktopEnvironment, makeDesktopEnvironment(input));