@embrace-io/react-native
Version:
A React Native wrapper for the Embrace SDK
132 lines (131 loc) • 7.42 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.withAndroidEmbraceApplyGradlePlugin = exports.withAndroidEmbraceGradlePluginDependency = 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 withAndroidEmbraceGradlePluginDependency = expoConfig => {
return (0, config_plugins_1.withProjectBuildGradle)(expoConfig, (config) => __awaiter(void 0, void 0, void 0, function* () {
// Remove the legacy embrace-swazzler classpath if present
const lines = config.modResults.contents
.split("\n")
.filter(line => !line.includes("io.embrace:embrace-swazzler"));
// Don't insert the dependency again if it already has it
if ((0, textUtils_1.hasMatch)(lines, "embrace-gradle-plugin")) {
config.modResults.contents = lines.join("\n");
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-gradle-plugin:\${findProject(':embrace-io_react-native').properties['emb_android_sdk']}")`);
if (!success) {
throw new Error("failed to insert a dependency for the Embrace Gradle plugin in the project's gradle file");
}
config.modResults.contents = lines.join("\n");
return config;
}));
};
exports.withAndroidEmbraceGradlePluginDependency = withAndroidEmbraceGradlePluginDependency;
const withAndroidEmbraceApplyGradlePlugin = expoConfig => {
return (0, config_plugins_1.withAppBuildGradle)(expoConfig, (config) => __awaiter(void 0, void 0, void 0, function* () {
// Remove the legacy embrace-swazzler apply line if present
const lines = config.modResults.contents
.split("\n")
.filter(line => !line.includes("embrace-swazzler"));
// Don't add the apply plugin line again if it's already there
if ((0, textUtils_1.hasMatch)(lines, "io.embrace.gradle")) {
config.modResults.contents = lines.join("\n");
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: "io.embrace.gradle"');
if (addLegacyPlugin) {
config.modResults.contents = lines.join("\n");
return config;
}
const addPlugin = (0, textUtils_1.addAfter)(lines, androidPluginRE, 'id("io.embrace.gradle")');
if (!addPlugin) {
throw new Error("failed to apply the Embrace Gradle plugin in the project's app gradle file");
}
config.modResults.contents = lines.join("\n");
return config;
}));
};
exports.withAndroidEmbraceApplyGradlePlugin = withAndroidEmbraceApplyGradlePlugin;
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, language === "java"
? "Embrace.INSTANCE.start(this);"
: "Embrace.start(this)");
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 = withAndroidEmbraceGradlePluginDependency(config, props);
config = withAndroidEmbraceApplyGradlePlugin(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;