WMS_MAIN/temp_rebuild_system_json.js
2026-04-27 16:09:26 +08:00

50 lines
2.2 KiB
JavaScript

const fs = require('fs');
const path = 'methods/system.json';
const s = fs.readFileSync(path, 'utf8');
function isEscaped(str, idx) { let cnt = 0; let j = idx - 1; while (j >= 0 && str[j] == '\\') { cnt++; j--; } return cnt % 2 === 1; }
// find headers key
const ph = s.indexOf('"headers"');
if (ph === -1) { console.error('no headers key'); process.exit(1); }
const posOpen = s.indexOf('[', ph);
if (posOpen === -1) { console.error('no [ after headers'); process.exit(1); }
// find matching closing bracket for headers
let depth = 0; let inStr = false; let posClose = -1;
for (let i = posOpen; i < s.length; i++) {
const c = s[i];
if (c == '"' && !isEscaped(s, i)) inStr = !inStr;
if (inStr) continue;
if (c == '[') depth++;
else if (c == ']') { depth--; if (depth === 0) { posClose = i; break; } }
}
if (posClose === -1) { console.error('cannot find headers close'); process.exit(1); }
const preHeader = s.slice(0, posOpen);
const headerContent = s.slice(posOpen, posClose + 1);
// find first group start after posClose
const gNameIndex = s.indexOf('"groupName"', posClose + 1);
if (gNameIndex === -1) { console.error('no groupName found after headers'); process.exit(1); }
// find the starting '{' before this groupName
let groupStart = s.lastIndexOf('{', gNameIndex);
if (groupStart === -1 || groupStart <= posClose) {
// try to find next '{' after posClose which encloses group
groupStart = s.indexOf('{', posClose + 1);
}
if (groupStart === -1) { console.error('cannot find group object start'); process.exit(1); }
// find the groups tail closers (we expect existing file to end with ]\n }\n}
const tailPattern = '\n ]\n }\n}';
let tailIndex = s.lastIndexOf(tailPattern);
if (tailIndex === -1) {
// fallback: try shorter pattern
tailIndex = s.lastIndexOf('\n }\n}\n');
}
if (tailIndex === -1) {
// fallback use end of file
tailIndex = s.length;
}
// groups content is from groupStart to the position before tailPattern
const groupsContent = s.slice(groupStart, tailIndex);
// build new content
const newContent = preHeader + headerContent + ',\n "methods": {\n "groups": [\n' + groupsContent.replace(/^\s*/, '') + '\n ]\n }\n}';
// write to file
fs.writeFileSync(path, newContent, 'utf8');
console.log('rewritten', path);