@embrace-io/react-native
Version:
A React Native wrapper for the Embrace SDK
161 lines (159 loc) • 7.47 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.addEmbraceInitializerSwift = exports.addUploadBuildPhase = exports.patchXcodeBundlePhase = exports.iosPodfile = exports.patchPodfile = exports.iosInitializeEmbrace = exports.tryToPatchAppDelegate = void 0;
const ios_1 = require("../util/ios");
const EmbraceLogger_1 = require("../../src/utils/EmbraceLogger");
const patch_1 = require("./patches/patch");
const common_1 = require("./common");
const path = require("path");
const fs = require("fs");
const semverGte = require("semver/functions/gte");
const logger = new EmbraceLogger_1.default(console);
const tryToPatchAppDelegate = (_a) => __awaiter(void 0, [_a], void 0, function* ({ name, }) {
const project = yield (0, ios_1.xcodePatchable)({ name });
const bridgingHeader = project.getBridgingHeaderName(name);
const response = (0, patch_1.default)("objectivec", name, { bridgingHeader });
if (response) {
return project.addBridgingHeader(name);
}
else {
return (0, patch_1.default)("swift", name) || false;
}
});
exports.tryToPatchAppDelegate = tryToPatchAppDelegate;
exports.iosInitializeEmbrace = {
name: "iOS initialize Embrace",
run: (wizard) => __awaiter(void 0, void 0, void 0, function* () {
const pJSON = yield wizard.fieldValue(common_1.packageJSON);
const name = pJSON.name;
return (0, exports.tryToPatchAppDelegate)({ name });
}),
docURL: "https://embrace.io/docs/react-native/integration/add-embrace-sdk/?platform=ios#manually",
};
const patchPodfile = (json) => {
const rnVersion = (json.dependencies || {})["react-native"];
if (!rnVersion) {
throw Error("react-native dependency was not found");
}
const rnVersionSanitized = rnVersion.replace("^", "");
// If 6.0.0, autolink should have linked the Pod.
if (semverGte("6.0.0", rnVersionSanitized)) {
logger.log("skipping patching Podfile since react-native is on an autolink supported version");
return;
}
return (0, ios_1.podfilePatchable)().then(podfile => {
if (podfile.hasLine(ios_1.embraceNativePod)) {
logger.warn("Already has EmbraceIO pod");
return;
}
podfile.addBefore("use_react_native", `${ios_1.embraceNativePod}\n`);
return podfile.patch();
});
};
exports.patchPodfile = patchPodfile;
exports.iosPodfile = {
name: "Podfile patch (ONLY React Native Version < 0.6)",
run: (wizard) => wizard.fieldValue(common_1.packageJSON).then(exports.patchPodfile),
docURL: "https://embrace.io/docs/react-native/integration/add-embrace-sdk/?platform=ios#native-modules",
};
exports.patchXcodeBundlePhase = {
name: "Update bundle phase",
run: (wizard) => wizard
.fieldValue(common_1.packageJSON)
.then(json => (0, ios_1.xcodePatchable)(json))
.then(project => {
const bundlePhaseKey = project.findPhase(ios_1.bundlePhaseRE);
if (!bundlePhaseKey) {
logger.error("Could not find Xcode React Native bundle phase");
return;
}
if (project.hasLine(bundlePhaseKey, ios_1.exportSourcemapRNVariable)) {
logger.warn("already patched Xcode React Native bundle phase");
return;
}
logger.log("Patching Xcode React Native bundle phase");
project.modifyPhase(bundlePhaseKey, /^.*?\/(packager|scripts)\/react-native-xcode\.sh\s*/m, `${ios_1.makeSourcemapDirectory}\n${ios_1.exportSourcemapRNVariable}\n`);
return project.patch();
}),
docURL: "https://embrace.io/docs/react-native/integration/upload-symbol-files/#uploading-source-maps",
};
exports.addUploadBuildPhase = {
name: "Add upload phase",
run: (wizard) => wizard.fieldValue(common_1.packageJSON).then(json => {
return (0, ios_1.xcodePatchable)(json).then(project => {
const uploadBuildPhaseKey = project.findPhase(ios_1.embRunScript);
if (uploadBuildPhaseKey) {
logger.warn("already added upload phase");
return;
}
return wizard.fieldValueList([common_1.iosAppID, common_1.apiToken]).then(list => {
const [id, token] = list;
const proj = project.project;
proj.addBuildPhase([], "PBXShellScriptBuildPhase", "Upload Debug Symbols to Embrace", null, {
shellPath: "/bin/sh",
shellScript: `REACT_NATIVE_MAP_PATH="$CONFIGURATION_BUILD_DIR/embrace-assets/main.jsbundle.map" EMBRACE_ID=${id} EMBRACE_TOKEN=${token} ${ios_1.embRunScript}`,
});
return project.patch();
});
});
}),
docURL: "https://embrace.io/docs/react-native/integration/upload-symbol-files/#uploading-native-and-javascript-symbol-files",
};
exports.addEmbraceInitializerSwift = {
name: "Adding EmbraceInitializer.swift",
run: (wizard) => __awaiter(void 0, void 0, void 0, function* () {
const [appId, json] = yield wizard.fieldValueList([common_1.iosAppID, common_1.packageJSON]);
const { name } = json;
const project = yield (0, ios_1.xcodePatchable)({ name });
const filePath = path.join("ios", name, "EmbraceInitializer.swift");
try {
const fd = fs.openSync(filePath, "wx");
fs.writeFileSync(fd, getEmbraceInitializerContents(appId));
}
catch (e) {
if (e instanceof Error && e.message.includes("EEXIST")) {
logger.warn("EmbraceInitializer.swift already exists");
return;
}
else {
throw e;
}
}
const nameWithCaseSensitive = (0, ios_1.findNameWithCaseSensitiveFromPath)(project.path, name);
project.addFile(nameWithCaseSensitive, `${nameWithCaseSensitive}/EmbraceInitializer.swift`, "source");
return project.patch();
}),
docURL: "https://embrace.io/docs/react-native/integration/add-embrace-sdk/#manually",
};
const getEmbraceInitializerContents = (appId) => {
return `import Foundation
import EmbraceIO
@objcMembers class EmbraceInitializer: NSObject {
// Start the EmbraceSDK with the minimum required settings, for more advanced configuration options see:
// https://embrace.io/docs/ios/open-source/integration/embrace-options/
static func start() -> Void {
do {
try Embrace
.setup(
options: Embrace.Options(
appId: "${appId}",
platform: .reactNative
)
)
.start()
} catch let e {
print("Error starting Embrace \\(e.localizedDescription)")
}
}
}
`;
};