<commit_before>/* global require, console */

var fs, scriptsFilePath, template;
var minify = require("minify");
var exec = require("child_process").exec;

fs = require('fs');
scriptsFilePath = 'scripts.json';
template = "" + fs.readFileSync('deploy-template.tpl');

function mkdirSync (path) {
    
    try {
        fs.mkdirSync(path);
    }
    catch (e) {
        if (e.code != 'EEXIST') {
            throw e;
        }
    }
};

function processScriptsFileFn (data) {
    
    var json;
    
    try {
        json = JSON.parse(data);
    }
    catch (e) {
        
        console.log('Parsing file ' + scriptsFilePath + ' as JSON failed!');
        console.log('Error was:' + e);
        
        return;
    }
    
    concatJsFiles(json.files);
};

function concatJsFiles (files) {
    
    var coreFile, moduleName, fullFile;
    
    coreFile = ''; 
    
    function fn (path) {
        
        function concFn (data) {
            coreFile += "" + removeUnwantedSections(data);
        };
        
        concFn(fs.readFileSync('./' + path, 'utf-8'));
    };
    
    for (moduleName in files) {
        fn(files[moduleName]);
    }
    
    coreFile = template.replace("{{content}}", coreFile);
    fullFile = fs.readFileSync("./libs/using.js/using.js", "utf-8") + coreFile;
    
    writeFileFn(coreFile, "enjoy-core");
    writeFileFn(fullFile, "enjoy");
    
    exec("docco -o docs/ bin/enjoy-core.js", function () {
        exec("mv docs/enjoy-core.html docs/index.html", function () {
            console.log("Documentation created.");
        });
    });
};

function writeFileFn (contents, fileNameBase) {
    
    function makeErrorFn (successText) {
        return function (err) {
            
            if (err) {
                console.log(err);
                return;
            }
            
            console.log(successText);
        };
    }
    
    mkdirSync('./bin');
    fs.writeFile('./bin/' + fileNameBase + '.js', contents, makeErrorFn('File created.'));
    
    minify({ext: '.js', data: contents}, function(error, data) {
        if (error) {
            console.log(error);
        }
        else {
            fs.writeFile('./bin/' + fileNameBase + '.min.js', data, 
                makeErrorFn("Minified file created."));
        }
    });
}

function removeUnwantedSections (fileContents) {
    
    fileContents = fileContents.
        replace(/\/\*<ON_DEPLOY_REMOVE>\*\/[\s\S]*\/\*<\/ON_DEPLOY_REMOVE>\*\//g, "");
    
    return fileContents;
}

processScriptsFileFn(fs.readFileSync(scriptsFilePath, 'utf-8'));<commit_msg>Fixed deploy script to work with recent minify versions.<commit_after>/* global require, console */

var fs, scriptsFilePath, template;
var minify = require("minify");
var exec = require("child_process").exec;

fs = require('fs');
scriptsFilePath = 'scripts.json';
template = "" + fs.readFileSync('deploy-template.tpl');

function mkdirSync (path) {
    
    try {
        fs.mkdirSync(path);
    }
    catch (e) {
        if (e.code != 'EEXIST') {
            throw e;
        }
    }
};

function processScriptsFileFn (data) {
    
    var json;
    
    try {
        json = JSON.parse(data);
    }
    catch (e) {
        
        console.log('Parsing file ' + scriptsFilePath + ' as JSON failed!');
        console.log('Error was:' + e);
        
        return;
    }
    
    concatJsFiles(json.files);
};

function concatJsFiles (files) {
    
    var coreFile, moduleName, fullFile;
    
    coreFile = ''; 
    
    function fn (path) {
        
        function concFn (data) {
            coreFile += "" + removeUnwantedSections(data);
        };
        
        concFn(fs.readFileSync('./' + path, 'utf-8'));
    };
    
    for (moduleName in files) {
        fn(files[moduleName]);
    }
    
    coreFile = template.replace("{{content}}", coreFile);
    fullFile = fs.readFileSync("./libs/using.js/using.js", "utf-8") + coreFile;
    
    writeFileFn(coreFile, "enjoy-core");
    writeFileFn(fullFile, "enjoy");
    
    exec("docco -o docs/ bin/enjoy-core.js", function () {
        exec("mv docs/enjoy-core.html docs/index.html", function () {
            console.log("Documentation created.");
        });
    });
};

function writeFileFn (contents, fileNameBase) {
    
    function makeErrorFn (successText) {
        return function (err) {
            
            if (err) {
                console.log(err);
                return;
            }
            
            console.log(successText);
        };
    }
    
    mkdirSync('./bin');
    fs.writeFile('./bin/' + fileNameBase + '.js', contents, makeErrorFn('File created.'));
    
    minify('./bin/' + fileNameBase + '.js', function(error, data) {
        if (error) {
            console.log(error);
        }
        else {
            fs.writeFile('./bin/' + fileNameBase + '.min.js', data, 
                makeErrorFn("Minified file created."));
        }
    });
}

function removeUnwantedSections (fileContents) {
    
    fileContents = fileContents.
        replace(/\/\*<ON_DEPLOY_REMOVE>\*\/[\s\S]*\/\*<\/ON_DEPLOY_REMOVE>\*\//g, "");
    
    return fileContents;
}

processScriptsFileFn(fs.readFileSync(scriptsFilePath, 'utf-8'));
{'fraction_non_alphanumeric': 0.15527831094049904, 'fraction_numerical': 0.0011516314779270633, 'mean_word_length': 1.9507361268403172, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2668620', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1622, 'n_words': 302}