fix: respect old config from expo-branch

This commit is contained in:
Giau. Tran Minh committed 2022-01-20 15:46:08 +07:00
1 parent 0b06baa2b4
commit d0e018601c
13 files changed
+186 -93

No files matched your search

+12 -5
View File
@@ -16,13 +16,23 @@
"assetBundlePatterns": ["**/*"],
"ios": {
"supportsTablet": true,
"bundleIdentifier": "dev.bacon.app"
"bundleIdentifier": "dev.bacon.app",
"config": {
"branch": {
"apiKey": "key_live_f9f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8"
}
}
},
"android": {
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#FFFFFF"
},
"config": {
"branch": {
"apiKey": "key_live_f9f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8"
}
},
"package": "com.bacon.app"
},
"web": {
@@ -57,10 +67,7 @@
],
"@config-plugins/detox",
"@config-plugins/react-native-adjust",
["@config-plugins/react-native-branch", {
"apiKey": "key_live_f9f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8",
"appDomain": "https://bacon.app.link"
}],
"@config-plugins/react-native-branch",
"@config-plugins/react-native-google-cast",
"@config-plugins/react-native-ble-plx",
"@config-plugins/react-native-webrtc",
+4 -4
View File
@@ -29,8 +29,8 @@ Next, rebuild your app as described in the ["Adding custom native code"](https:/
The plugin provides props for extra customization. Every time you change the props or plugins, you'll need to rebuild (and `prebuild`) the native app. If no extra properties are added, defaults will be used.
- `apiKey` (_string_): Branch API key. Required.
- `appDomain` (_string_): App Domain for iOS. Required.
- `apiKey` (_string_): Branch API key. Optional.
- `iosAppDomain` (_string_): App Domain for iOS. Optional.
#### Example
@@ -41,8 +41,8 @@ The plugin provides props for extra customization. Every time you change the pro
[
"@config-plugins/react-native-branch",
{
"apiKey": "xxxxxx",
"appDomain": "xxxxxx",
"apiKey": "key_live_f9f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8",
"iosAppDomain": "awesome-alternate.app.link",
}
]
]
+2 -2
View File
@@ -1,4 +1,4 @@
export declare type ConfigData = {
apiKey: string;
appDomain: string;
apiKey?: string;
iosAppDomain?: string;
};
+4 -5
View File
@@ -3,11 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
const config_plugins_1 = require("@expo/config-plugins");
const withBranchAndroid_1 = require("./withBranchAndroid");
const withBranchIOS_1 = require("./withBranchIOS");
const withBranch = (config, { apiKey, appDomain }) => {
return config_plugins_1.withPlugins(config, [
[withBranchIOS_1.withBranchIos, { apiKey, appDomain }],
[withBranchAndroid_1.withBranchAndroid, { apiKey }],
]);
const withBranch = (config, { apiKey, iosAppDomain } = {}) => {
config = withBranchAndroid_1.withBranchAndroid(config, { apiKey });
config = withBranchIOS_1.withBranchIOS(config, { apiKey, iosAppDomain });
return config;
};
let pkg = {
name: "branch",
+4 -2
View File
@@ -1,5 +1,6 @@
import { AndroidConfig } from "@expo/config-plugins";
import type { ConfigPlugin, ExportedConfigWithProps } from "@expo/config-plugins";
import type { ExpoConfig } from "@expo/config-types";
import type { MergeResults } from "@expo/config-plugins/build/utils/generateCode";
export declare function editMainApplication(config: ExportedConfigWithProps, action: (mainApplication: string) => string): Promise<void>;
export declare function editProguardRules(config: ExportedConfigWithProps, action: (mainApplication: string) => string): Promise<void>;
@@ -8,7 +9,8 @@ export declare function addBranchGetAutoInstance(src: string): MergeResults;
export declare function addBranchMainActivityImport(src: string, packageId: string): MergeResults;
export declare function addBranchInitSession(src: string): MergeResults;
export declare function addBranchOnNewIntent(src: string): MergeResults;
export declare function setCustomConfigAsync(config: ExportedConfigWithProps<AndroidConfig.Manifest.AndroidManifest>, androidManifest: AndroidConfig.Manifest.AndroidManifest, branchApiKey: string): Promise<AndroidConfig.Manifest.AndroidManifest>;
export declare function getBranchApiKey(config: ExpoConfig): string | null;
export declare function setBranchApiKey(apiKey: string, androidManifest: AndroidConfig.Manifest.AndroidManifest): AndroidConfig.Manifest.AndroidManifest;
export declare const withBranchAndroid: ConfigPlugin<{
apiKey: string;
apiKey?: string;
}>;
+50 -21
View File
@@ -1,19 +1,40 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.withBranchAndroid = exports.setCustomConfigAsync = exports.addBranchOnNewIntent = exports.addBranchInitSession = exports.addBranchMainActivityImport = exports.addBranchGetAutoInstance = exports.addBranchMainApplicationImport = exports.editProguardRules = exports.editMainApplication = void 0;
exports.withBranchAndroid = exports.setBranchApiKey = exports.getBranchApiKey = exports.addBranchOnNewIntent = exports.addBranchInitSession = exports.addBranchMainActivityImport = exports.addBranchGetAutoInstance = exports.addBranchMainApplicationImport = exports.editProguardRules = exports.editMainApplication = void 0;
const config_plugins_1 = require("@expo/config-plugins");
const generateCode_1 = require("@expo/config-plugins/build/utils/generateCode");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const assert_1 = __importDefault(require("assert"));
const { addMetaDataItemToMainApplication, getMainApplicationOrThrow, removeMetaDataItemFromMainApplication, } = config_plugins_1.AndroidConfig.Manifest;
const META_BRANCH_KEY = "io.branch.sdk.BranchKey";
async function readFileAsync(path) {
return fs_1.default.promises.readFile(path, "utf8");
return fs.promises.readFile(path, "utf8");
}
async function saveFileAsync(path, content) {
return fs_1.default.promises.writeFile(path, content, "utf8");
return fs.promises.writeFile(path, content, "utf8");
}
// Fork of config-plugins mergeContents, but appends the contents to the end of the file.
function appendContents({ src, newSrc, tag, comment, }) {
@@ -41,7 +62,7 @@ async function editMainApplication(config, action) {
var _a;
const packageName = (_a = config.android) === null || _a === void 0 ? void 0 : _a.package;
assert_1.default(packageName, "android.package must be defined");
const mainApplicationPath = path_1.default.join(config.modRequest.platformProjectRoot, "app", "src", "main", "java", ...packageName.split("."), "MainApplication.java");
const mainApplicationPath = path.join(config.modRequest.platformProjectRoot, "app", "src", "main", "java", ...packageName.split("."), "MainApplication.java");
try {
const mainApplication = action(await readFileAsync(mainApplicationPath));
return await saveFileAsync(mainApplicationPath, mainApplication);
@@ -52,7 +73,7 @@ async function editMainApplication(config, action) {
}
exports.editMainApplication = editMainApplication;
async function editProguardRules(config, action) {
const proguardRulesPath = path_1.default.join(config.modRequest.platformProjectRoot, "app", "proguard-rules.pro");
const proguardRulesPath = path.join(config.modRequest.platformProjectRoot, "app", "proguard-rules.pro");
try {
const proguardRules = action(await readFileAsync(proguardRulesPath));
return await saveFileAsync(proguardRulesPath, proguardRules);
@@ -173,24 +194,32 @@ function addBranchOnNewIntent(src) {
});
}
exports.addBranchOnNewIntent = addBranchOnNewIntent;
const { addMetaDataItemToMainApplication, getMainApplicationOrThrow } = config_plugins_1.AndroidConfig.Manifest;
// Splitting this function out of the mod makes it easier to test.
async function setCustomConfigAsync(config, androidManifest, branchApiKey) {
// Get the <application /> tag and assert if it doesn't exist.
function getBranchApiKey(config) {
var _a, _b, _c, _d;
return (_d = (_c = (_b = (_a = config.android) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.branch) === null || _c === void 0 ? void 0 : _c.apiKey) !== null && _d !== void 0 ? _d : null;
}
exports.getBranchApiKey = getBranchApiKey;
function setBranchApiKey(apiKey, androidManifest) {
const mainApplication = getMainApplicationOrThrow(androidManifest);
addMetaDataItemToMainApplication(mainApplication,
// value for `android:name`
"io.branch.sdk.BranchKey",
// value for `android:value`
branchApiKey);
if (apiKey) {
// If the item exists, add it back
addMetaDataItemToMainApplication(mainApplication, META_BRANCH_KEY, apiKey);
}
else {
// Remove any existing item
removeMetaDataItemFromMainApplication(mainApplication, META_BRANCH_KEY);
}
return androidManifest;
}
exports.setCustomConfigAsync = setCustomConfigAsync;
exports.setBranchApiKey = setBranchApiKey;
const withBranchAndroid = (config, data) => {
// Insert the branch_key into the AndroidManifest
config = config_plugins_1.withAndroidManifest(config, async (config) => {
// Modifiers can be async, but try to keep them fast.
config.modResults = await setCustomConfigAsync(config, config.modResults, data.apiKey);
var _a;
const apiKey = (_a = data.apiKey) !== null && _a !== void 0 ? _a : getBranchApiKey(config);
if (!apiKey) {
throw new Error("Branch API key is required");
}
config = config_plugins_1.withAndroidManifest(config, (config) => {
config.modResults = setBranchApiKey(apiKey, config.modResults);
return config;
});
// Directly edit MainApplication.java
+6 -3
View File
@@ -1,8 +1,11 @@
import { MergeResults } from "@expo/config-plugins/build/utils/generateCode";
import type { ConfigPlugin } from "@expo/config-plugins";
import type { ConfigPlugin, InfoPlist } from "@expo/config-plugins";
import type { ExpoConfig } from '@expo/config-types';
import type { MergeResults } from "@expo/config-plugins/build/utils/generateCode";
import type { ConfigData } from "./types";
export declare function addBranchAppDelegateImport(src: string): MergeResults;
export declare function addBranchAppDelegateInit(src: string): MergeResults;
export declare function addBranchAppDelegateOpenURL(src: string): MergeResults;
export declare function addBranchAppDelegateContinueUserActivity(src: string): MergeResults;
export declare const withBranchIos: ConfigPlugin<ConfigData>;
export declare function getBranchApiKey(config: Pick<ExpoConfig, 'ios'>): string | null;
export declare function setBranchApiKey(apiKey: string, infoPlist: InfoPlist): InfoPlist;
export declare const withBranchIOS: ConfigPlugin<ConfigData>;
+29 -7
View File
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.withBranchIos = exports.addBranchAppDelegateContinueUserActivity = exports.addBranchAppDelegateOpenURL = exports.addBranchAppDelegateInit = exports.addBranchAppDelegateImport = void 0;
exports.withBranchIOS = exports.setBranchApiKey = exports.getBranchApiKey = exports.addBranchAppDelegateContinueUserActivity = exports.addBranchAppDelegateOpenURL = exports.addBranchAppDelegateInit = exports.addBranchAppDelegateImport = void 0;
const config_plugins_1 = require("@expo/config-plugins");
const generateCode_1 = require("@expo/config-plugins/build/utils/generateCode");
function addBranchAppDelegateImport(src) {
@@ -62,17 +62,39 @@ function addBranchAppDelegateContinueUserActivity(src) {
});
}
exports.addBranchAppDelegateContinueUserActivity = addBranchAppDelegateContinueUserActivity;
const withBranchIos = (config, data) => {
function getBranchApiKey(config) {
var _a, _b, _c, _d;
return (_d = (_c = (_b = (_a = config.ios) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.branch) === null || _c === void 0 ? void 0 : _c.apiKey) !== null && _d !== void 0 ? _d : null;
}
exports.getBranchApiKey = getBranchApiKey;
function setBranchApiKey(apiKey, infoPlist) {
if (apiKey === null) {
return infoPlist;
}
return {
...infoPlist,
branch_key: {
live: apiKey,
},
};
}
exports.setBranchApiKey = setBranchApiKey;
const withBranchIOS = (config, data) => {
var _a;
// Ensure object exist
if (!config.ios) {
config.ios = {};
}
const apiKey = (_a = data.apiKey) !== null && _a !== void 0 ? _a : getBranchApiKey(config);
if (!apiKey) {
throw new Error("Branch API key is required");
}
// Update the infoPlist with the branch key and branch domain
config = config_plugins_1.withInfoPlist(config, (config) => {
config.modResults.branch_app_domain = data.appDomain;
config.modResults.branch_key = {
live: data.apiKey,
};
config.modResults = setBranchApiKey(apiKey, config.modResults);
if (data.iosAppDomain) {
config.modResults.branch_app_domain = data.iosAppDomain;
}
return config;
});
// Update the AppDelegate.m
@@ -85,4 +107,4 @@ const withBranchIos = (config, data) => {
});
return config;
};
exports.withBranchIos = withBranchIos;
exports.withBranchIOS = withBranchIOS;
+2 -1
View File
@@ -27,7 +27,8 @@
"preset": "expo-module-scripts"
},
"dependencies": {
"@expo/config-plugins": "^4.0.7"
"@expo/config-plugins": "^4.0.7",
"@expo/config-types": "^43.0.1"
},
"devDependencies": {
"expo-module-scripts": "^2.0.0"
+2 -2
View File
@@ -1,4 +1,4 @@
export type ConfigData = {
apiKey: string;
appDomain: string;
apiKey?: string;
iosAppDomain?: string;
}
+6 -10
View File
@@ -1,18 +1,14 @@
import { createRunOncePlugin, withPlugins } from "@expo/config-plugins";
import { createRunOncePlugin } from "@expo/config-plugins";
import type { ConfigPlugin } from "@expo/config-plugins";
import { withBranchAndroid } from "./withBranchAndroid";
import { withBranchIos } from "./withBranchIOS";
import { withBranchIOS } from "./withBranchIOS";
import type { ConfigData } from "./types";
const withBranch: ConfigPlugin<ConfigData> = (config, { apiKey, appDomain }) => {
return withPlugins(
config,
[
[withBranchIos, { apiKey, appDomain }],
[withBranchAndroid, { apiKey }],
]
);
const withBranch: ConfigPlugin<ConfigData> = (config, { apiKey, iosAppDomain } = {}) => {
config = withBranchAndroid(config, { apiKey });
config = withBranchIOS(config, { apiKey, iosAppDomain });
return config;
};
let pkg: { name: string; version?: string } = {
@@ -9,16 +9,25 @@ import type {
ConfigPlugin,
ExportedConfigWithProps,
} from "@expo/config-plugins";
import type { ExpoConfig } from "@expo/config-types";
import {
createGeneratedHeaderComment,
mergeContents,
removeGeneratedContents,
} from "@expo/config-plugins/build/utils/generateCode";
import type { MergeResults } from "@expo/config-plugins/build/utils/generateCode";
import fs from "fs";
import path from "path";
import * as fs from "fs";
import * as path from "path";
import assert from "assert";
const {
addMetaDataItemToMainApplication,
getMainApplicationOrThrow,
removeMetaDataItemFromMainApplication,
} = AndroidConfig.Manifest;
const META_BRANCH_KEY = "io.branch.sdk.BranchKey";
async function readFileAsync(path: string) {
return fs.promises.readFile(path, "utf8");
}
@@ -175,7 +184,7 @@ export function addBranchInitSession(src: string): MergeResults {
comment: "//",
});
} catch (err) {
if (err.code !== "ERR_NO_MATCH") {
if ((err as any).code !== "ERR_NO_MATCH") {
throw err;
}
}
@@ -212,7 +221,7 @@ export function addBranchOnNewIntent(src: string): MergeResults {
comment: "//",
});
} catch (err) {
if (err.code !== "ERR_NO_MATCH") {
if ((err as any).code !== "ERR_NO_MATCH") {
throw err;
}
}
@@ -234,40 +243,41 @@ export function addBranchOnNewIntent(src: string): MergeResults {
});
}
const { addMetaDataItemToMainApplication, getMainApplicationOrThrow } =
AndroidConfig.Manifest;
// Splitting this function out of the mod makes it easier to test.
export async function setCustomConfigAsync(
config: ExportedConfigWithProps<AndroidConfig.Manifest.AndroidManifest>,
export function getBranchApiKey(config: ExpoConfig) {
return config.android?.config?.branch?.apiKey ?? null;
}
export function setBranchApiKey(
apiKey: string,
androidManifest: AndroidConfig.Manifest.AndroidManifest,
branchApiKey: string
) {
// Get the <application /> tag and assert if it doesn't exist.
const mainApplication = getMainApplicationOrThrow(androidManifest);
addMetaDataItemToMainApplication(
mainApplication,
// value for `android:name`
"io.branch.sdk.BranchKey",
// value for `android:value`
branchApiKey
);
if (apiKey) {
// If the item exists, add it back
addMetaDataItemToMainApplication(mainApplication, META_BRANCH_KEY, apiKey);
} else {
// Remove any existing item
removeMetaDataItemFromMainApplication(mainApplication, META_BRANCH_KEY);
}
return androidManifest;
}
export const withBranchAndroid: ConfigPlugin<{ apiKey: string }> = (
export const withBranchAndroid: ConfigPlugin<{ apiKey?: string }> = (
config,
data
) => {
// Insert the branch_key into the AndroidManifest
config = withAndroidManifest(config, async (config) => {
// Modifiers can be async, but try to keep them fast.
config.modResults = await setCustomConfigAsync(
config,
const apiKey = data.apiKey ?? getBranchApiKey(config);
if (!apiKey) {
throw new Error("Branch API key is required");
}
config = withAndroidManifest(config, (config) => {
config.modResults = setBranchApiKey(
apiKey,
config.modResults,
data.apiKey
);
return config;
});
@@ -1,6 +1,8 @@
import { withAppDelegate, withInfoPlist } from "@expo/config-plugins";
import { mergeContents, MergeResults } from "@expo/config-plugins/build/utils/generateCode";
import type { ConfigPlugin } from "@expo/config-plugins";
import type { ConfigPlugin, InfoPlist } from "@expo/config-plugins";
import type { ExpoConfig } from '@expo/config-types';
import { mergeContents } from "@expo/config-plugins/build/utils/generateCode";
import type { MergeResults } from "@expo/config-plugins/build/utils/generateCode";
import type { ConfigData } from "./types";
export function addBranchAppDelegateImport(src: string): MergeResults {
@@ -69,17 +71,39 @@ export function addBranchAppDelegateContinueUserActivity(src: string): MergeResu
});
}
export const withBranchIos: ConfigPlugin<ConfigData> = (config, data) =>{
export function getBranchApiKey(config: Pick<ExpoConfig, 'ios'>) {
return config.ios?.config?.branch?.apiKey ?? null;
}
export function setBranchApiKey(apiKey: string, infoPlist: InfoPlist): InfoPlist {
if (apiKey === null) {
return infoPlist;
}
return {
...infoPlist,
branch_key: {
live: apiKey,
},
};
}
export const withBranchIOS: ConfigPlugin<ConfigData> = (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");
}
// Update the infoPlist with the branch key and branch domain
config = withInfoPlist(config, (config) => {
config.modResults.branch_app_domain = data.appDomain;
config.modResults.branch_key = {
live: data.apiKey,
config.modResults = setBranchApiKey(apiKey, config.modResults);
if (data.iosAppDomain) {
config.modResults.branch_app_domain = data.iosAppDomain;
}
return config;
});