summaryrefslogtreecommitdiff
path: root/hooks
diff options
context:
space:
mode:
Diffstat (limited to 'hooks')
-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
-rwxr-xr-xhooks/before_platform_add/init_directories.js23
-rwxr-xr-xhooks/before_prepare/01_pp_hacks.sh62
-rwxr-xr-xhooks/before_prepare/02_jshint.js73
-rw-r--r--hooks/uglify-config.json21
7 files changed, 379 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;
+ }
+}
diff --git a/hooks/before_platform_add/init_directories.js b/hooks/before_platform_add/init_directories.js
new file mode 100755
index 00000000..babde347
--- /dev/null
+++ b/hooks/before_platform_add/init_directories.js
@@ -0,0 +1,23 @@
+#!/usr/bin/env node
+
+/**
+ * On a fresh clone, the local platforms/ and plugins/ directories will be
+ * missing, so ensure they get created before the first platform is added.
+ */
+var fs = require('fs');
+var path = require('path');
+
+var platformsDir = path.resolve(__dirname, '../../platforms');
+var pluginsDir = path.resolve(__dirname, '../../plugins');
+
+try {
+ fs.mkdirSync(platformsDir, function (err) {
+ if (err) { console.error(err); }
+ });
+} catch(ex) {}
+
+try {
+ fs.mkdirSync(pluginsDir, function (err) {
+ if (err) { console.error(err); }
+ });
+} catch(ex) {}
diff --git a/hooks/before_prepare/01_pp_hacks.sh b/hooks/before_prepare/01_pp_hacks.sh
new file mode 100755
index 00000000..d049f8e7
--- /dev/null
+++ b/hooks/before_prepare/01_pp_hacks.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+exe() { echo "\$ $@" ; "$@" ; }
+
+# Custom stuff I need to do for zmNinja
+echo ----------------------------------------------------
+echo Pliable Pixels build pre-preprocessing
+echo ----------------------------------------------------
+echo Curr Dir: `pwd`
+
+#if [ -d "plugins/phonegap-plugin-push/src/android/com/adobe/phonegap/push/" ]; then
+# echo "Copying Modified GCMIntentService for custom sound"
+# exe cp www/external/GCMIntentService.java plugins/phonegap-plugin-push/src/android/com/adobe/phonegap/push/
+# exe cp www/external/GCMIntentService.java platforms/android/src/com/adobe/phonegap/push
+#else
+# echo "Directory plugins/phonegap-plugin-push/src/android/com/adobe/phonegap/push/ does not exist, skipping..."
+#fi
+
+echo "Copying custom sound"
+echo "---------------------"
+
+if [ -d "platforms/android" ]; then
+ exe mkdir -p platforms/android/res/raw/
+ exe cp www/sounds/blop.mp3 platforms/android/res/raw/
+ exe cp www/sounds/blop.caf platforms/ios/zmNinja/Resources
+else
+ echo "Directory platforms/android does not exist, skipping..."
+fi
+
+#echo "Copying plist hack for iOS for non SSL connections"
+#echo "--------------------------------------------------"
+#if [ -d "platforms/ios/zmNinja" ]; then
+# exe cp www/external/zmNinja-Info.plist.IOS9nonSSLPatch platforms/ios/zmNinja/zmNinja-Info.plist
+#else
+# echo "Directory platforms/ios/zmNinja does not exist, skipping..."
+#fi
+
+echo "Copying Android notification icons to resource dir"
+echo "--------------------------------------------------"
+if [ -d "platforms/android/res/" ]; then
+ exe cp -R www/external/android-notification-icons/ platforms/android/res/
+else
+ echo "Directory platforms/android/res/ does not exist, skipping..."
+fi
+
+#echo "Fixing insecure SSL permission problem"
+#echo "--------------------------------------------------"
+#if [ -d "platforms/android/CordovaLib/src/org/apache/cordova/engine" ]; then
+# exe cp www/external/SystemWebViewClient.java platforms/android/CordovaLib/src/org/apache/cordova/engine
+#else
+# echo "Directory platforms/android/CordovaLib/src/org/apache/cordova/engine does not exist, skipping..."
+#fi
+#if [ -d "platforms/ios/zmNinja/Classes" ]; then
+# exe cp www/external/AppDelegate.m platforms/ios/zmNinja/Classes/
+#else
+# echo "Directory platforms/ios/zmNinja/Classes does not exist, skipping..."
+#fi
+
+
+
+
+
diff --git a/hooks/before_prepare/02_jshint.js b/hooks/before_prepare/02_jshint.js
new file mode 100755
index 00000000..fff7775d
--- /dev/null
+++ b/hooks/before_prepare/02_jshint.js
@@ -0,0 +1,73 @@
+#!/usr/bin/env node
+
+var fs = require('fs');
+var path = require('path');
+var jshint = require('jshint').JSHINT;
+var async = require('async');
+
+var foldersToProcess = [
+ 'js'
+];
+
+foldersToProcess.forEach(function(folder) {
+ processFiles("www/" + folder);
+});
+
+function processFiles(dir, callback) {
+ var errorCount = 0;
+ fs.readdir(dir, function(err, list) {
+ if (err) {
+ console.log('processFiles err: ' + err);
+ return;
+ }
+ async.eachSeries(list, function(file, innercallback) {
+ file = dir + '/' + file;
+ fs.stat(file, function(err, stat) {
+ if(!stat.isDirectory()) {
+ if(path.extname(file) === ".js") {
+ lintFile(file, function(hasError) {
+ if(hasError) {
+ errorCount++;
+ }
+ innercallback();
+ });
+ } else {
+ innercallback();
+ }
+ } else {
+ innercallback();
+ }
+ });
+ }, function(error) {
+ if(errorCount > 0) {
+ process.exit(1);
+ }
+ });
+ });
+}
+
+function lintFile(file, callback) {
+ console.log("Linting " + file);
+ fs.readFile(file, function(err, data) {
+ if(err) {
+ console.log('Error: ' + err);
+ return;
+ }
+ if(jshint(data.toString())) {
+ console.log('File ' + file + ' has no errors.');
+ console.log('-----------------------------------------');
+ callback(false);
+ } else {
+ console.log('Errors in file ' + file);
+ var out = jshint.data(),
+ errors = out.errors;
+ for(var j = 0; j < errors.length; j++) {
+ console.log(errors[j].line + ':' + errors[j].character + ' -> ' + errors[j].reason + ' -> ' +
+errors[j].evidence);
+ }
+ console.log('-----------------------------------------');
+ callback(true);
+ }
+ });
+}
+
diff --git a/hooks/uglify-config.json b/hooks/uglify-config.json
new file mode 100644
index 00000000..e285cd2d
--- /dev/null
+++ b/hooks/uglify-config.json
@@ -0,0 +1,21 @@
+{
+ "alwaysRun": false,
+ "recursiveFolderSearch": true,
+ "foldersToProcess": [
+ "js",
+ "css",
+ "img",
+ "build"
+ ],
+ "uglifyJsOptions": {
+ "compress": {
+ "drop_console": true
+ },
+ "fromString": true,
+ "mangle": false
+ },
+ "cleanCssOptions": {
+ "noAdvanced": true,
+ "keepSpecialComments": 0
+ }
+}