| 17 | * Returns null if the file doesn't exist. |
| 18 | */ |
| 19 | export const loadConfig = ( |
| 20 | path: string, |
| 21 | ): Effect.Effect< |
| 22 | ExecutorFileConfig | null, |
| 23 | ConfigParseError | PlatformError, |
| 24 | FileSystem.FileSystem |
| 25 | > => |
| 26 | Effect.gen(function* () { |
| 27 | const fs = yield* FileSystem.FileSystem; |
| 28 | |
| 29 | const exists = yield* fs.exists(path); |
| 30 | if (!exists) return null; |
| 31 | |
| 32 | const raw = yield* fs.readFileString(path); |
| 33 | |
| 34 | const errors: jsonc.ParseError[] = []; |
| 35 | const parsed = jsonc.parse(raw, errors); |
| 36 | |
| 37 | if (errors.length > 0) { |
| 38 | const msg = errors |
| 39 | .map((e) => `offset ${e.offset}: ${jsonc.printParseErrorCode(e.error)}`) |
| 40 | .join("; "); |
| 41 | return yield* new ConfigParseError({ path, message: msg }); |
| 42 | } |
| 43 | |
| 44 | const decoded = yield* Schema.decodeUnknownEffect(ExecutorFileConfig)(parsed).pipe( |
| 45 | Effect.mapError( |
| 46 | (error) => |
| 47 | new ConfigParseError({ |
| 48 | path, |
| 49 | message: error.issue.toString(), |
| 50 | }), |
| 51 | ), |
| 52 | ); |
| 53 | |
| 54 | return decoded; |
| 55 | }); |