Skip to content

Instantly share code, notes, and snippets.

@Leftyx
Created September 17, 2015 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leftyx/9abb24c07dbbeb56b4ee to your computer and use it in GitHub Desktop.
Save Leftyx/9abb24c07dbbeb56b4ee to your computer and use it in GitHub Desktop.
Ionic hooks: delete all unused folders.
#!/usr/bin/env node
// Remove unused folder which are not needed by our app.
// v1.0
// Since the lib folder will be deleted we need to move the fonts
// referenced in the css to the root.
// foldersToRemove = array of folders to remove.
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 = (cliCommand.indexOf('--release') > -1) || (cliCommand.indexOf('--runrelease') > -1);
// isRelease = true;
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;
}
console.log('************************************************************************************');
console.log('... Removing unused folders ...');
console.log('************************************************************************************');
// Folders you want to remove from the build.
var foldersToRemove = [
'lib',
'js'
];
foldersToRemove.forEach(function(folder) {
removeDir(path.join(platformPath, folder));
});
function removeDir(dirPath) {
var files = [];
try {
files = fs.readdirSync(dirPath);
} catch (err) {
console.log('removeDir => Error', err);
return;
}
if (files.length > 0)
for (var i = 0; i < files.length; i++) {
var filePath = dirPath + '/' + files[i];
if (fs.statSync(filePath).isFile()) {
fs.unlinkSync(filePath);
// console.log('File deleted: ' + filePath);
} else {
removeDir(filePath);
}
}
fs.rmdirSync(dirPath);
console.log('Folder deleted: ' + dirPath);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment