konstantinbueschel
6/9/2013 - 9:22 PM

Simple build configuration file for Titanium Mobile Alloy to remove unimportant log calls if build type is production

Simple build configuration file for Titanium Mobile Alloy to remove unimportant log calls if build type is production

function isProduction(alloyConfig)
{
    return 'production' == alloyConfig.deploytype;
}

function removeUnimportantLogCallsFromContent(content)
{
    if (!content) {
        return;
    }

    content = content.replace(/console.debug/g, '');
    content = content.replace(/console.log/g, '');

    return content;
}

function removeUnimportantLogCallsFromFile(file)
{
    var fs = require('fs');

    var resourceContent = fs.readFileSync(file).toString();
    var replacedContent = removeUnimportantLogCallsFromContent(resourceContent);

    fs.writeFileSync(file, replacedContent);
}

function isJsFile(filename)
{
    return (filename.match(/\.js$/));
}

function removeUnimportantLogCallsFromJsFiles(dir) 
{
    var path   = require('path');
    var wrench = require('wrench');
    var resourcesPath = dir.resources;

    wrench.readdirSyncRecursive(resourcesPath).forEach(function(target) {

        if (!isJsFile(target)) {
            return;
        }

        var jsFile = path.join(resourcesPath, target);
        removeUnimportantLogCallsFromFile(jsFile);
    });
}

task("post:compile", function(event, logger) {

    if (isProduction(event.alloyConfig)) {
        logger.info("Removing Unimportant Log Calls");
        removeUnimportantLogCallsFromJsFiles(event.dir);
    }

});