<commit_before>var models = require('../models');
var _ = require('lodash');
var resumer = require('resumer');
var browserify = require('browserify');
var path = require('path');
var cache = require('../cache');
var sass = require('node-sass');
var uuid = require('node-uuid');
var Q = require('q');


exports.getDynamicVizBundle = function (req, res, next) {

    res.set('Content-Type', 'application/javascript');
    res.set('Access-Control-Allow-Origin', "*");

    // Get all vizTypes in array
    var visualizationTypes = _.uniq(req.query.visualizations).sort();

    var cacheHit = false;

    if(!req.query.cachemiss) {
        var bundle = cache.get('js/' + visualizationTypes.toString());

        if(bundle) {
            cacheHit = true;
            res.send(bundle);
        }
    }

    console.log('building viz bundle with ' + visualizationTypes);
    var tmpPath = path.resolve(__dirname + '/../../tmp/js-build/' + uuid.v4() + '/');

    req.session.lastBundlePath = tmpPath;
    var b = browserify();

    models.VisualizationType
        .findAll().success(function(vizTypes) {
            console.log(_.pluck(vizTypes, 'name'));
            var funcs = [];
            _.each(vizTypes, function(vizType) {
                if(!vizType.isModule) {
                    funcs.push(vizType.exportToFS(tmpPath));
                }
            });

            Q.all(funcs).spread(function() {

                _.each(_.filter(vizTypes, function(vizType) { return (visualizationTypes.indexOf(vizType.name) > -1); }), function(vizType) {
                    if(vizType.isModule) {
                        console.log(vizType.moduleName);
                        b.require(vizType.moduleName, {
                            expose: vizType.moduleName
                        });
                    } else {
                        var stream = resumer().queue(vizType.javascript).end();
                        b.require(stream, {
                            basedir: tmpPath,
                            expose: vizType.name
                        });
                    }
                });

                b.bundle(function(err, buf) {
                    if(err) {
                        return next(err);
                    }               

                    var out = buf.toString('utf8');
                    cache.put('js/' + visualizationTypes.toString(), out, 1000 * 60 * 10);
                    if(!cacheHit) {
                        res.send(out);
                    }
                });
            });
        }).error(next);

};


exports.bundleJSForExecution = function(req, res, next) {
    
    res.set('Content-Type', 'application/javascript');
    
    var b = browserify();

    var js = req.body.javascript;
    var stream = resumer().queue(js).end();
    b.require(stream, {
        basedir: req.session.lastBundlePath || '',
        expose: 'dynamicallyBundledJavascript'
    });

    var bundle = b.bundle();

    bundle.on('error', function(err) {
        console.log(err);
        return res.status(500).send();
    });

    bundle.pipe(res);


};
<commit_msg>better errors in static builder<commit_after>var models = require('../models');
var _ = require('lodash');
var resumer = require('resumer');
var browserify = require('browserify');
var path = require('path');
var cache = require('../cache');
var sass = require('node-sass');
var uuid = require('node-uuid');
var Q = require('q');


exports.getDynamicVizBundle = function (req, res, next) {

    res.set('Content-Type', 'application/javascript');
    res.set('Access-Control-Allow-Origin', "*");

    // Get all vizTypes in array
    var visualizationTypes = _.uniq(req.query.visualizations).sort();

    var cacheHit = false;

    if(!req.query.cachemiss) {
        var bundle = cache.get('js/' + visualizationTypes.toString());

        if(bundle) {
            cacheHit = true;
            res.send(bundle);
        }
    }

    console.log('building viz bundle with ' + visualizationTypes);
    var tmpPath = path.resolve(__dirname + '/../../tmp/js-build/' + uuid.v4() + '/');

    req.session.lastBundlePath = tmpPath;
    var b = browserify();

    models.VisualizationType
        .findAll().success(function(vizTypes) {
            console.log(_.pluck(vizTypes, 'name'));
            var funcs = [];
            _.each(vizTypes, function(vizType) {
                if(!vizType.isModule) {
                    funcs.push(vizType.exportToFS(tmpPath));
                }
            });

            Q.all(funcs).spread(function() {

                _.each(_.filter(vizTypes, function(vizType) { return (visualizationTypes.indexOf(vizType.name) > -1); }), function(vizType) {
                    if(vizType.isModule) {
                        console.log(vizType.moduleName);
                        b.require(vizType.moduleName, {
                            expose: vizType.moduleName
                        });
                    } else {
                        var stream = resumer().queue(vizType.javascript).end();
                        b.require(stream, {
                            basedir: tmpPath,
                            expose: vizType.name
                        });
                    }
                });

                b.bundle(function(err, buf) {
                    if(err) {
                        return next(err);
                    }               

                    var out = buf.toString('utf8');
                    cache.put('js/' + visualizationTypes.toString(), out, 1000 * 60 * 10);
                    if(!cacheHit) {
                        res.send(out);
                    }
                });
            });
        }).catch(function(err) {
            return res.status(500).send(err.message).end();
        });

};


exports.bundleJSForExecution = function(req, res, next) {
    
    res.set('Content-Type', 'application/javascript');
    
    var b = browserify();

    var js = req.body.javascript;
    var stream = resumer().queue(js).end();
    b.require(stream, {
        basedir: req.session.lastBundlePath || '',
        expose: 'dynamicallyBundledJavascript'
    });

    var bundle = b.bundle();

    bundle.on('error', function(err) {
        console.log(err);
        return res.status(500).send();
    });

    bundle.pipe(res);


};

{'fraction_non_alphanumeric': 0.14412276538522387, 'fraction_numerical': 0.004904287296313875, 'mean_word_length': 1.6070103092783505, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3593390', 'n_tokens_mistral': 1833, 'n_tokens_neox': 1707, 'n_words': 298}