Something went wrong. Try again.
presents omp as codex for use with chatgpt remote
Something went wrong. Try again.
3.0 kB · 114 lines
JavaScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115import fs from "node:fs/promises";
export function parseToml(text) { const result = {}; let currentSection = result;
const setPath = (obj, pathParts, value) => { let curr = obj; for (let i = 0; i < pathParts.length - 1; i++) { const part = pathParts[i]; if (!curr[part] || typeof curr[part] !== "object") curr[part] = {}; curr = curr[part]; } curr[pathParts[pathParts.length - 1]] = value; };
const getOrCreateSection = (obj, pathParts) => { let curr = obj; for (const part of pathParts) { if (!curr[part] || typeof curr[part] !== "object") curr[part] = {}; curr = curr[part]; } return curr; };
const parseValue = (valStr) => { valStr = valStr.trim(); if (valStr === "true") return true; if (valStr === "false") return false; if (valStr === "null") return null; if (/^-?\d+$/.test(valStr)) return parseInt(valStr, 10); if (/^-?\d+\.\d+$/.test(valStr)) return parseFloat(valStr); if (valStr.startsWith("\"") || valStr.startsWith("\x27")) { try { return JSON.parse(valStr); } catch { return valStr.slice(1, -1); } } if (valStr.startsWith("[") && valStr.endsWith("]")) { try { return JSON.parse(valStr); } catch { const inner = valStr.slice(1, -1).trim(); if (!inner) return []; return inner.split(",").map((s) => parseValue(s)); } } if (valStr.startsWith("{") && valStr.endsWith("}")) { try { return JSON.parse(valStr); } catch { return valStr; } } return valStr; };
const splitPath = (header) => { const parts = []; let cur = ""; let inQuote = false; let quoteChar = ""; for (let i = 0; i < header.length; i++) { const c = header[i]; if ((c === "\"" || c === "\x27") && !inQuote) { inQuote = true; quoteChar = c; } else if (c === quoteChar && inQuote) { inQuote = false; } else if (c === "." && !inQuote) { parts.push(cur); cur = ""; } else { cur += c; } } if (cur) parts.push(cur); return parts; };
const lines = text.split("\n"); for (let line of lines) { line = line.trim(); if (!line || line.startsWith("#")) continue;
if (line.startsWith("[") && line.endsWith("]")) { const header = line.slice(1, -1).trim(); const parts = splitPath(header); currentSection = getOrCreateSection(result, parts); continue; }
const eqIdx = line.indexOf("="); if (eqIdx === -1) continue;
const rawKey = line.slice(0, eqIdx).trim(); const rawVal = line.slice(eqIdx + 1).trim(); const keyParts = splitPath(rawKey); const parsedVal = parseValue(rawVal); setPath(currentSection, keyParts, parsedVal); }
return result;}
export async function readTomlFile(filePath) { try { const content = await fs.readFile(filePath, "utf8"); return parseToml(content); } catch { return null; }}