File
Blob: src/worker/sandbox/repo-config.ts
| 1 | import { parse as parseYaml } from "yaml"; |
| 2 | |
| 3 | import { RepoConfig, RepoConfigFileText } from "@/worker/contracts"; |
| 4 | import { formatCodecIssues } from "@/lib/codec-errors"; |
| 5 | |
| 6 | export const parseRepoConfigFile = (text: string): RepoConfig => { |
| 7 | let boundedText: string; |
| 8 | |
| 9 | try { |
| 10 | boundedText = RepoConfigFileText.assertDecode(text); |
| 11 | } catch (error) { |
| 12 | const detail = formatCodecIssues(error) ?? (error instanceof Error ? error.message : String(error)); |
| 13 | throw new Error(`Repository config failed size validation: ${detail}`); |
| 14 | } |
| 15 | |
| 16 | let parsed: unknown; |
| 17 | try { |
| 18 | parsed = parseYaml(boundedText) as unknown; |
| 19 | } catch (error) { |
| 20 | throw new Error(`Repository config is not valid YAML: ${error instanceof Error ? error.message : String(error)}`); |
| 21 | } |
| 22 | |
| 23 | try { |
| 24 | return RepoConfig.assertDecode(parsed); |
| 25 | } catch (error) { |
| 26 | const detail = formatCodecIssues(error) ?? (error instanceof Error ? error.message : String(error)); |
| 27 | throw new Error(`Repository config failed validation: ${detail}`); |
| 28 | } |
| 29 | }; |
| 30 | |
| 31 | export const resolveWorkingDirectory = (repoRoot: string, workingDirectory: string): string => |
| 32 | workingDirectory === "." ? repoRoot : `${repoRoot}/${workingDirectory}`; |