@embrace-io/react-native
Version:
A React Native wrapper for the Embrace SDK
122 lines (121 loc) • 6.94 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 });
exports.withAndroidEmbraceOnCreate = exports.withAndroidEmbraceApplySwazzlerPlugin = exports.withAndroidEmbraceSwazzlerDependency = exports.withAndroidEmbraceJSONConfig = void 0;
const config_plugins_1 = require("@expo/config-plugins");
const textUtils_1 = require("./textUtils");
const fileUtils_1 = require("./fileUtils");
const path = require("path");
const androidBuildToolsRE = /(\s*)classpath.*com\.android\.tools\.build:gradle/;
const androidLegacyPluginRE = /(\s*)apply plugin.*com\.android\.application/;
const androidPluginRE = /(\s*)id.*\(.*com\.android\.application.*\)\.*/;
const importAndroidAppRE = /(\s*)import android\.app\.Application/;
const onCreateRE = /(\s*)super\.onCreate\(\)/;
const withAndroidEmbraceJSONConfig = (expoConfig, props) => {
return (0, config_plugins_1.withDangerousMod)(expoConfig, [
"android",
(config) => __awaiter(void 0, void 0, void 0, function* () {
const filePath = path.join(config.modRequest.platformProjectRoot, "app", "src", "main", "embrace-config.json");
(0, fileUtils_1.writeIfNotExists)(filePath, JSON.stringify({
app_id: props.androidAppId,
api_token: props.apiToken,
sdk_config: Object.assign({ app_framework: "react_native" }, props.androidSDKConfig),
}, null, 2), "withAndroidEmbraceJSONConfig");
return config;
}),
]);
};
exports.withAndroidEmbraceJSONConfig = withAndroidEmbraceJSONConfig;
const withAndroidEmbraceSwazzlerDependency = expoConfig => {
return (0, config_plugins_1.withProjectBuildGradle)(expoConfig, (config) => __awaiter(void 0, void 0, void 0, function* () {
const lines = config.modResults.contents.split("\n");
// Don't insert the dependency again if it already has it
if ((0, textUtils_1.hasMatch)(lines, "embrace-swazzler")) {
return config;
}
const success = (0, textUtils_1.addAfter)(lines,
// Look for a dependency on 'com.android.tools.build:gradle', which all projects should have, so that we can
// add our own dependency underneath
androidBuildToolsRE,
// https://developer.android.com/build/migrate-to-kotlin-dsl#convert-strings
`classpath("io.embrace:embrace-swazzler:\${findProject(':embrace-io_react-native').properties['emb_android_sdk']}")`);
if (!success) {
throw new Error("failed to insert a dependency for the Embrace Swazzler in the project's gradle file");
}
config.modResults.contents = lines.join("\n");
return config;
}));
};
exports.withAndroidEmbraceSwazzlerDependency = withAndroidEmbraceSwazzlerDependency;
const withAndroidEmbraceApplySwazzlerPlugin = expoConfig => {
return (0, config_plugins_1.withAppBuildGradle)(expoConfig, (config) => __awaiter(void 0, void 0, void 0, function* () {
const lines = config.modResults.contents.split("\n");
// Don't add the apply plugin line again if it's already there
if ((0, textUtils_1.hasMatch)(lines, "embrace-swazzler")) {
return config;
}
// Look for the 'com.android.application' plugin being applied, which all projects should do, so that we can
// apply our plugin underneath. Check for both the legacy and plugins block method of adding plugins:
// https://developer.android.com/build/migrate-to-kotlin-dsl#migrate-buildscript
const addLegacyPlugin = (0, textUtils_1.addAfter)(lines, androidLegacyPluginRE, 'apply plugin: "embrace-swazzler"');
if (addLegacyPlugin) {
config.modResults.contents = lines.join("\n");
return config;
}
const addPlugin = (0, textUtils_1.addAfter)(lines, androidPluginRE, 'id("embrace-swazzler")');
if (!addPlugin) {
throw new Error("failed to apply the Embrace Swazzler plugin in the project's app gradle file");
}
config.modResults.contents = lines.join("\n");
return config;
}));
};
exports.withAndroidEmbraceApplySwazzlerPlugin = withAndroidEmbraceApplySwazzlerPlugin;
const withAndroidEmbraceOnCreate = expoConfig => {
return (0, config_plugins_1.withMainApplication)(expoConfig, config => {
const lines = config.modResults.contents.split("\n");
const language = config.modResults.language;
// Don't add the Embrace initialize line again if it's already there
if ((0, textUtils_1.hasMatch)(lines, "Embrace")) {
return config;
}
const addedImport = (0, textUtils_1.addAfter)(lines,
// Look for the import of android.app.Application which should happen in the MainApplication file
// and add our import underneath
importAndroidAppRE, `import io.embrace.android.embracesdk.Embrace${language === "java" ? ";" : ""}`);
if (!addedImport) {
throw new Error("failed to add the Embrace import to the MainApplication file");
}
const addedInit = (0, textUtils_1.addAfter)(lines,
// Want the Embrace SDK initialization to happen right after the super.OnCreate() call in the
// Application.onCreate() method
onCreateRE, `Embrace.getInstance().start(this)${language === "java" ? ";" : ""}`);
if (!addedInit) {
throw new Error("failed to add the Embrace initialization to the MainApplication onCreate method");
}
config.modResults.contents = lines.join("\n");
return config;
});
};
exports.withAndroidEmbraceOnCreate = withAndroidEmbraceOnCreate;
const withAndroidEmbrace = (config, props) => {
try {
config = withAndroidEmbraceJSONConfig(config, props);
config = withAndroidEmbraceSwazzlerDependency(config, props);
config = withAndroidEmbraceApplySwazzlerPlugin(config, props);
config = withAndroidEmbraceOnCreate(config, props);
}
catch (e) {
config_plugins_1.WarningAggregator.addWarningAndroid("@embrace-io/expo-config-plugin", e instanceof Error ? e.message : "", "https://embrace.io/docs/react-native/integration/add-embrace-sdk/");
}
return config;
};
exports.default = withAndroidEmbrace;