fix-biz-braces.mjs 671 B

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