Skip to content
This repository was archived by the owner on Jul 20, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions copy_google_services_config_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,22 @@
* 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 fse = require('fs-extra');
var config = fse.readFileSync('config.xml').toString();
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';
Expand All @@ -16,8 +30,8 @@ var ANDROID_DIR = 'platforms/android';
var PLATFORM = {
IOS: {
dest: [
IOS_DIR + name + '/Resources/GoogleService-Info.plist',
IOS_DIR + name + '/Resources/Resources/GoogleService-Info.plist'
IOS_DIR + '/' + name + '/Resources/GoogleService-Info.plist',
IOS_DIR + '/' + name + '/Resources/Resources/GoogleService-Info.plist'
],
src: [
'GoogleService-Info.plist',
Expand All @@ -41,13 +55,14 @@ var PLATFORM = {
// Copy key files to their platform specific folders
if (directoryExists(IOS_DIR)) {
copyKey(PLATFORM.IOS);
} else if (directoryExists(ANDROID_DIR)) {
}
if (directoryExists(ANDROID_DIR)) {
copyKey(PLATFORM.ANDROID, updateStringsXml)
}

function updateStringsXml(contents) {
var json = JSON.parse(contents);
var strings = fse.readFileSync(PLATFORM.ANDROID.stringsXml).toString();
var strings = fs.readFileSync(PLATFORM.ANDROID.stringsXml).toString();

// strip non-default value
strings = strings.replace(new RegExp('<string name="google_app_id">([^\@<]+?)</string>', 'i'), '');
Expand All @@ -64,21 +79,21 @@ function updateStringsXml(contents) {
// replace the default value
strings = strings.replace(new RegExp('<string name="google_api_key">([^<]+?)</string>', 'i'), '<string name="google_api_key">' + json.client[0].api_key[0].current_key + '</string>');

fse.writeFileSync(PLATFORM.ANDROID.stringsXml, strings);
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 = fse.readFileSync(file).toString();
var contents = fs.readFileSync(file).toString();

try {
platform.dest.forEach(function (destinationPath) {
var folder = destinationPath.substring(0, destinationPath.lastIndexOf('/'));
fse.ensureDirSync(folder);
fse.writeFileSync(destinationPath, contents);
fs.ensureDirSync(folder);
fs.writeFileSync(destinationPath, contents);
});
} catch (e) {
// skip
Expand All @@ -105,15 +120,15 @@ function getValue(config, name) {

function fileExists(path) {
try {
return fse.statSync(path).isFile();
return fs.statSync(path).isFile();
} catch (e) {
return false;
}
}

function directoryExists(path) {
try {
return fse.statSync(path).isDirectory();
return fs.statSync(path).isDirectory();
} catch (e) {
return false;
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
},
"homepage": "https://github.com/ionic-team/ionic-package-hooks#readme",
"dependencies": {
"xcode": "^0.8.2",
"fs-extra": "3.0.1"
"xcode": "^0.8.2"
}
}