Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fcm_config_files_process.js #575

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
180 changes: 69 additions & 111 deletions scripts/fcm_config_files_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,129 +2,87 @@
'use strict';

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'

var getValue = function(config, name) {
var value = config.match(new RegExp('<' + name + '>(.*?)</' + name + '>', "i"))
if(value && value[1]) {
return value[1]
} else {
return null
}
};
}

// Copy key files to their platform specific folders
if (directoryExists(IOS_DIR)) {
copyKey(PLATFORM.IOS);
function fileExists(path) {
try {
return fs.statSync(path).isFile();
}
catch (e) {
return false;
}
}
if (directoryExists(ANDROID_DIR)) {
copyKey(PLATFORM.ANDROID, updateStringsXml)

function directoryExists(path) {
try {
return fs.statSync(path).isDirectory();
}
catch (e) {
return false;
}
}

function updateStringsXml(contents) {
var json = JSON.parse(contents);
var strings = fs.readFileSync(PLATFORM.ANDROID.stringsXml).toString();
var config = fs.readFileSync("config.xml").toString()
var name = getValue(config, "name")

// strip non-default value
strings = strings.replace(new RegExp('<string name="google_app_id">([^\@<]+?)</string>', 'i'), '');
if (directoryExists("platforms/ios")) {
var path = "GoogleService-Info.plist";

// strip non-default value
strings = strings.replace(new RegExp('<string name="google_api_key">([^\@<]+?)</string>', 'i'), '');
if (fileExists( path )) {
try {
var contents = fs.readFileSync(path).toString();
fs.writeFileSync("platforms/ios/" + name + "/Resources/GoogleService-Info.plist", contents)
} catch(err) {
process.stdout.write(err);
}

// strip empty lines
strings = strings.replace(new RegExp('(\r\n|\n|\r)[ \t]*(\r\n|\n|\r)', 'gm'), '$1');
} else {
throw new Error("cordova-plugin-fcm: You have installed platform ios but file 'GoogleService-Info.plist' was not found in your Cordova project root folder.")
}
}

// replace the default value
strings = strings.replace(new RegExp('<string name="google_app_id">([^<]+?)</string>', 'i'), '<string name="google_app_id">' + json.client[0].client_info.mobilesdk_app_id + '</string>');
if (directoryExists("platforms/android")) {
var path = "google-services.json";

// 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>');
if (fileExists( path )) {
try {
var contents = fs.readFileSync(path).toString();
fs.writeFileSync("platforms/android/google-services.json", contents);
fs.writeFileSync("platforms/android/app/google-services.json", contents);

fs.writeFileSync(PLATFORM.ANDROID.stringsXml, strings);
}
var json = JSON.parse(contents);

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;
}
}
}
var strings = fs.readFileSync("platforms/android/app/src/main/res/values/strings.xml").toString();

function getValue(config, name) {
var value = config.match(new RegExp('<' + name + '>(.*?)</' + name + '>', 'i'));
if (value && value[1]) {
return value[1]
} else {
return null
}
}
// strip non-default value
strings = strings.replace(new RegExp('<string name="google_app_id">([^\@<]+?)</string>', "i"), '')

function fileExists(path) {
try {
return fs.statSync(path).isFile();
} catch (e) {
return false;
}
}
// strip non-default value
strings = strings.replace(new RegExp('<string name="google_api_key">([^\@<]+?)</string>', "i"), '')

function directoryExists(path) {
try {
return fs.statSync(path).isDirectory();
} catch (e) {
return false;
}
}
// 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('<string name="google_app_id">([^<]+?)</string>', "i"), '<string name="google_app_id">' + json.client[0].client_info.mobilesdk_app_id + '</string>')

// 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>')

fs.writeFileSync("platforms/android/app/src/main/res/values/strings.xml", strings);
} catch(err) {
process.stdout.write(err);
}

} else {
throw new Error("cordova-plugin-fcm: You have installed platform android but file 'google-services.json' was not found in your Cordova project root folder.")
}
}