Skip to content

Commit

Permalink
Fix for new cordova paths fechanique#213
Browse files Browse the repository at this point in the history
Cordova 7.0+ changed the android paths structure, this should fix that problem and also maintain compatibility with older versions
  • Loading branch information
Grohden authored Jan 8, 2018
1 parent 037cb5b commit 5ee6347
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions scripts/fcm_config_files_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

var fs = require('fs');
var execSync = require('child_process').execSync;

var getValue = function(config, name) {
var value = config.match(new RegExp('<' + name + '>(.*?)</' + name + '>', "i"))
Expand Down Expand Up @@ -55,10 +56,17 @@ if (directoryExists("platforms/android")) {
if (fileExists( path )) {
try {
var contents = fs.readFileSync(path).toString();
fs.writeFileSync("platforms/android/google-services.json", contents);

var cordovaVersion = execSync('cordova -v', {encoding: 'utf-8'});
var isNewFolderStructures = cordovaVersion.split('.')[0] >= 7;

var gServicesWritePath = isNewFolderStructures ? "platforms/android/" : "platforms/android/app" ;
var stringsPath = (isNewFolderStructures ? "platforms/android" : "platforms/android/app/src/main") + "/res/values/strings.xml"

fs.writeFileSync(gServicesWritePath + "google-services.json", contents);

var json = JSON.parse(contents);
var strings = fs.readFileSync("platforms/android/res/values/strings.xml").toString();
var strings = fs.readFileSync(stringsPath).toString();

// strip non-default value
strings = strings.replace(new RegExp('<string name="google_app_id">([^\@<]+?)</string>', "i"), '')
Expand All @@ -75,12 +83,12 @@ if (directoryExists("platforms/android")) {
// 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/res/values/strings.xml", strings);
fs.writeFileSync(stringsPath, 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.")
}
}
}

0 comments on commit 5ee6347

Please sign in to comment.