Open History to see the last commit
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 Schema from "effect/Schema";
7
8import * as DesktopEnvironment from "./DesktopEnvironment.ts";
9
10export interface DesktopIconPaths {
11 readonly ico: Option.Option<string>;
12 readonly icns: Option.Option<string>;
13 readonly png: Option.Option<string>;
14}
15
16export class DesktopAssetProbeError extends Schema.TaggedErrorClass<DesktopAssetProbeError>()(
17 "DesktopAssetProbeError",
18 {
19 fileName: Schema.String,
20 candidatePath: Schema.String,
21 cause: Schema.Defect(),
22 },
23) {
24 override get message(): string {
25 return `Failed to probe desktop asset "${this.fileName}" at ${this.candidatePath}.`;
26 }
27}
28
29export class DesktopAssets extends Context.Service<
30 DesktopAssets,
31 {
32 readonly iconPaths: Effect.Effect<DesktopIconPaths>;
33 readonly resolveResourcePath: (
34 fileName: string,
35 ) => Effect.Effect<Option.Option<string>, DesktopAssetProbeError>;
36 }
37>()("@v12/desktop/app/DesktopAssets") {}
38
39const resolveResourcePath = Effect.fn("desktop.assets.resolveResourcePath")(function* (
40 fileName: string,
41): Effect.fn.Return<
42 Option.Option<string>,
43 DesktopAssetProbeError,
44 FileSystem.FileSystem | DesktopEnvironment.DesktopEnvironment
45> {
46 const fileSystem = yield* FileSystem.FileSystem;
47 const environment = yield* DesktopEnvironment.DesktopEnvironment;
48 const candidates = environment.resolveResourcePathCandidates(fileName);
49 for (const candidate of candidates) {
50 const exists = yield* fileSystem
51 .exists(candidate)
52 .pipe(
53 Effect.mapError(
54 (cause) => new DesktopAssetProbeError({ fileName, candidatePath: candidate, cause }),
55 ),
56 );
57 if (exists) {
58 return Option.some(candidate);
59 }
60 }
61 return Option.none<string>();
62});
63
64const resolveIconPath = Effect.fn("desktop.assets.resolveIconPath")(function* (
65 ext: keyof DesktopIconPaths,
66): Effect.fn.Return<
67 Option.Option<string>,
68 DesktopAssetProbeError,
69 FileSystem.FileSystem | DesktopEnvironment.DesktopEnvironment
70> {
71 const fileSystem = yield* FileSystem.FileSystem;
72 const environment = yield* DesktopEnvironment.DesktopEnvironment;
73 if (environment.isDevelopment && environment.platform === "darwin" && ext === "png") {
74 const developmentDockIconPath = environment.developmentDockIconPath;
75 const developmentDockIconExists = yield* fileSystem.exists(developmentDockIconPath).pipe(
76 Effect.mapError(
77 (cause) =>
78 new DesktopAssetProbeError({
79 fileName: "icon.png",
80 candidatePath: developmentDockIconPath,
81 cause,
82 }),
83 ),
84 );
85 if (developmentDockIconExists) {
86 return Option.some(developmentDockIconPath);
87 }
88 }
89
90 return yield* resolveResourcePath(`icon.${ext}`);
91});
92
93export const make = Effect.gen(function* () {
94 const context = yield* Effect.context<
95 FileSystem.FileSystem | DesktopEnvironment.DesktopEnvironment
96 >();
97 const [ico, icns, png] = yield* Effect.all(
98 [resolveIconPath("ico"), resolveIconPath("icns"), resolveIconPath("png")] as const,
99 { concurrency: "unbounded" },
100 );
101 const iconPaths = { ico, icns, png } satisfies DesktopIconPaths;
102
103 return DesktopAssets.of({
104 iconPaths: Effect.succeed(iconPaths),
105 resolveResourcePath: Effect.fn("desktop.assets.resolveResourcePath.scoped")(
106 function* (fileName) {
107 return yield* resolveResourcePath(fileName).pipe(Effect.provide(context));
108 },
109 ),
110 });
111});
112
113export const layer = Layer.effect(DesktopAssets, make);