const fs = require('fs');

if (fs.existsSync('app_final.js')) {
    let js = fs.readFileSync('app_final.js', 'utf8');

    js = js.replace(/\(inv\.items \|\| \[\]\)\.forEach\(it => \{ newTotal \+= \(it\.qty \* \(it\.price \|\| 0\)\); \}\);/g,
        `let newTotal = 0; (inv.items || []).forEach(it => { newTotal += (it.qty * (it.price || 0)); });`);

    fs.writeFileSync('app_final.js', js, 'utf8');
    console.log('Fixed newTotal ReferenceError');
}

// Cache buster
if (fs.existsSync('index.html')) {
    let html = fs.readFileSync('index.html', 'utf8');
    html = html.replace(/v=\d+/g, 'v=' + Date.now());
    fs.writeFileSync('index.html', html, 'utf8');
}
