| ... | ... | @@ -3,6 +3,7 @@ import * as Effect from "effect/Effect"; |
| 3 | 3 | import * as FileSystem from "effect/FileSystem"; |
| 4 | 4 | import * as Layer from "effect/Layer"; |
| 5 | 5 | import * as Option from "effect/Option"; |
| 6 | import * as Schema from "effect/Schema"; |
| 6 | 7 | |
| 7 | 8 | import * as DesktopEnvironment from "./DesktopEnvironment.ts"; |
| 8 | 9 | |
| ... | ... | @@ -12,11 +13,26 @@ export interface DesktopIconPaths { |
| 12 | 13 | readonly png: Option.Option<string>; |
| 13 | 14 | } |
| 14 | 15 | |
| 16 | export 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 | |
| 15 | 29 | export class DesktopAssets extends Context.Service< |
| 16 | 30 | DesktopAssets, |
| 17 | 31 | { |
| 18 | 32 | readonly iconPaths: Effect.Effect<DesktopIconPaths>; |
| 19 | | readonly resolveResourcePath: (fileName: string) => Effect.Effect<Option.Option<string>>; |
| 33 | readonly resolveResourcePath: ( |
| 34 | fileName: string, |
| 35 | ) => Effect.Effect<Option.Option<string>, DesktopAssetProbeError>; |
| 20 | 36 | } |
| 21 | 37 | >()("@t3tools/desktop/app/DesktopAssets") {} |
| 22 | 38 | |
| ... | ... | @@ -24,14 +40,20 @@ const resolveResourcePath = Effect.fn("desktop.assets.resolveResourcePath")(func |
| 24 | 40 | fileName: string, |
| 25 | 41 | ): Effect.fn.Return< |
| 26 | 42 | Option.Option<string>, |
| 27 | | never, |
| 43 | DesktopAssetProbeError, |
| 28 | 44 | FileSystem.FileSystem | DesktopEnvironment.DesktopEnvironment |
| 29 | 45 | > { |
| 30 | 46 | const fileSystem = yield* FileSystem.FileSystem; |
| 31 | 47 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 32 | 48 | const candidates = environment.resolveResourcePathCandidates(fileName); |
| 33 | 49 | for (const candidate of candidates) { |
| 34 | | const exists = yield* fileSystem.exists(candidate).pipe(Effect.orElseSucceed(() => false)); |
| 50 | const exists = yield* fileSystem |
| 51 | .exists(candidate) |
| 52 | .pipe( |
| 53 | Effect.mapError( |
| 54 | (cause) => new DesktopAssetProbeError({ fileName, candidatePath: candidate, cause }), |
| 55 | ), |
| 56 | ); |
| 35 | 57 | if (exists) { |
| 36 | 58 | return Option.some(candidate); |
| 37 | 59 | } |
| ... | ... | @@ -43,16 +65,23 @@ const resolveIconPath = Effect.fn("desktop.assets.resolveIconPath")(function* ( |
| 43 | 65 | ext: keyof DesktopIconPaths, |
| 44 | 66 | ): Effect.fn.Return< |
| 45 | 67 | Option.Option<string>, |
| 46 | | never, |
| 68 | DesktopAssetProbeError, |
| 47 | 69 | FileSystem.FileSystem | DesktopEnvironment.DesktopEnvironment |
| 48 | 70 | > { |
| 49 | 71 | const fileSystem = yield* FileSystem.FileSystem; |
| 50 | 72 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 51 | 73 | if (environment.isDevelopment && environment.platform === "darwin" && ext === "png") { |
| 52 | 74 | const developmentDockIconPath = environment.developmentDockIconPath; |
| 53 | | const developmentDockIconExists = yield* fileSystem |
| 54 | | .exists(developmentDockIconPath) |
| 55 | | .pipe(Effect.orElseSucceed(() => false)); |
| 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 | ); |
| 56 | 85 | if (developmentDockIconExists) { |
| 57 | 86 | return Option.some(developmentDockIconPath); |
| 58 | 87 | } |
| ... | ... | @@ -61,7 +90,7 @@ const resolveIconPath = Effect.fn("desktop.assets.resolveIconPath")(function* ( |
| 61 | 90 | return yield* resolveResourcePath(`icon.${ext}`); |
| 62 | 91 | }); |
| 63 | 92 | |
| 64 | | const make = Effect.gen(function* () { |
| 93 | export const make = Effect.gen(function* () { |
| 65 | 94 | const context = yield* Effect.context< |
| 66 | 95 | FileSystem.FileSystem | DesktopEnvironment.DesktopEnvironment |
| 67 | 96 | >(); |