UNPKG

@embrace-io/react-native

Version:
162 lines (161 loc) 6.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTextToAddWithBreakingLine = exports.getText = exports.SUPPORTED_REMOVALS = exports.SUPPORTED_PATCHES = exports.EMBRACE_INIT_KOTLIN = exports.EMBRACE_IMPORT_KOTLIN = exports.EMBRACE_INIT_JAVA = exports.EMBRACE_IMPORT_JAVA = exports.EMBRACE_INIT_SWIFT = void 0; const ios_1 = require("../../util/ios"); const android_1 = require("../../util/android"); const EmbraceLogger_1 = require("../../../src/utils/EmbraceLogger"); const patch_ios_5x_1 = require("./patch_ios_5x"); const common_1 = require("./common"); exports.EMBRACE_INIT_SWIFT = "EmbraceInitializer.start()"; exports.EMBRACE_IMPORT_JAVA = "import io.embrace.android.embracesdk.Embrace;"; exports.EMBRACE_INIT_JAVA = "Embrace.getInstance().start(this);"; exports.EMBRACE_IMPORT_KOTLIN = "import io.embrace.android.embracesdk.Embrace"; exports.EMBRACE_INIT_KOTLIN = "Embrace.getInstance().start(this)"; const logger = new EmbraceLogger_1.default(console); const PATCH_IOS_SWIFT_APPDELEGATE = { fileName: common_1.MAIN_CLASS_BY_LANGUAGE.swift, textsToAdd: [ { searchText: /func\s+application\(\s*_\s*[^}]*\{/, textToAdd: exports.EMBRACE_INIT_SWIFT, order: "after", breakingLine: "before", }, ], findFileFunction: (language, projectName) => { if (!projectName) { logger.warn("The project name is required"); return undefined; } return (0, ios_1.getAppDelegateByIOSLanguage)(projectName, language); }, }; const PATCH_IOS_OBJECTIVEC_APPDELEGATE = { fileName: common_1.MAIN_CLASS_BY_LANGUAGE.objectivec, textsToAdd: [ { searchText: '#import "AppDelegate.h"', textToAdd: ios_1.EMBRACE_IMPORT_OBJECTIVEC, order: "after", breakingLine: "before", }, { searchText: /(-\s*\(BOOL\)\s*application:\s*\(UIApplication\s\*\)\s*(app|application)\s+didFinishLaunchingWithOptions:\s*\(NSDictionary\s*\*\)launchOptions\s*\{\s*)/, textToAdd: ios_1.EMBRACE_INIT_OBJECTIVEC, order: "after", breakingLine: "after", }, ], findFileFunction: (language, projectName) => { if (!projectName) { logger.warn("The project name is required"); return undefined; } return (0, ios_1.getAppDelegateByIOSLanguage)(projectName, language); }, }; const PATCH_ANDROID_KOTLIN_MAIN_ACTIVITTY = { fileName: common_1.MAIN_CLASS_BY_LANGUAGE.kotlin, textsToAdd: [ { searchText: "import android.app.Application", textToAdd: `${exports.EMBRACE_IMPORT_KOTLIN}`, order: "after", breakingLine: "before", }, { searchText: "super.onCreate()", textToAdd: `${exports.EMBRACE_INIT_KOTLIN}`, order: "after", breakingLine: "before", }, ], findFileFunction: (language) => (0, android_1.getMainApplicationPatchable)(language), }; const PATCH_ANDROID_JAVA_MAIN_ACTIVITTY = { fileName: common_1.MAIN_CLASS_BY_LANGUAGE.java, textsToAdd: [ { searchText: "import android.app.Application;", textToAdd: exports.EMBRACE_IMPORT_JAVA, order: "after", breakingLine: "before", }, { searchText: "super.onCreate();", textToAdd: exports.EMBRACE_INIT_JAVA, order: "after", breakingLine: "before", }, ], findFileFunction: (language) => (0, android_1.getMainApplicationPatchable)(language), }; exports.SUPPORTED_PATCHES = { objectivec: PATCH_IOS_OBJECTIVEC_APPDELEGATE, swift: PATCH_IOS_SWIFT_APPDELEGATE, java: PATCH_ANDROID_JAVA_MAIN_ACTIVITTY, kotlin: PATCH_ANDROID_KOTLIN_MAIN_ACTIVITTY, swift5x: undefined, objectivec5x: undefined, }; exports.SUPPORTED_REMOVALS = Object.assign(Object.assign({}, exports.SUPPORTED_PATCHES), { swift5x: patch_ios_5x_1.PATCH_IOS_SWIFT_APPDELEGATE_5X, objectivec5x: patch_ios_5x_1.PATCH_IOS_OBJECTIVEC_APPDELEGATE_5X, }); const getText = (item, dynamicTextParams) => typeof item.textToAdd === "function" ? item.textToAdd(dynamicTextParams || {}) : item.textToAdd; exports.getText = getText; const getTextToAddWithBreakingLine = (textToAdd, breakingLine, padding = "") => { if (Array.isArray(textToAdd)) { textToAdd = textToAdd.join(`\n${padding}`); } if (breakingLine === "both") { return `\n${padding}${textToAdd}\n${padding}`; } if (breakingLine === "before") { return `\n${padding}${textToAdd}`; } if (breakingLine === "after") { return `${textToAdd}\n${padding}`; } return textToAdd; }; exports.getTextToAddWithBreakingLine = getTextToAddWithBreakingLine; const patch = (language, projectName, dynamicTextParams) => { const patchDefinition = exports.SUPPORTED_PATCHES[language]; if (patchDefinition === undefined) { return logger.warn("This language is not supported"); } const { fileName, textsToAdd, findFileFunction } = patchDefinition; const file = findFileFunction(language, projectName); if (!file) { return logger.warn("The file to be patched not found"); } const result = textsToAdd.map(item => { var _a, _b; const { order, searchText, breakingLine } = item; let padding = ""; // If its a regex we take the spaces from the next breaking line to the next line if (searchText instanceof RegExp) { padding = (_a = file .getPaddingAfterStringToTheNextString(searchText)) === null || _a === void 0 ? void 0 : _a.replace(searchText, ""); } else { padding = (_b = file.getPaddingFromString(searchText)) === null || _b === void 0 ? void 0 : _b.replace(searchText, ""); } const finalTextToAdd = (0, exports.getTextToAddWithBreakingLine)((0, exports.getText)(item, dynamicTextParams || {}), breakingLine, padding); if (order === "after") { return (0, common_1.addLineAfterToTextInFile)(file, finalTextToAdd, searchText, fileName); } if (order === "before") { return (0, common_1.addLineBeforeToTextInFile)(file, finalTextToAdd, searchText, fileName); } }); const hasToPatch = result.some(item => item); if (hasToPatch) { file.patch(); } return hasToPatch; }; exports.default = patch;