summaryrefslogtreecommitdiff
path: root/hooks/after_prepare
diff options
context:
space:
mode:
authorPliable Pixels <pliablepixels@gmail.com>2017-09-21 12:49:18 -0400
committerPliable Pixels <pliablepixels@gmail.com>2017-09-21 12:49:18 -0400
commitb28028ac4082842143b0f528d6bc539da6ccb419 (patch)
tree1e26ea969a781ed8e323fca4e3c76345113fc694 /hooks/after_prepare
parent676270d21beed31d767a06c89522198c77d5d865 (diff)
mega changes, including updates and X
Diffstat (limited to 'hooks/after_prepare')
-rwxr-xr-xhooks/after_prepare/020_remove_sass_from_platforms.js28
-rwxr-xr-xhooks/after_prepare/030_android_manifest.js29
-rwxr-xr-xhooks/after_prepare/uglify.js143
3 files changed, 200 insertions, 0 deletions
diff --git a/hooks/after_prepare/020_remove_sass_from_platforms.js b/hooks/after_prepare/020_remove_sass_from_platforms.js
new file mode 100755
index 00000000..da3193a3
--- /dev/null
+++ b/hooks/after_prepare/020_remove_sass_from_platforms.js
@@ -0,0 +1,28 @@
+#!/usr/bin/env node
+
+/**
+ * After prepare, files are copied to the platforms/ios and platforms/android folders.
+ * Lets clean up some of those files that arent needed with this hook.
+ */
+var fs = require('fs');
+var path = require('path');
+
+var deleteFolderRecursive = function(removePath) {
+ if( fs.existsSync(removePath) ) {
+ fs.readdirSync(removePath).forEach(function(file,index){
+ var curPath = path.join(removePath, file);
+ if(fs.lstatSync(curPath).isDirectory()) { // recurse
+ deleteFolderRecursive(curPath);
+ } else { // delete file
+ fs.unlinkSync(curPath);
+ }
+ });
+ fs.rmdirSync(removePath);
+ }
+};
+
+var iosPlatformsDir = path.resolve(__dirname, '../../platforms/ios/www/lib/ionic/scss');
+var androidPlatformsDir = path.resolve(__dirname, '../../platforms/android/assets/www/lib/ionic/scss');
+
+deleteFolderRecursive(iosPlatformsDir);
+deleteFolderRecursive(androidPlatformsDir);
diff --git a/hooks/after_prepare/030_android_manifest.js b/hooks/after_prepare/030_android_manifest.js
new file mode 100755
index 00000000..c5590376
--- /dev/null
+++ b/hooks/after_prepare/030_android_manifest.js
@@ -0,0 +1,29 @@
+#!/usr/bin/env node
+
+var fs = require('fs');
+var async = require('async');
+var exec = require('child_process').exec;
+var path = require('path');
+
+var root = process.argv[2];
+var androidManifest = path.join(root, 'platforms/android/AndroidManifest.xml');
+fs.exists(path.join(root, 'platforms/android'), function(exists) {
+ if(!exists) return;
+ fs.readFile(androidManifest, 'utf8', function(err, data) {
+ if(err) throw err;
+
+ var lines = data.split('\n');
+ var searchingFor = '<application android:hardwareAccelerated="true"';
+ var newManifest = [];
+ var largeHeap = 'android:largeHeap="true"';
+ lines.forEach(function(line) {
+ if(line.trim().indexOf(searchingFor) != -1 && line.trim().indexOf(largeHeap) == -1) {
+ newManifest.push(line.replace(/\>$/, ' ') + largeHeap + ">");
+ } else {
+ newManifest.push(line);
+ }
+ });
+
+ fs.writeFileSync(androidManifest, newManifest.join('\n'));
+ });
+}); \ No newline at end of file
diff --git a/hooks/after_prepare/uglify.js b/hooks/after_prepare/uglify.js
new file mode 100755
index 00000000..9d18f563
--- /dev/null
+++ b/hooks/after_prepare/uglify.js
@@ -0,0 +1,143 @@
+#!/usr/bin/env node
+
+/*jshint latedef:nofunc, node:true*/
+
+// Modules
+var fs = require('fs');
+var path = require('path');
+var dependencyPath = path.join(process.cwd(), 'node_modules');
+// cordova-uglify module dependencies
+var UglifyJS = require(path.join(dependencyPath, 'uglify-js'));
+var CleanCSS = require(path.join(dependencyPath, 'clean-css'));
+var ngAnnotate = require(path.join(dependencyPath, 'ng-annotate'));
+
+// Process
+var rootDir = process.argv[2];
+var platformPath = path.join(rootDir, 'platforms');
+var platforms = process.env.CORDOVA_PLATFORMS.split(',');
+var cliCommand = process.env.CORDOVA_CMDLINE;
+
+// Hook configuration
+var configFilePath = path.join(rootDir, 'hooks/uglify-config.json');
+var hookConfig = JSON.parse(fs.readFileSync(configFilePath));
+var isRelease = hookConfig.alwaysRun || (cliCommand.indexOf('--release') > -1);
+var recursiveFolderSearch = hookConfig.recursiveFolderSearch; // set this to false to manually indicate the folders to process
+var foldersToProcess = hookConfig.foldersToProcess; // add other www folders in here if needed (ex. js/controllers)
+var cssMinifier = new CleanCSS(hookConfig.cleanCssOptions);
+
+// Exit
+if (!isRelease) {
+ return;
+}
+
+// Run uglifier
+run();
+
+/**
+ * Run compression for all specified platforms.
+ * @return {undefined}
+ */
+function run() {
+ platforms.forEach(function(platform) {
+ var wwwPath;
+
+ switch (platform) {
+ case 'android':
+ wwwPath = path.join(platformPath, platform, 'assets', 'www');
+ break;
+
+ case 'ios':
+ case 'browser':
+ case 'wp8':
+ case 'windows':
+ wwwPath = path.join(platformPath, platform, 'www');
+ break;
+
+ default:
+ console.log('this hook only supports android, ios, wp8, windows, and browser currently');
+ return;
+ }
+
+ processFolders(wwwPath);
+ });
+}
+
+/**
+ * Processes defined folders.
+ * @param {string} wwwPath - Path to www directory
+ * @return {undefined}
+ */
+function processFolders(wwwPath) {
+ foldersToProcess.forEach(function(folder) {
+ processFiles(path.join(wwwPath, folder));
+ });
+}
+
+/**
+ * Processes files in directories.
+ * @param {string} dir - Directory path
+ * @return {undefined}
+ */
+function processFiles(dir) {
+ fs.readdir(dir, function(err, list) {
+ if (err) {
+ console.log('processFiles err: ' + err);
+
+ return;
+ }
+
+ list.forEach(function(file) {
+ file = path.join(dir, file);
+
+ fs.stat(file, function(err, stat) {
+ if (stat.isFile()) {
+ compress(file);
+
+ return;
+ }
+
+ if (recursiveFolderSearch && stat.isDirectory()) {
+ processFiles(file);
+
+ return;
+ }
+ });
+ });
+ });
+}
+
+/**
+ * Compresses file.
+ * @param {string} file - File path
+ * @return {undefined}
+ */
+function compress(file) {
+ var ext = path.extname(file),
+ res,
+ source,
+ result;
+
+ switch (ext) {
+ case '.js':
+ console.log('uglifying js file ' + file);
+
+ res = ngAnnotate(String(fs.readFileSync(file)), {
+ add: true
+ });
+ result = UglifyJS.minify(res.src, hookConfig.uglifyJsOptions);
+ fs.writeFileSync(file, result.code, 'utf8'); // overwrite the original unminified file
+ break;
+
+ case '.css':
+ console.log('minifying css file ' + file);
+
+ source = fs.readFileSync(file, 'utf8');
+ result = cssMinifier.minify(source);
+ fs.writeFileSync(file, result.styles, 'utf8'); // overwrite the original unminified file
+ break;
+
+ default:
+ console.log('encountered a ' + ext + ' file, not compressing it');
+ break;
+ }
+}