reference.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. export * as ConfigReference from "./reference"
  2. import { ConfigReferenceV1 } from "@opencode-ai/core/v1/config/reference"
  3. export type NormalizedEntry =
  4. | {
  5. kind: "local"
  6. path: string
  7. }
  8. | {
  9. kind: "git"
  10. repository: string
  11. branch?: string
  12. }
  13. | {
  14. kind: "invalid"
  15. message: string
  16. }
  17. export type NormalizedInfo = Record<string, NormalizedEntry>
  18. export function validateAlias(name: string) {
  19. if (name.length === 0) return "Reference alias must not be empty"
  20. if (/[\/\s`,]/.test(name)) {
  21. return "Reference alias must not contain /, whitespace, comma, or backtick"
  22. }
  23. }
  24. export function normalizeEntry(entry: ConfigReferenceV1.Entry): NormalizedEntry {
  25. if (typeof entry === "string") {
  26. if (entry.startsWith(".") || entry.startsWith("/") || entry.startsWith("~")) {
  27. return { kind: "local", path: entry }
  28. }
  29. return { kind: "git", repository: entry }
  30. }
  31. if ("path" in entry) return { kind: "local", path: entry.path }
  32. return { kind: "git", repository: entry.repository, branch: entry.branch }
  33. }
  34. export function normalize(info: ConfigReferenceV1.Info): NormalizedInfo {
  35. return Object.fromEntries(
  36. Object.entries(info).map(([name, entry]) => {
  37. const aliasError = validateAlias(name)
  38. return [name, aliasError ? { kind: "invalid" as const, message: aliasError } : normalizeEntry(entry)] as const
  39. }),
  40. )
  41. }