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

DesktopAppIdentity.ts

128 lines4.5 KB

1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import * as Ref from "effect/Ref";
7import * as Schema from "effect/Schema";
8​
9import * as ElectronApp from "../electron/ElectronApp.ts";
10import * as DesktopAssets from "./DesktopAssets.ts";
11import * as DesktopEnvironment from "./DesktopEnvironment.ts";
12​
13const COMMIT_HASH_PATTERN = /^[0-9a-f]{7,40}$/i;
14const COMMIT_HASH_DISPLAY_LENGTH = 12;
15​
16const AppPackageMetadata = Schema.Struct({
17 t3codeCommitHash: Schema.optional(Schema.String),
18});
19const decodeAppPackageMetadata = Schema.decodeEffect(Schema.fromJsonString(AppPackageMetadata));
20​
21export interface DesktopAppIdentityShape {
22 readonly resolveUserDataPath: Effect.Effect<string>;
23 readonly configure: Effect.Effect<void>;
25​
26export class DesktopAppIdentity extends Context.Service<
27 DesktopAppIdentity,
28 DesktopAppIdentityShape
29>()("t3/desktop/AppIdentity") {}
30​
31const normalizeCommitHash = (value: string): Option.Option<string> => {
32 const trimmed = value.trim();
33 return COMMIT_HASH_PATTERN.test(trimmed)
34 ? Option.some(trimmed.slice(0, COMMIT_HASH_DISPLAY_LENGTH).toLowerCase())
35 : Option.none();
36};
37​
38const make = Effect.gen(function* () {
39 const assets = yield* DesktopAssets.DesktopAssets;
40 const electronApp = yield* ElectronApp.ElectronApp;
41 const environment = yield* DesktopEnvironment.DesktopEnvironment;
42 const fileSystem = yield* FileSystem.FileSystem;
43 const commitHashCache = yield* Ref.make<Option.Option<Option.Option<string>>>(Option.none());
44​
45 const resolveEmbeddedCommitHash = Effect.gen(function* () {
46 const packageJsonPath = environment.path.join(environment.appRoot, "package.json");
47 const raw = yield* fileSystem.readFileString(packageJsonPath).pipe(Effect.option);
48 return yield* Option.match(raw, {
49 onNone: () => Effect.succeed(Option.none<string>()),
50 onSome: (value) =>
51 decodeAppPackageMetadata(value).pipe(
52 Effect.map((parsed) =>
53 Option.fromNullishOr(parsed.t3codeCommitHash).pipe(Option.flatMap(normalizeCommitHash)),
54 ),
55 Effect.catch(() => Effect.succeed(Option.none<string>())),
56 ),
57 });
58 });
59​
60 const resolveAboutCommitHash = Effect.gen(function* () {
61 const cached = yield* Ref.get(commitHashCache);
62 if (Option.isSome(cached)) {
63 return cached.value;
64 }
65​
66 const override = Option.flatMap(environment.commitHashOverride, normalizeCommitHash);
67 if (Option.isSome(override)) {
68 yield* Ref.set(commitHashCache, Option.some(override));
69 return override;
70 }
71​
72 if (!environment.isPackaged) {
73 const empty = Option.none<string>();
74 yield* Ref.set(commitHashCache, Option.some(empty));
75 return empty;
76 }
77​
78 const commitHash = yield* resolveEmbeddedCommitHash;
79 yield* Ref.set(commitHashCache, Option.some(commitHash));
80 return commitHash;
81 });
82​
83 const resolveUserDataPath = Effect.gen(function* () {
84 const legacyPath = environment.path.join(
85 environment.appDataDirectory,
86 environment.legacyUserDataDirName,
87 );
88 const legacyPathExists = yield* fileSystem
89 .exists(legacyPath)
90 .pipe(Effect.orElseSucceed(() => false));
91 return legacyPathExists
92 ? legacyPath
93 : environment.path.join(environment.appDataDirectory, environment.userDataDirName);
94 }).pipe(Effect.withSpan("desktop.appIdentity.resolveUserDataPath"));
95​
96 const configure = Effect.gen(function* () {
97 const commitHash = yield* resolveAboutCommitHash;
98 yield* electronApp.setName(environment.displayName);
99 yield* electronApp.setAboutPanelOptions({
100 applicationName: environment.displayName,
101 applicationVersion: environment.appVersion,
102 version: Option.getOrElse(commitHash, () => "unknown"),
103 });
104​
105 if (environment.platform === "win32") {
106 yield* electronApp.setAppUserModelId(environment.appUserModelId);
107 }
108​
109 if (environment.platform === "linux") {
110 yield* electronApp.setDesktopName(environment.linuxDesktopEntryName);
111 }
112​
113 if (environment.platform === "darwin") {
114 const iconPaths = yield* assets.iconPaths;
115 yield* Option.match(iconPaths.png, {
116 onNone: () => Effect.void,
117 onSome: electronApp.setDockIcon,
118 });
119 }
120 }).pipe(Effect.withSpan("desktop.appIdentity.configure"));
121​
122 return DesktopAppIdentity.of({
123 resolveUserDataPath,
124 configure,
125 });
126});
127​
128export const layer = Layer.effect(DesktopAppIdentity, make);