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

DesktopEnvironment.ts

248 lines8.7 KB

1import type {
2 DesktopAppBranding,
3 DesktopAppStageLabel,
4 DesktopRuntimeArch,
5 DesktopRuntimeInfo,
6} from "@v12/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 devRemoteV12ServerEntryPath: 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>()("@v12/desktop/app/DesktopEnvironment") {}
78​
79const APP_BASE_NAME = "V12";
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 baseDir = Option.getOrElse(config.v12Home, () => path.join(homeDirectory, ".v12"));
151 const rootDir = path.resolve(input.dirname, "../../..");
152 const appRoot = input.isPackaged ? input.appPath : rootDir;
153 const branding = resolveDesktopAppBranding({
154 isDevelopment,
155 appVersion: input.appVersion,
156 });
157 const displayName = branding.displayName;
158 const stateDir = path.join(baseDir, isDevelopment ? "dev" : "userdata");
159 const userDataDirName = isDevelopment ? "v12-dev" : "v12";
160 const legacyUserDataDirName = isDevelopment ? "V12 (Dev)" : "V12 (Alpha)";
161 const resourcesPath = input.resourcesPath;
162​
163 return DesktopEnvironment.of({
164 path,
165 dirname: input.dirname,
166 platform: input.platform,
167 processArch: input.processArch,
168 isPackaged: input.isPackaged,
169 isDevelopment,
170 appVersion: input.appVersion,
171 appPath: input.appPath,
172 resourcesPath,
173 homeDirectory,
174 appDataDirectory,
175 baseDir,
176 stateDir,
177 desktopSettingsPath: path.join(stateDir, "desktop-settings.json"),
178 clientSettingsPath: path.join(stateDir, "client-settings.json"),
179 savedEnvironmentRegistryPath: path.join(stateDir, "saved-environments.json"),
180 serverSettingsPath: path.join(stateDir, "settings.json"),
181 logDir: path.join(stateDir, "logs"),
182 browserArtifactsDir: path.join(stateDir, "browser-artifacts"),
183 rootDir,
184 appRoot,
185 backendEntryPath: path.join(appRoot, "apps/server/dist/bin.mjs"),
186 backendCwd: input.isPackaged ? homeDirectory : appRoot,
187 preloadPath: path.join(input.dirname, "preload.cjs"),
188 appUpdateYmlPath: input.isPackaged
189 ? path.join(resourcesPath, "app-update.yml")
190 : path.join(input.appPath, "dev-app-update.yml"),
191 devServerUrl,
192 devRemoteV12ServerEntryPath: config.devRemoteV12ServerEntryPath,
193 configuredBackendPort: config.configuredBackendPort,
194 commitHashOverride: config.commitHashOverride,
195 otlpTracesUrl: config.otlpTracesUrl,
196 otlpExportIntervalMs: config.otlpExportIntervalMs,
197 branding,
198 displayName,
199 appUserModelId: Option.getOrElse(config.appUserModelIdOverride, () =>
200 isDevelopment ? "com.v12.v12.dev" : "com.v12.v12",
201 ),
202 linuxDesktopEntryName: isDevelopment ? "v12-dev.desktop" : "v12.desktop",
203 linuxWmClass: isDevelopment ? "v12-dev" : "v12",
204 userDataDirName,
205 legacyUserDataDirName,
206 defaultDesktopSettings: DesktopAppSettings.resolveDefaultDesktopSettings(input.appVersion),
207 runtimeInfo: resolveDesktopRuntimeInfo({
208 platform: input.platform,
209 processArch: input.processArch,
210 runningUnderArm64Translation: input.runningUnderArm64Translation,
211 }),
212 resolvePickFolderDefaultPath: (rawOptions) => {
213 if (typeof rawOptions !== "object" || rawOptions === null) {
214 return Option.none();
215 }
216​
217 const { initialPath } = rawOptions as { initialPath?: unknown };
218 if (typeof initialPath !== "string") {
219 return Option.none();
220 }
221​
222 const trimmedPath = initialPath.trim();
223 if (trimmedPath.length === 0) {
224 return Option.none();
225 }
226​
227 if (trimmedPath === "~") {
228 return Option.some(homeDirectory);
229 }
230​
231 if (trimmedPath.startsWith("~/") || trimmedPath.startsWith("~\\")) {
232 return Option.some(path.join(homeDirectory, trimmedPath.slice(2)));
233 }
234​
235 return Option.some(path.resolve(trimmedPath));
236 },
237 resolveResourcePathCandidates: (fileName) => [
238 path.join(input.dirname, "../resources", fileName),
239 path.join(input.dirname, "../prod-resources", fileName),
240 path.join(resourcesPath, "resources", fileName),
241 path.join(resourcesPath, fileName),
242 ],
243 developmentDockIconPath: path.join(rootDir, "assets", "dev", "blueprint-macos-1024.png"),
244 });
245});
246​
247export const layer = (input: MakeDesktopEnvironmentInput) =>
248 Layer.effect(DesktopEnvironment, make(input));