Files
pedwfrontend/lib/forms/readFormXml.js
T
2026-05-14 08:55:49 +00:00

35 lines
979 B
JavaScript

// lib/forms/readFormXml.js
import fs from "fs";
import path from "path";
const xmlCache = new Map();
/**
* Reads /data/formsxml/{appealtypes}.xml and normalises it.
* Returns { xmlStr } or throws on missing file.
*/
export function readFormXml(appealtypes) {
if (!appealtypes) throw new Error("readFormXml: appealtypes is required");
const configDirectory = path.resolve(process.cwd(), "data/formsxml");
const filePath = path.join(configDirectory, `${appealtypes}.xml`);
const cacheKey = filePath;
const cached = xmlCache.get(cacheKey);
if (cached) {
return { xmlStr: cached, filePath, fromCache: true };
}
let xmlStr = fs.readFileSync(filePath, "utf8");
// Keep existing normalisation behaviour
xmlStr = xmlStr.replace(/\t/g, "");
xmlStr = xmlStr.replace(/\n/g, "");
xmlStr = xmlStr.replace(/> <"/g, "><");
xmlStr = xmlStr.toString();
xmlCache.set(cacheKey, xmlStr);
return { xmlStr, filePath };
}