UNPKG

@config-plugins/react-native-branch

Version:

Config plugin to auto configure react-native-branch on prebuild

119 lines (118 loc) 4.35 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.withBranchIOS = void 0; exports.getBranchApiKey = getBranchApiKey; exports.setBranchApiKey = setBranchApiKey; exports.addBridgingHeaderImport = addBridgingHeaderImport; const generateCode_1 = require("@expo/config-plugins/build/utils/generateCode"); const config_plugins_1 = require("expo/config-plugins"); const glob_1 = require("glob"); const fs = __importStar(require("node:fs")); const path = __importStar(require("node:path")); function getBranchApiKey(config) { return config.ios?.config?.branch?.apiKey ?? null; } function setBranchApiKey(apiKey, infoPlist) { if (apiKey === null) { return infoPlist; } return { ...infoPlist, branch_key: { live: apiKey, }, }; } function addBridgingHeaderImport(src) { return (0, generateCode_1.mergeContents)({ tag: "react-native-branch", src, newSrc: "#import <React/RCTBridge.h>", anchor: /\/\//, offset: 4, comment: "//", }); } const withBranchIOS = (config, data) => { // Ensure object exist if (!config.ios) { config.ios = {}; } const apiKey = data.apiKey ?? getBranchApiKey(config); if (!apiKey) { throw new Error("Branch API key is required: expo.ios.config.branch.apiKey"); } // Add `React/RCTBridge` to bridging header (0, config_plugins_1.withDangerousMod)(config, [ "ios", async (config) => { if (!config.modRequest.projectName) { return config; } const [using] = (0, glob_1.globSync)("*-Bridging-Header.h", { absolute: true, cwd: path.join(config.modRequest.platformProjectRoot, config.modRequest.projectName), }); if (!using) { throw new Error("Cannot find bridging header. Please make sure you have a bridging header in your project."); } const src = await fs.promises.readFile(using, "utf-8"); const res = addBridgingHeaderImport(src); await fs.promises.writeFile(using, res.contents, "utf-8"); return config; }, ]); // Update the infoPlist with the branch key and branch domain config = (0, config_plugins_1.withInfoPlist)(config, (config) => { config.modResults = setBranchApiKey(apiKey, config.modResults); if (data.iosAppDomain) { config.modResults.branch_app_domain = data.iosAppDomain; } else { delete config.modResults.branch_app_domain; } if (data.iosUniversalLinkDomains) { config.modResults.branch_universal_link_domains = data.iosUniversalLinkDomains; } else { delete config.modResults.branch_universal_link_domains; } return config; }); return config; }; exports.withBranchIOS = withBranchIOS;