const jsonIdx = process.argv.indexOf("--json"); const xmlIdx = process.argv.indexOf("--xml"); export const jsonMode = jsonIdx !== -1; export const xmlMode = xmlIdx !== -1; if (jsonIdx !== -1) process.argv.splice(jsonIdx, 1); if (xmlIdx !== -1) process.argv.splice(xmlIdx, 1); function escapeXml(val: unknown): string { return String(val ?? "") .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """); } function toXmlItem(obj: Record, tag = "item"): string { const attrs = Object.entries(obj) .filter(([, v]) => v === null || typeof v !== "object") .map(([k, v]) => `${k}="${escapeXml(v)}"`) .join(" "); const children = Object.entries(obj) .filter(([, v]) => v !== null && typeof v === "object") .map(([k, v]) => Array.isArray(v) ? `<${k}>${v .map((i) => typeof i === "object" && i !== null ? toXmlItem(i as Record) : `${escapeXml(i)}`, ) .join("")}` : toXmlItem(v as Record, k), ) .join(""); return `<${tag}${attrs ? " " + attrs : ""}>${children}`; } export function toXml(results: readonly unknown[]): string { return `\n${results.map((r) => " " + toXmlItem(r as Record)).join("\n")}\n`; }