// Helpers for deploy/pair.mjs (kept separate so they are unit-testable). /** * Set `refs.` in the text of a DSH `.credentials.yaml`, preserving everything else * (records, other refs, comments). Values are written as JSON strings (valid YAML scalars). */ export function setRefIn(text, ref, value) { if (!/^[A-Z][A-Z0-9_]*$/.test(ref)) throw new Error(`bad ref name ${ref}`) const q = JSON.stringify(String(value)) const lines = (text || 'version: 1\n').split('\n') if (lines[lines.length - 1] === '') lines.pop() let refsAt = lines.findIndex((l) => /^refs:\s*(\{\})?\s*$/.test(l)) if (refsAt === -1) { lines.push('refs:'); refsAt = lines.length - 1 } else lines[refsAt] = 'refs:' let end = refsAt + 1 while (end < lines.length && /^\s+\S/.test(lines[end])) end++ const existing = lines.slice(refsAt + 1, end).findIndex((l) => new RegExp(`^\\s+${ref}:`).test(l)) if (existing >= 0) lines[refsAt + 1 + existing] = ` ${ref}: ${q}` else lines.splice(end, 0, ` ${ref}: ${q}`) return lines.join('\n') + '\n' } /** Read `refs.` from `.credentials.yaml` text (plain, quoted or JSON-quoted scalars). */ export function getRefIn(text, ref) { const m = String(text || '').match(new RegExp(`^\\s+${ref}:\\s*(.+)$`, 'm')) if (!m) return undefined const v = m[1].trim() if (v.startsWith('"')) { try { return JSON.parse(v) } catch { return v.slice(1, -1) } } return v.replace(/^'|'$/g, '') }