Skip to content

Commit

Permalink
Merge pull request wizpanda#30 from wizpanda/js-improvement
Browse files Browse the repository at this point in the history
JavaScript code improvements & documentation
  • Loading branch information
sagrawal31 authored Jul 1, 2019
2 parents d4fa7f6 + 487f3be commit 5b1a526
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 79 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ cordova plugin add cordova-plugin-androidx
cordova plugin add cordova-plugin-androidx-adapter
```

### CocoaPods

This plugin uses [CocoaPods](https://cocoapods.org/) to manage the iOS Firebase dependencies. So please note the following two points:

1. If you are building your app using Xcode, please open `platform/ios/my-cordova-project.xcworkspace` instead of
`platform/ios/my-cordova-project.xcodeproj` so that the Xcode can load both Cordova app & the Pods.
2. Your repo needs to be up to date. To keep it up to date, run `pod repo update` anywhere in your terminal.

### Guides

1. Great installation and setup guide [https://medium.com/@felipepucinelli/how-to-add-push...](https://medium.com/@felipepucinelli/how-to-add-push-notifications-in-your-cordova-application-using-firebase-69fac067e821)
Expand Down Expand Up @@ -193,9 +201,9 @@ for details on how to download the files.

### Important Notes

- This plugin uses a hook (after prepare) that copies the configuration files to the right place, namely
- This plugin uses a hook (`after_prepare`) that copies the configuration files to the right place, namely
`platforms/ios/my-cordova-project/Resources` for iOS and `platforms/android` for Android.
- Firebase SDK requires the configuration files to be present and valid, otherwise your app will crash on boot or Firebase features won't work.
- Firebase SDK requires the configuration files to be present and valid, otherwise your app will crash on boot or build.

### PhoneGap Build

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@
"test:cordova@8.0.0:browser@5.0.3": "bash ./test/test-default.sh 8.0.0 browser 5.0.3"
},
"dependencies": {
"chalk": "^2.4.2",
"xcode": "^2.0.0"
},
"devDependencies": {
"cordova-common": "^3.2.0",
"markdown-toc": "^1.2.0"
}
}
30 changes: 10 additions & 20 deletions scripts/after_prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
* 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.
*/
const fs = require('fs');
const utilities = require("./lib/utilities");
const chalk = require('chalk');
const utilities = require('./lib/utilities');

const config = fs.readFileSync('config.xml').toString();
var name = utilities.getValue(config, 'name');
if(name.includes("&")){
let name = utilities.getAppName();
if (name.includes('&')) {
name = name.replace(/&/g, '&');
}

Expand All @@ -25,36 +24,27 @@ const PLATFORM = {
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'
]
src: 'GoogleService-Info.plist'
},
ANDROID: {
dest: [
ANDROID_DIR + '/google-services.json',
ANDROID_DIR + '/app/google-services.json'
],
src: [
'google-services.json',
ANDROID_DIR + '/assets/www/google-services.json',
'www/google-services.json',
ANDROID_DIR + '/app/src/main/google-services.json'
],
src: 'google-services.json'
}
};

module.exports = function (context) {
//get platform from the context supplied by cordova
// Get platform from the context supplied by cordova
const platforms = context.opts.platforms;

// Copy key files to their platform specific folders
if (platforms.indexOf('ios') !== -1 && utilities.directoryExists(IOS_DIR)) {
console.log('Preparing Firebase on iOS');
console.log(chalk.green.bold('Preparing Firebase on iOS'));
utilities.copyKey(PLATFORM.IOS);
}
if (platforms.indexOf('android') !== -1 && utilities.directoryExists(ANDROID_DIR)) {
console.log('Preparing Firebase on Android');
console.log(chalk.green.bold('Preparing Firebase on Android'));
utilities.copyKey(PLATFORM.ANDROID);
}
};
25 changes: 12 additions & 13 deletions scripts/ios/helper.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
const fs = require("fs");
const path = require("path");
const utilities = require("../lib/utilities");
const xcode = require("xcode");
const fs = require('fs');
const path = require('path');
const xcode = require('xcode');
const chalk = require('chalk');
const utilities = require('../lib/utilities');

/**
* This is used as the display text for the build phase block in XCode as well as the
* inline comments inside of the .pbxproj file for the build script phase block.
*/
const comment = "\"Crashlytics\"";
const comment = '"Crashlytics"';

module.exports = {

/**
* Used to get the path to the XCode project's .pbxproj file.
*
* @param {object} context - The Cordova context.
* @returns The path to the XCode project's .pbxproj file.
* @returns string path to the XCode project's .pbxproj file.
*/
getXcodeProjectPath: function (context) {
const appName = utilities.getAppName(context);

return path.join("platforms", "ios", appName + ".xcodeproj", "project.pbxproj");
getXcodeProjectPath: function () {
const appName = utilities.getAppName();
const xcodeProjectPath = path.join("platforms", "ios", appName + ".xcodeproj", "project.pbxproj");
console.log(chalk.blue.bold('Xcode project path:', xcodeProjectPath));
return xcodeProjectPath;
},

/**
Expand Down
64 changes: 20 additions & 44 deletions scripts/lib/utilities.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/**
* Utilities and shared functionality for the build hooks.
*/
var fs = require('fs');
var path = require("path");
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const ConfigParser = require('cordova-common').ConfigParser;

fs.ensureDirSync = function (dir) {
// TODO Use Node.js 10 & use mkdirSync with recursive option
if (!fs.existsSync(dir)) {
dir.split(path.sep).reduce(function (currentPath, folder) {
currentPath += folder + path.sep;
Expand All @@ -17,56 +20,29 @@ fs.ensureDirSync = function (dir) {
};

module.exports = {
/**
* Used to get the name of the application as defined in the config.xml.
*
* @param {object} context - The Cordova context.
* @returns {string} The value of the name element in config.xml.
*/
getAppName: function (context) {
var ConfigParser = context.requireCordovaModule("cordova-lib").configparser;
var config = new ConfigParser("config.xml");
getAppName: function () {
const config = new ConfigParser('config.xml');
return config.name();
},

/**
* The ID of the plugin; this should match the ID in plugin.xml.
*/
getPluginId: function () {
return "cordova-plugin-firebase-lib";
},

copyKey: function (platform) {
for (var i = 0; i < platform.src.length; i++) {
var file = platform.src[i];
if (this.fileExists(file)) {
try {
var contents = fs.readFileSync(file).toString();
const file = platform.src;

try {
platform.dest.forEach(function (destinationPath) {
var folder = destinationPath.substring(0, destinationPath.lastIndexOf('/'));
fs.ensureDirSync(folder);
fs.writeFileSync(destinationPath, contents);
});
} catch (e) {
// skip
}
} catch (err) {
console.log(err);
}
if (this.fileExists(file)) {
const contents = fs.readFileSync(file).toString();

break;
try {
platform.dest.forEach(function (destinationPath) {
const folder = destinationPath.substring(0, destinationPath.lastIndexOf('/'));
fs.ensureDirSync(folder);
fs.writeFileSync(destinationPath, contents);
});
} catch (e) {
// skip
}
}
},

getValue: function (config, name) {
var value = config.match(new RegExp('<' + name + '(.*?)>(.*?)</' + name + '>', 'i'));
if (value && value[2]) {
return value[2]
} else {
return null
console.warn(chalk.yellow.bold('File: [' + file + '] not found. Please checkout',
'https://github.com/wizpanda/cordova-plugin-firebase-lib#setup'));
}
},

Expand Down

0 comments on commit 5b1a526

Please sign in to comment.