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; for (let index = 0; index < lines.length; index++) { const line = lines[index].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 = lines[index] .replaceAll("NODE_ID", "TOOLTIPID") .replaceAll(_nodeID, tip); lines.splice(index, 0, `${_tip}`); const comment = lines[index - 1].replaceAll( /\S.*/g, "" ); lines.splice(index - 1, 0, comment); index += 2; //Advance } templateName = undefined; } } fs.writeFileSync(path.join(_path, file), lines.join("\n"), { encoding: "utf-8", }); }); });