const fs = require('fs');
let js = fs.readFileSync('app_final.js', 'utf8');

js = js.replace(/\\nwindow\./g, '\nwindow.');
js = js.replace(/\\ndocument\./g, '\ndocument.');
js = js.replace(/\\n\s*if\(/g, '\nif(');
js = js.replace(/\\n\s*let\s/g, '\nlet ');
js = js.replace(/\\n\s*const\s/g, '\nconst ');
js = js.replace(/\\n\s*var\s/g, '\nvar ');
js = js.replace(/\\n\s*return/g, '\nreturn');
js = js.replace(/\\n\s*console\./g, '\nconsole.');
js = js.replace(/\\n\s*await/g, '\nawait');
js = js.replace(/\\n\s*try/g, '\ntry');
js = js.replace(/\\n\s*catch/g, '\ncatch');
js = js.replace(/\\n\s*for/g, '\nfor');
js = js.replace(/\\n\s*while/g, '\nwhile');
js = js.replace(/\\n\s*switch/g, '\nswitch');
js = js.replace(/\\n\s*function/g, '\nfunction');
js = js.replace(/\\n\s*\}/g, '\n}');
js = js.replace(/\\n\s*\{/g, '\n{');
js = js.replace(/\\n  /g, '\n  ');
js = js.replace(/\\n/g, '\n'); // actually, it seems safe to replace all literal \n if they were wrongly escaped in the script

fs.writeFileSync('app_final.js', js, 'utf8');
console.log('Fixed newlines');
