From 73cbc66bb96b348ec791e6854d6c24e6d85b2fa8 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 26 Sep 2017 00:21:27 +0200 Subject: cordova-plugin-firebase --- .../scripts/after_prepare.js | 134 +++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100755 StoneIsland/plugins/cordova-plugin-firebase/scripts/after_prepare.js (limited to 'StoneIsland/plugins/cordova-plugin-firebase/scripts/after_prepare.js') diff --git a/StoneIsland/plugins/cordova-plugin-firebase/scripts/after_prepare.js b/StoneIsland/plugins/cordova-plugin-firebase/scripts/after_prepare.js new file mode 100755 index 00000000..3f8406c2 --- /dev/null +++ b/StoneIsland/plugins/cordova-plugin-firebase/scripts/after_prepare.js @@ -0,0 +1,134 @@ +#!/usr/bin/env node +'use strict'; + +/** + * This hook makes sure projects using [cordova-plugin-firebase](https://github.com/arnesson/cordova-plugin-firebase) + * will build properly and have the required key files copied to the proper destinations when the app is build on Ionic Cloud using the package command. + * Credits: https://github.com/arnesson. + */ +var fs = require('fs'); +var path = require('path'); + +fs.ensureDirSync = function (dir) { + if (!fs.existsSync(dir)) { + dir.split(path.sep).reduce(function (currentPath, folder) { + currentPath += folder + path.sep; + if (!fs.existsSync(currentPath)) { + fs.mkdirSync(currentPath); + } + return currentPath; + }, ''); + } +}; + +var config = fs.readFileSync('config.xml').toString(); +var name = getValue(config, 'name'); + +var IOS_DIR = 'platforms/ios'; +var ANDROID_DIR = 'platforms/android'; + +var PLATFORM = { + IOS: { + dest: [ + IOS_DIR + '/' + name + '/Resources/GoogleService-Info.plist', + IOS_DIR + '/' + name + '/Resources/Resources/GoogleService-Info.plist' + ], + src: [ + 'GoogleService-Info.plist', + IOS_DIR + '/www/GoogleService-Info.plist', + 'www/GoogleService-Info.plist' + ] + }, + ANDROID: { + dest: [ + ANDROID_DIR + '/google-services.json' + ], + src: [ + 'google-services.json', + ANDROID_DIR + '/assets/www/google-services.json', + 'www/google-services.json' + ], + stringsXml: ANDROID_DIR + '/res/values/strings.xml' + } +}; + +// Copy key files to their platform specific folders +if (directoryExists(IOS_DIR)) { + copyKey(PLATFORM.IOS); +} else if (directoryExists(ANDROID_DIR)) { + copyKey(PLATFORM.ANDROID, updateStringsXml) +} + +function updateStringsXml(contents) { + var json = JSON.parse(contents); + var strings = fs.readFileSync(PLATFORM.ANDROID.stringsXml).toString(); + + // strip non-default value + strings = strings.replace(new RegExp('([^\@<]+?)', 'i'), ''); + + // strip non-default value + strings = strings.replace(new RegExp('([^\@<]+?)', 'i'), ''); + + // strip empty lines + strings = strings.replace(new RegExp('(\r\n|\n|\r)[ \t]*(\r\n|\n|\r)', 'gm'), '$1'); + + // replace the default value + strings = strings.replace(new RegExp('([^<]+?)', 'i'), '' + json.client[0].client_info.mobilesdk_app_id + ''); + + // replace the default value + strings = strings.replace(new RegExp('([^<]+?)', 'i'), '' + json.client[0].api_key[0].current_key + ''); + + fs.writeFileSync(PLATFORM.ANDROID.stringsXml, strings); +} + +function copyKey(platform, callback) { + for (var i = 0; i < platform.src.length; i++) { + var file = platform.src[i]; + if (fileExists(file)) { + try { + var contents = fs.readFileSync(file).toString(); + + try { + platform.dest.forEach(function (destinationPath) { + var folder = destinationPath.substring(0, destinationPath.lastIndexOf('/')); + fs.ensureDirSync(folder); + fs.writeFileSync(destinationPath, contents); + }); + } catch (e) { + // skip + } + + callback && callback(contents); + } catch (err) { + console.log(err) + } + + break; + } + } +} + +function getValue(config, name) { + var value = config.match(new RegExp('<' + name + '>(.*?)', 'i')); + if (value && value[1]) { + return value[1] + } else { + return null + } +} + +function fileExists(path) { + try { + return fs.statSync(path).isFile(); + } catch (e) { + return false; + } +} + +function directoryExists(path) { + try { + return fs.statSync(path).isDirectory(); + } catch (e) { + return false; + } +} -- cgit v1.2.3-70-g09d2