| ... | ... | @@ -25,7 +25,9 @@ const decodeClientSettingsJsonValue = Schema.decodeEffect(ClientSettingsJson); |
| 25 | 25 | const decodeClientSettingsJson = (raw: string): Effect.Effect<ClientSettings, Schema.SchemaError> => |
| 26 | 26 | decodeLegacyClientSettingsDocumentJson(raw).pipe( |
| 27 | 27 | Effect.map((document) => document.settings), |
| 28 | | Effect.catch(() => decodeClientSettingsJsonValue(raw)), |
| 28 | Effect.catchTags({ |
| 29 | SchemaError: () => decodeClientSettingsJsonValue(raw), |
| 30 | }), |
| 29 | 31 | ); |
| 30 | 32 | const encodeClientSettingsJson = Schema.encodeEffect(ClientSettingsJson); |
| 31 | 33 | |
| ... | ... | @@ -36,7 +38,6 @@ const DesktopClientSettingsWriteOperation = Schema.Literals([ |
| 36 | 38 | "write-temporary-file", |
| 37 | 39 | "replace-settings-file", |
| 38 | 40 | ]); |
| 39 | | type DesktopClientSettingsWriteOperation = typeof DesktopClientSettingsWriteOperation.Type; |
| 40 | 41 | |
| 41 | 42 | export class DesktopClientSettingsWriteError extends Schema.TaggedErrorClass<DesktopClientSettingsWriteError>()( |
| 42 | 43 | "DesktopClientSettingsWriteError", |
| ... | ... | @@ -51,13 +52,6 @@ export class DesktopClientSettingsWriteError extends Schema.TaggedErrorClass<Des |
| 51 | 52 | } |
| 52 | 53 | } |
| 53 | 54 | |
| 54 | | const writeError = ( |
| 55 | | operation: DesktopClientSettingsWriteOperation, |
| 56 | | path: string, |
| 57 | | cause: unknown, |
| 58 | | ): DesktopClientSettingsWriteError => |
| 59 | | new DesktopClientSettingsWriteError({ operation, path, cause }); |
| 60 | | |
| 61 | 55 | export class DesktopClientSettings extends Context.Service< |
| 62 | 56 | DesktopClientSettings, |
| 63 | 57 | { |
| ... | ... | @@ -96,19 +90,45 @@ const writeClientSettings = Effect.fnUntraced(function* (input: { |
| 96 | 90 | const directory = input.path.dirname(input.settingsPath); |
| 97 | 91 | const tempPath = `${input.settingsPath}.${process.pid}.${input.suffix}.tmp`; |
| 98 | 92 | const encoded = yield* encodeClientSettingsJson(input.settings).pipe( |
| 99 | | Effect.mapError((cause) => writeError("encode-document", input.settingsPath, cause)), |
| 93 | Effect.mapError( |
| 94 | (cause) => |
| 95 | new DesktopClientSettingsWriteError({ |
| 96 | operation: "encode-document", |
| 97 | path: input.settingsPath, |
| 98 | cause, |
| 99 | }), |
| 100 | ), |
| 101 | ); |
| 102 | yield* input.fileSystem.makeDirectory(directory, { recursive: true }).pipe( |
| 103 | Effect.mapError( |
| 104 | (cause) => |
| 105 | new DesktopClientSettingsWriteError({ |
| 106 | operation: "create-directory", |
| 107 | path: directory, |
| 108 | cause, |
| 109 | }), |
| 110 | ), |
| 111 | ); |
| 112 | yield* input.fileSystem.writeFileString(tempPath, `${encoded}\n`).pipe( |
| 113 | Effect.mapError( |
| 114 | (cause) => |
| 115 | new DesktopClientSettingsWriteError({ |
| 116 | operation: "write-temporary-file", |
| 117 | path: tempPath, |
| 118 | cause, |
| 119 | }), |
| 120 | ), |
| 121 | ); |
| 122 | yield* input.fileSystem.rename(tempPath, input.settingsPath).pipe( |
| 123 | Effect.mapError( |
| 124 | (cause) => |
| 125 | new DesktopClientSettingsWriteError({ |
| 126 | operation: "replace-settings-file", |
| 127 | path: input.settingsPath, |
| 128 | cause, |
| 129 | }), |
| 130 | ), |
| 100 | 131 | ); |
| 101 | | yield* input.fileSystem |
| 102 | | .makeDirectory(directory, { recursive: true }) |
| 103 | | .pipe(Effect.mapError((cause) => writeError("create-directory", directory, cause))); |
| 104 | | yield* input.fileSystem |
| 105 | | .writeFileString(tempPath, `${encoded}\n`) |
| 106 | | .pipe(Effect.mapError((cause) => writeError("write-temporary-file", tempPath, cause))); |
| 107 | | yield* input.fileSystem |
| 108 | | .rename(tempPath, input.settingsPath) |
| 109 | | .pipe( |
| 110 | | Effect.mapError((cause) => writeError("replace-settings-file", input.settingsPath, cause)), |
| 111 | | ); |
| 112 | 132 | }); |
| 113 | 133 | |
| 114 | 134 | export const make = Effect.gen(function* () { |
| ... | ... | @@ -124,8 +144,13 @@ export const make = Effect.gen(function* () { |
| 124 | 144 | set: (settings) => |
| 125 | 145 | crypto.randomUUIDv4.pipe( |
| 126 | 146 | Effect.map((uuid) => uuid.replace(/-/g, "")), |
| 127 | | Effect.mapError((cause) => |
| 128 | | writeError("create-temporary-file-name", environment.clientSettingsPath, cause), |
| 147 | Effect.mapError( |
| 148 | (cause) => |
| 149 | new DesktopClientSettingsWriteError({ |
| 150 | operation: "create-temporary-file-name", |
| 151 | path: environment.clientSettingsPath, |
| 152 | cause, |
| 153 | }), |
| 129 | 154 | ), |
| 130 | 155 | Effect.flatMap((suffix) => |
| 131 | 156 | writeClientSettings({ |