import fs from "node:fs"; import path from "node:path"; const paths = [ "..\\PackageSources\\ModelBehaviorDefs\\TFDi_Design\\MD11\\Cabin", "..\\PackageSources\\ModelBehaviorDefs\\TFDi_Design\\MD11\\FlightDeck", ]; let tips = fs.readFileSync("tooltips.csv", { encoding: "utf-8" }).split("\n"); tips = tips.reduce((prev, tip, index) => { if (index == 0) return {}; const [file, templateName, nodeID, tooltip] = tip.split(","); if (!prev[file]) prev[file] = {}; if (!prev[file][templateName]) prev[file][templateName] = {}; prev[file][templateName][nodeID] = tooltip; return prev; }, {}); paths.forEach((_path) => { const files = fs.readdirSync(_path); files.forEach((file) => { const _file = fs.readFileSync(path.join(_path, file), { encoding: "utf-8", }); const lines = _file.split("\n"); let templateName; lines.forEach((line, index) => { const _line = line.trim(); const open = [..._line.matchAll(/^$/gi)]; const nodeID = [..._line.matchAll(/^(.*)<\/NODE_ID>$/gi)]; if (open.length > 0) { templateName = open[0][1]; } else if (nodeID.length > 0) { const _nodeID = nodeID[0][1]; const _file = tips[file]; const _templateName = _file?.[templateName]; const tip = _templateName?.[_nodeID]; if (tip) { const _tip = line .replaceAll("NODE_ID", "TOOLTIPID") .replaceAll(_nodeID, tip); lines.splice(index, 0, `${_tip}\n`); } } }); }); }); /* const fd = fs.openSync("tooltips.csv", "w"); fs.writeSync(fd, "Template Name,Node ID,Tooltip\n"); objects.forEach((obj) => { fs.writeSync(fd, `${obj.templateName},${obj.nodeID},\n`); }); fs.closeSync(fd); */