react-native-expo-moengage
Version:
MoEngage Expo plugin for integrating MoEngage React-Native SDK
101 lines (100 loc) • 5.25 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const config_plugins_1 = require("@expo/config-plugins");
const constants_1 = require("./constants");
const utils_1 = require("./utils");
const packageJsonFile = require('../../package.json');
const withMoEngageAndroid = (config, props) => {
console.log('Running withMoEngageAndroid');
config = withMoEngageAndroidManifest(config, props);
config = withMoEngageInitialisationConfigResource(config, props);
config = withMoEngageDependencies(config, props);
return config;
};
/**
* Update the AndroidManifest.xml file with MoEngage specific configurations.
*/
const withMoEngageAndroidManifest = (config, props) => {
console.log('Running withMoEngageAndroidManifest');
if (!props.android.disableMoEngageDefaultBackupFile) {
(0, config_plugins_1.withAndroidManifest)(config, config => {
const mainApplication = config_plugins_1.AndroidConfig.Manifest.getMainApplicationOrThrow(config.modResults);
console.log('Adding manifest application keys');
constants_1.manifestApplicationKeys.forEach(item => {
const key = Object.keys(item)[0];
if (key !== undefined) {
const value = item[key];
if (!mainApplication.$) {
const androidName = mainApplication["android:name"] || "";
mainApplication.$ = { "android:name": androidName };
}
if (!(key in mainApplication.$)) {
mainApplication.$[key] = value;
}
}
});
return config;
});
}
if (props.android.shouldIncludeMoEngageFirebaseMessagingService) {
(0, config_plugins_1.withAndroidManifest)(config, config => {
const mainApplication = config_plugins_1.AndroidConfig.Manifest.getMainApplicationOrThrow(config.modResults);
if (props.android.isExpoNotificationIntegration) {
console.log('Adding Expo Notification Service to manifest');
(0, utils_1.addServiceToManifestIfNotExist)(mainApplication, constants_1.moEngageExpoNotificationServiceEntry, constants_1.moEngageExpoNotificationServiceName);
}
else {
console.log('Adding MoEngage FCM Service to manifest');
(0, utils_1.addServiceToManifestIfNotExist)(mainApplication, constants_1.moEngageFCMServiceEntry, constants_1.moEngageFCMServiceName);
}
return config;
});
}
return config;
};
/**
* Copy all the required resources for MoEngage initialisation to native Android module.
*/
const withMoEngageInitialisationConfigResource = (config, props) => {
console.log('Running withMoEngageInitialisationConfigResource');
return (0, config_plugins_1.withDangerousMod)(config, [
'android',
(config) => __awaiter(void 0, void 0, void 0, function* () {
if (props.android.smallIconPath !== undefined) {
console.log('Copying small icon:', props.android.smallIconPath);
yield (0, utils_1.copyFile)(props.android.smallIconPath, constants_1.drawableResourcePath);
}
if (props.android.largeIconPath !== undefined) {
console.log('Copying large icon:', props.android.largeIconPath);
yield (0, utils_1.copyFile)(props.android.largeIconPath, constants_1.drawableResourcePath);
}
console.log('Copying config file:', props.android.configFilePath);
yield (0, utils_1.copyFile)(props.android.configFilePath, constants_1.xmlValuesResourcePath);
return config;
}),
]);
};
/**
* Add required dependencies to app/build.gradle
*/
const withMoEngageDependencies = (config, props) => {
if (props.android.includeFirebaseMessagingDependencies) {
const firebaseMessagingVersion = packageJsonFile.dependencyVersions.firebaseMessaging;
console.log('Adding Firebase Messaging dependency:', firebaseMessagingVersion);
return (0, config_plugins_1.withAppBuildGradle)(config, gradleConfig => {
gradleConfig.modResults.contents = (0, utils_1.addDependencyToGradle)(gradleConfig.modResults.contents, constants_1.googleFirebaseMessagingGroup, constants_1.googleFirebaseMessagingModule, firebaseMessagingVersion);
return gradleConfig;
});
}
return config;
};
exports.default = withMoEngageAndroid;