( filePath: string, )
| 106 | new StorageError({ message, cause }); |
| 107 | |
| 108 | const readAuthFile = ( |
| 109 | filePath: string, |
| 110 | ): Effect.Effect<Record<string, string>, StorageError | AuthFileDecodeError> => { |
| 111 | if (!fs.existsSync(filePath)) return Effect.succeed({}); |
| 112 | return Effect.try({ |
| 113 | try: () => fs.readFileSync(filePath, "utf-8"), |
| 114 | catch: toStorageError("Failed to read auth file"), |
| 115 | }).pipe( |
| 116 | Effect.catchIf( |
| 117 | (error: StorageError) => isFileNotFoundCause(error.cause), |
| 118 | () => Effect.succeed(""), |
| 119 | ), |
| 120 | Effect.flatMap((raw: string) => |
| 121 | raw === "" |
| 122 | ? Effect.succeed<Record<string, string>>({}) |
| 123 | : decodeFlatAuthFile(raw).pipe( |
| 124 | Effect.mapError((cause) => new AuthFileDecodeError({ filePath, cause })), |
| 125 | ), |
| 126 | ), |
| 127 | ); |
| 128 | }; |
| 129 | |
| 130 | const readAll = (filePath: string): Effect.Effect<Record<string, string>, StorageError> => |
| 131 | readAuthFile(filePath).pipe( |
no test coverage detected