var fs = require('fs');
var html = fs.readFileSync('app/templates/aufmass/bearbeiten.html', 'utf8');
var scripts = html.match(/<script>([\s\S]*?)<\/script>/g);
var ok = 0, fail = 0;
scripts.forEach(function(s, i) {
    var code = s.replace(/<\/?script>/g, '');
    try {
        new (require('vm').Script)(code.trim());
        ok++;
    } catch(e) {
        fail++;
        console.log('FAIL block ' + i + ': ' + e.message.substring(0, 80));
        var lines = code.split('\n');
        var errline = parseInt(e.stack.split(':').pop()) || 1;
        var start = Math.max(0, errline - 3);
        var end = Math.min(lines.length, errline + 2);
        for (var li = start; li < end; li++) {
            console.log('  ' + (li+1) + ': ' + lines[li].substring(0, 100));
        }
    }
});
console.log(ok + ' OK, ' + fail + ' FAIL, total ' + scripts.length);
