{"size": 2860, "ext": "js", "max_stars_repo_path": "make/generate_production_javascript.js", "max_stars_repo_name": "noisebleed/youtify", "max_stars_repo_stars_event_min_datetime": "2015-01-06T17:32:54.000Z", "max_stars_repo_stars_event_max_datetime": "2020-12-17T04:50:36.000Z", "max_issues_repo_path": "make/generate_production_javascript.js", "max_issues_repo_name": "noisebleed/youtify", "max_issues_repo_issues_event_min_datetime": "2017-07-02T11:52:55.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-24T08:59:25.000Z", "max_forks_repo_path": "make/generate_production_javascript.js", "max_forks_repo_name": "youtify/youtify", "max_forks_repo_forks_event_min_datetime": "2015-02-01T00:40:14.000Z", "max_forks_repo_forks_event_max_datetime": "2016-06-07T13:15:58.000Z"}
{"max_stars_count": 18, "max_issues_count": 2, "max_forks_count": 6, "avg_line_length": 26.4814814815, "max_line_length": 107, "alphanum_fraction": 0.591958042}
var fs = require("fs");
var jslint = require("jslint");

// https://github.com/mishoo/UglifyJS#api
var jsp = require("uglify-js").parser;
var pro = require("uglify-js").uglify;

var MATCH_PATTERN = /\/scripts\/(.*\.js)/g;
var IGNORE_PATTERN = /jquery-1\.|jquery\.time|less|soundmanager|production|shuffle|featuredetection.*.js/i;
var JSLINT_OPTIONS = {
    devel: true,
    browser: true,
    regexp: true,
    sloppy: true,
    undef: true,
    unparam: true,
    bitwise: true,
    vars: true,
    white: true,
    nomen: true,
    maxerr: 50,
    indent: 4
}

function extractScriptFileNames() {
    var filenames = [];
    var data = fs.readFileSync('./html/index.html', 'utf8');
    match = data.match(MATCH_PATTERN);

    for (var i =- 0; i < match.length; i += 1) {
        var filename = match[i];
        if (!filename.match(IGNORE_PATTERN)) {
            filenames.push(filename);
        }
    }

    return filenames;
}

function runJSLint(filenames) {
    var files = [];
    filenames.forEach(function(filename) {
        var data = fs.readFileSync('./' + filename);

        // Fix UTF8 with BOM
        if (0xEF === data[0] && 0xBB === data[1] && 0xBF === data[2]) {
            data = data.slice(3);
        }
        data = data.toString("utf8");

        console.log('[JSLINT]', filename);

        if (!jslint(data, JSLINT_OPTIONS)) {
            throw {
                filename: filename,
                report: jslint.data()
            }
        }
        files.push({
            'name': filename,
            'content': data
        });
    });
    return files;
}

function runUglify(mergedFile) {
    var ast;
    try {
        ast = jsp.parse(mergedFile); // parse code and get the initial AST
    } catch (e) {
        console.log('UglifyJS parse failed', e);
    }
    ast = pro.ast_mangle(ast); // get a new AST with mangled names
    ast = pro.ast_squeeze(ast); // get an AST with compression optimizations
    return pro.gen_code(ast); // compressed code here
}

function mergeFiles(files) {
    var mergedFile = '';
    files.forEach(function(file) {
        mergedFile += '\n\n';
        mergedFile += file.content;
    });
    return mergedFile;
}

function writeUglifiedFile(uglifiedFile) {
    var outputFileName = './scripts/production.js';
    fs.writeFileSync(outputFileName, uglifiedFile, 'utf8');
    console.log("Wrote", outputFileName);
}

var filenames = extractScriptFileNames();
try {
    var files = runJSLint(filenames);
} catch (e) {
    e.report.errors.forEach(function(e) {
        console.log(e.raw.replace('{a}', e.a).replace('{b}', e.b).replace('{c}', e.c).replace('{d}', e.d));
        console.log('Line ' + e.line + ', character', e.character, e.evidence);
    });
    process.exit(1);
}
var mergedFile = mergeFiles(files);
var uglifiedFile = runUglify(mergedFile);

writeUglifiedFile(uglifiedFile);
