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