Created
August 3, 2015 20:30
-
-
Save marcocarnazzo/4d68d668a19e7b0440bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
# Put this on hooks/after_prepare | |
# to clean up js junk | |
var fs = require('fs'); | |
var path = require('path'); | |
var rootDir = process.argv[2]; | |
var platformPath = path.join(rootDir, 'platforms'); | |
var platform = process.env.CORDOVA_PLATFORMS; | |
var cliCommand = process.env.CORDOVA_CMDLINE; | |
// hook configuration | |
//var isRelease = true; // by default this hook is always enabled, see the line below on how to execute it only for release | |
var isRelease = (cliCommand.indexOf('--release') > -1); | |
var foldersToProcess = [ // add other www folders in here if needed (ex. js/controllers) | |
'lib', | |
'work', | |
'resources' | |
]; | |
var junkPatterns = [ // add filename regex patterns to identify junk files | |
/^(?!.*www\/lib\/.*\.(js|css|map|ttf|jpg|png|svg)$)/, // All library files except .js and .css, fonts and images | |
/\/www\/lib\/.*\/src\/.*/, // All library original source files | |
/\/www\/lib\/.*\/examples\/.*/, // All library examples | |
/\/www\/lib\/.*\/tutorials\/.*/, // All library tutorials | |
/\/www\/work\//, | |
/\/www\/resources\// | |
]; | |
if (!isRelease) { | |
return; | |
} | |
switch (platform) { | |
case 'android': | |
platformPath = path.join(platformPath, platform, 'assets', 'www'); | |
break; | |
case 'ios': | |
platformPath = path.join(platformPath, platform, 'www'); | |
break; | |
default: | |
console.log('this hook only supports android and ios currently'); | |
return; | |
} | |
foldersToProcess.forEach(function(folder) { | |
var fullPath = path.join(platformPath, folder); | |
processFiles(fullPath, function onFile(fileEntry) { | |
if (_isJunk(fileEntry)) { | |
_deleteFile(fileEntry); | |
} | |
}); | |
}); | |
function _isJunk(file) { | |
var isJunk = false; | |
var i; | |
var len = junkPatterns.length; | |
for (i = 0; i < len; i++) { | |
if (junkPatterns[i].test(file)) { | |
isJunk = true; | |
break; | |
} | |
} | |
return isJunk; | |
} | |
/** | |
* Go recursively into a dir and, for each file, call a function | |
* @param dir Initial directory | |
* @param fileCallback The function to call for each file | |
*/ | |
function processFiles(dir, fileCallback) { | |
fs.readdir(dir, function (err, list) { | |
if (err) { | |
console.log('_findLiteFiles err: ' + err); | |
return; | |
} | |
list.forEach(function(entry) { | |
entry = path.join(dir, entry); | |
fs.stat(entry, function(err, stat) { | |
if (stat) { | |
if (stat.isDirectory()) { | |
processFiles(entry, fileCallback); | |
} else { | |
fileCallback(entry); | |
} | |
} | |
}); | |
}); | |
}); | |
} | |
function _deleteFile(file) { | |
console.log('Deleting file ' + file + '...'); | |
fs.unlinkSync(file); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment