Skip to content

Instantly share code, notes, and snippets.

@tsteur
Last active Nov 8, 2017
Embed
What would you like to do?
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);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment