From 441557adda6e39be72fc6f318f96cb6afb4e9ade Mon Sep 17 00:00:00 2001 From: aryeharmon Date: Tue, 19 Mar 2019 16:09:41 +0200 Subject: [PATCH 1/2] Update fcm_config_files_process.js fixed issue #213 strings.xml path has changed in later version --- scripts/fcm_config_files_process.js | 179 +++++++++++----------------- 1 file changed, 68 insertions(+), 111 deletions(-) diff --git a/scripts/fcm_config_files_process.js b/scripts/fcm_config_files_process.js index 1969efb6e..2f262bb5e 100644 --- a/scripts/fcm_config_files_process.js +++ b/scripts/fcm_config_files_process.js @@ -2,129 +2,86 @@ '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 + '>(.*?)', "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('([^\@<]+?)', 'i'), ''); +if (directoryExists("platforms/ios")) { + var path = "GoogleService-Info.plist"; - // strip non-default value - strings = strings.replace(new RegExp('([^\@<]+?)', '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('([^<]+?)', 'i'), '' + json.client[0].client_info.mobilesdk_app_id + ''); +if (directoryExists("platforms/android")) { + var path = "google-services.json"; - // replace the default value - strings = strings.replace(new RegExp('([^<]+?)', 'i'), '' + json.client[0].api_key[0].current_key + ''); + if (fileExists( path )) { + try { + var contents = fs.readFileSync(path).toString(); + fs.writeFileSync("platforms/android/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 + '>(.*?)', 'i')); - if (value && value[1]) { - return value[1] - } else { - return null - } -} + // strip non-default value + strings = strings.replace(new RegExp('([^\@<]+?)', "i"), '') -function fileExists(path) { - try { - return fs.statSync(path).isFile(); - } catch (e) { - return false; - } -} + // strip non-default value + strings = strings.replace(new RegExp('([^\@<]+?)', "i"), '') -function directoryExists(path) { - try { - return fs.statSync(path).isDirectory(); - } catch (e) { - return false; - } -} \ No newline at end of file + // 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("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.") + } +} From cba913817d7499c47072b70d7475b61eb3d22074 Mon Sep 17 00:00:00 2001 From: aryeharmon Date: Tue, 26 Mar 2019 16:27:46 +0200 Subject: [PATCH 2/2] Update fcm_config_files_process.js --- scripts/fcm_config_files_process.js | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/fcm_config_files_process.js b/scripts/fcm_config_files_process.js index 2f262bb5e..91c66b324 100644 --- a/scripts/fcm_config_files_process.js +++ b/scripts/fcm_config_files_process.js @@ -56,6 +56,7 @@ if (directoryExists("platforms/android")) { try { var contents = fs.readFileSync(path).toString(); fs.writeFileSync("platforms/android/google-services.json", contents); + fs.writeFileSync("platforms/android/app/google-services.json", contents); var json = JSON.parse(contents);