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";
6
7import * as DesktopEnvironment from "./DesktopEnvironment.ts";
8
9export interface DesktopIconPaths {
10 readonly ico: Option.Option<string>;
11 readonly icns: Option.Option<string>;
12 readonly png: Option.Option<string>;
13}
14
15export interface DesktopAssetsShape {
16 readonly iconPaths: Effect.Effect<DesktopIconPaths>;
17 readonly resolveResourcePath: (fileName: string) => Effect.Effect<Option.Option<string>>;
18}
19
20export class DesktopAssets extends Context.Service<DesktopAssets, DesktopAssetsShape>()(
21 "t3/desktop/Assets",
22) {}
23
24const resolveResourcePath = Effect.fn("desktop.assets.resolveResourcePath")(function* (
25 fileName: string,
26): Effect.fn.Return<
27 Option.Option<string>,
28 never,
29 FileSystem.FileSystem | DesktopEnvironment.DesktopEnvironment
30> {
31 const fileSystem = yield* FileSystem.FileSystem;
32 const environment = yield* DesktopEnvironment.DesktopEnvironment;
33 const candidates = environment.resolveResourcePathCandidates(fileName);
34 for (const candidate of candidates) {
35 const exists = yield* fileSystem.exists(candidate).pipe(Effect.orElseSucceed(() => false));
36 if (exists) {
37 return Option.some(candidate);
38 }
39 }
40 return Option.none<string>();
41});
42
43const resolveIconPath = Effect.fn("desktop.assets.resolveIconPath")(function* (
44 ext: keyof DesktopIconPaths,
45): Effect.fn.Return<
46 Option.Option<string>,
47 never,
48 FileSystem.FileSystem | DesktopEnvironment.DesktopEnvironment
49> {
50 const fileSystem = yield* FileSystem.FileSystem;
51 const environment = yield* DesktopEnvironment.DesktopEnvironment;
52 if (environment.isDevelopment && process.platform === "darwin" && ext === "png") {
53 const developmentDockIconPath = environment.developmentDockIconPath;
54 const developmentDockIconExists = yield* fileSystem
55 .exists(developmentDockIconPath)
56 .pipe(Effect.orElseSucceed(() => false));
57 if (developmentDockIconExists) {
58 return Option.some(developmentDockIconPath);
59 }
60 }
61
62 return yield* resolveResourcePath(`icon.${ext}`);
63});
64
65const make = Effect.gen(function* () {
66 const context = yield* Effect.context<
67 FileSystem.FileSystem | DesktopEnvironment.DesktopEnvironment
68 >();
69 const [ico, icns, png] = yield* Effect.all(
70 [resolveIconPath("ico"), resolveIconPath("icns"), resolveIconPath("png")] as const,
71 { concurrency: "unbounded" },
72 );
73 const iconPaths = { ico, icns, png } satisfies DesktopIconPaths;
74
75 return DesktopAssets.of({
76 iconPaths: Effect.succeed(iconPaths),
77 resolveResourcePath: Effect.fn("desktop.assets.resolveResourcePath.scoped")(
78 function* (fileName) {
79 return yield* resolveResourcePath(fileName).pipe(Effect.provide(context));
80 },
81 ),
82 });
83});
84
85export const layer = Layer.effect(DesktopAssets, make);