import fs from 'fs' const path = new URL('../src/api/biz.ts', import.meta.url) let s = fs.readFileSync(path, 'utf8') // After "return real.xxx(...)" if next non-empty line is export/comment and no }, insert } s = s.replace( /(return real\.\w+\([^;]*\))\r?\n(?=\r?\n?(?:\/\*|export ))/g, '$1\n}\n', ) // handleAlert may be missing final } if (!/return real\.fetchOpLogs\(\)\r?\n\}\r?\n?$/.test(s.trimEnd() + '\n')) { s = s.replace(/(return real\.fetchOpLogs\(\))\s*$/, '$1\n}\n') } fs.writeFileSync(path, s) let bal = 0 for (const c of s) { if (c === '{') bal++ if (c === '}') bal-- } console.log('brace balance', bal) console.log('tail:\n' + s.slice(-180))