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

DesktopEnvironment.ts

252 lines8.9 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 { 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;
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 userDataDirName: string;
70 readonly legacyUserDataDirName: string;
71 readonly defaultDesktopSettings: DesktopAppSettings.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>()("@t3tools/desktop/app/DesktopEnvironment") {}
78​
79const APP_BASE_NAME = "T3 Code";
80​
81function resolveDesktopAppStageLabel(input: {
82 readonly isDevelopment: boolean;
83 readonly appVersion: string;
84}): DesktopAppStageLabel {
85 if (input.isDevelopment) {
86 return "Dev";
87 }
88​
89 return isNightlyDesktopVersion(input.appVersion) ? "Nightly" : "Alpha";
91​
92function resolveDesktopAppBranding(input: {
93 readonly isDevelopment: boolean;
94 readonly appVersion: string;
95}): DesktopAppBranding {
96 const stageLabel = resolveDesktopAppStageLabel(input);
97 return {
98 baseName: APP_BASE_NAME,
99 stageLabel,
100 displayName: `${APP_BASE_NAME} (${stageLabel})`,
101 };
103​
104function normalizeDesktopArch(arch: string): DesktopRuntimeArch {
105 if (arch === "arm64") return "arm64";
106 if (arch === "x64") return "x64";
107 return "other";
109​
110function resolveDesktopRuntimeInfo(input: {
111 readonly platform: NodeJS.Platform;
112 readonly processArch: string;
113 readonly runningUnderArm64Translation: boolean;
114}): DesktopRuntimeInfo {
115 const appArch = normalizeDesktopArch(input.processArch);
116​
117 if (input.platform !== "darwin") {
118 return {
119 hostArch: appArch,
120 appArch,
121 runningUnderArm64Translation: false,
122 };
123 }
124​
125 const hostArch = appArch === "arm64" || input.runningUnderArm64Translation ? "arm64" : appArch;
126​
127 return {
128 hostArch,
129 appArch,
130 runningUnderArm64Translation: input.runningUnderArm64Translation,
131 };
133​
134const make = Effect.fn("desktop.environment.make")(function* (
135 input: MakeDesktopEnvironmentInput,
136): Effect.fn.Return<DesktopEnvironment["Service"], Config.ConfigError, Path.Path> {
137 const path = yield* Path.Path;
138 const config = yield* DesktopConfig.DesktopConfig;
139 const homeDirectory = input.homeDirectory;
140 const devServerUrl = config.devServerUrl;
141 const isDevelopment = Option.isSome(devServerUrl);
142 const appDataDirectory =
143 input.platform === "win32"
144 ? Option.getOrElse(config.appDataDirectory, () =>
145 path.join(homeDirectory, "AppData", "Roaming"),
146 )
147 : input.platform === "darwin"
148 ? path.join(homeDirectory, "Library", "Application Support")
149 : Option.getOrElse(config.xdgConfigHome, () => path.join(homeDirectory, ".config"));
150 const configuredBaseDir = config.t3Home;
151 const baseDir = Option.getOrElse(configuredBaseDir, () => path.join(homeDirectory, ".t3"));
152 const rootDir = path.resolve(input.dirname, "../../..");
153 const appRoot = input.isPackaged ? input.appPath : rootDir;
154 const branding = resolveDesktopAppBranding({
155 isDevelopment,
156 appVersion: input.appVersion,
157 });
158 const displayName = branding.displayName;
159 const stateDir = path.join(
160 baseDir,
161 isDevelopment && Option.isNone(configuredBaseDir) ? "dev" : "userdata",
162 );
163 const userDataDirName = isDevelopment ? "t3code-dev" : "t3code";
164 const legacyUserDataDirName = isDevelopment ? "T3 Code (Dev)" : "T3 Code (Alpha)";
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 browserArtifactsDir: path.join(stateDir, "browser-artifacts"),
187 rootDir,
188 appRoot,
189 backendEntryPath: path.join(appRoot, "apps/server/dist/bin.mjs"),
190 backendCwd: input.isPackaged ? homeDirectory : appRoot,
191 preloadPath: path.join(input.dirname, "preload.cjs"),
192 appUpdateYmlPath: input.isPackaged
193 ? path.join(resourcesPath, "app-update.yml")
194 : path.join(input.appPath, "dev-app-update.yml"),
195 devServerUrl,
196 devRemoteT3ServerEntryPath: config.devRemoteT3ServerEntryPath,
197 configuredBackendPort: config.configuredBackendPort,
198 commitHashOverride: config.commitHashOverride,
199 otlpTracesUrl: config.otlpTracesUrl,
200 otlpExportIntervalMs: config.otlpExportIntervalMs,
201 branding,
202 displayName,
203 appUserModelId: Option.getOrElse(config.appUserModelIdOverride, () =>
204 isDevelopment ? "com.t3tools.t3code.dev" : "com.t3tools.t3code",
205 ),
206 linuxDesktopEntryName: isDevelopment ? "t3code-dev.desktop" : "t3code.desktop",
207 linuxWmClass: isDevelopment ? "t3code-dev" : "t3code",
208 userDataDirName,
209 legacyUserDataDirName,
210 defaultDesktopSettings: DesktopAppSettings.resolveDefaultDesktopSettings(input.appVersion),
211 runtimeInfo: resolveDesktopRuntimeInfo({
212 platform: input.platform,
213 processArch: input.processArch,
214 runningUnderArm64Translation: input.runningUnderArm64Translation,
215 }),
216 resolvePickFolderDefaultPath: (rawOptions) => {
217 if (typeof rawOptions !== "object" || rawOptions === null) {
218 return Option.none();
219 }
220​
221 const { initialPath } = rawOptions as { initialPath?: unknown };
222 if (typeof initialPath !== "string") {
223 return Option.none();
224 }
225​
226 const trimmedPath = initialPath.trim();
227 if (trimmedPath.length === 0) {
228 return Option.none();
229 }
230​
231 if (trimmedPath === "~") {
232 return Option.some(homeDirectory);
233 }
234​
235 if (trimmedPath.startsWith("~/") || trimmedPath.startsWith("~\\")) {
236 return Option.some(path.join(homeDirectory, trimmedPath.slice(2)));
237 }
238​
239 return Option.some(path.resolve(trimmedPath));
240 },
241 resolveResourcePathCandidates: (fileName) => [
242 path.join(input.dirname, "../resources", fileName),
243 path.join(input.dirname, "../prod-resources", fileName),
244 path.join(resourcesPath, "resources", fileName),
245 path.join(resourcesPath, fileName),
246 ],
247 developmentDockIconPath: path.join(rootDir, "assets", "dev", "blueprint-macos-1024.png"),
248 });
249});
250​
251export const layer = (input: MakeDesktopEnvironmentInput) =>
252 Layer.effect(DesktopEnvironment, make(input));