Skip to content
File

Blob: src/worker/sandbox/repo-config.ts

typescript33 lines
1import { parse as parseYaml } from "yaml";
2 
3import { RepoConfig, RepoConfigFileText } from "@/worker/contracts";
4import { formatCodecIssues } from "@/lib/codec-errors";
5 
6export 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 
31export const resolveWorkingDirectory = (repoRoot: string, workingDirectory: string): string =>
32 workingDirectory === "." ? repoRoot : `${repoRoot}/${workingDirectory}`;