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