smartech-base-expo-plugin
Version:
Smartech Base Expo SDK's React Native Plugin For React Native Projects.
184 lines (183 loc) • 9.19 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.withNetcoreiOS = void 0;
const config_plugins_1 = require("@expo/config-plugins");
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const fs_2 = __importDefault(require("fs"));
const iosConstants_1 = require("./iosConstants");
const helper_1 = require("./helper");
// Set custom entitlements plist
const modifyEntitlementsPlist = (config, props) => {
return (0, config_plugins_1.withEntitlementsPlist)(config, async (newConfig) => {
newConfig.modResults = {
...newConfig.modResults,
"com.apple.security.application-groups": [`group.${props.ios.groupIdentifier}`],
};
return newConfig;
});
};
// Set custom info plist
const modifyInfoPlist = (config, props) => {
return (0, config_plugins_1.withInfoPlist)(config, (newConfig) => {
newConfig.modResults = {
...newConfig.modResults,
SmartechKeys: {
SmartechAppGroup: `group.${props.ios.groupIdentifier}`,
SmartechAppId: props.ios.appId,
SmartechAutoFetchLocation: props.ios.autoFetchLocation,
SmartechUseAdvId: props.ios.useAdvID,
},
...(props.ios.smartechNudges.smartechNudgesEnabled && {
HanselKeys: {
HanselAppId: props.ios.smartechNudges.appId,
HanselAppKey: props.ios.smartechNudges.appKey,
}
})
};
return newConfig;
});
};
const addHanselURLScheme = (config, props) => {
return (0, config_plugins_1.withInfoPlist)(config, (newConfig) => {
const modResults = newConfig.modResults;
const existingURLTypes = newConfig.modResults.CFBundleURLTypes || [];
if (!props.ios.smartechNudges.smartechNudgesEnabled || !props.ios.smartechNudges.addTestDevice) {
return newConfig;
}
if (props.ios.smartechNudges?.testDeviceURLName == undefined || props.ios.smartechNudges?.testDeviceURLSchema == undefined) {
console.log(`Smartech Nudge Url scheme ${props.ios.smartechNudges?.testDeviceURLName} and ${props.ios.smartechNudges?.testDeviceURLSchema}`);
return newConfig;
}
const newEntry = {
CFBundleTypeRole: "Editor",
CFBundleURLName: props.ios.smartechNudges.testDeviceURLName,
CFBundleURLSchemes: [props.ios.smartechNudges.testDeviceURLSchema || ""],
};
const isNewEntryAlreadyAdded = existingURLTypes.some((entry) => {
return (entry.CFBundleURLName === newEntry.CFBundleURLName &&
entry.CFBundleURLSchemes.includes(newEntry.CFBundleURLSchemes[0]));
});
if (!isNewEntryAlreadyAdded) {
existingURLTypes.push(newEntry);
modResults.CFBundleURLTypes = existingURLTypes;
}
return newConfig;
});
};
const addSmartechHeaderMod = (config, props) => {
return (0, config_plugins_1.withAppDelegate)(config, async (config) => {
const { platformProjectRoot, projectName } = config.modRequest;
if (projectName && platformProjectRoot) {
try {
const appDelegateHeaderPath = path_1.default.join(platformProjectRoot, projectName, iosConstants_1.appdelegateSnippet.fileName);
const appDelegateHeaderContents = await fs_1.promises.readFile(appDelegateHeaderPath, 'utf-8');
// Add Smartech header snipper if not already present.
if (!appDelegateHeaderContents.includes(iosConstants_1.appdelegateSnippet.smartechHeaderSnippet)) {
const modifiedAppDelegateHeaderContents = `${appDelegateHeaderContents}\n${iosConstants_1.appdelegateSnippet.smartechHeaderSnippet}`;
await fs_1.promises.writeFile(appDelegateHeaderPath, modifiedAppDelegateHeaderContents, 'utf-8');
}
else {
console.log(`${iosConstants_1.appdelegateSnippet.smartechHeaderSnippet} is already added.`);
}
}
catch (error) {
console.error('Error modifying AppDelegate.h:', error);
}
}
return config;
});
};
const addSmartechHanseleHeaderMod = (config, props) => {
return (0, config_plugins_1.withAppDelegate)(config, async (config) => {
const { platformProjectRoot, projectName } = config.modRequest;
if (platformProjectRoot && projectName) {
try {
const appDelegateHeaderPath = path_1.default.join(platformProjectRoot, projectName, iosConstants_1.appdelegateSnippet.fileName);
const appDelegateHeaderContents = await fs_1.promises.readFile(appDelegateHeaderPath, 'utf-8');
// Add Hansel header snippet if Hansel is enabled and not already present
if (props.ios.smartechNudges.smartechNudgesEnabled && !appDelegateHeaderContents.includes(iosConstants_1.appdelegateSnippet.hanselHeaderSnippet)) {
const modifiedAppDelegateHeaderContents = `${appDelegateHeaderContents}\n${iosConstants_1.appdelegateSnippet.hanselHeaderSnippet}`;
await fs_1.promises.writeFile(appDelegateHeaderPath, modifiedAppDelegateHeaderContents, 'utf-8');
}
else {
console.log(`${iosConstants_1.appdelegateSnippet.hanselHeaderSnippet} is already added or status of SmartechNudge: ${props.ios.smartechNudges.smartechNudgesEnabled}`);
}
}
catch (error) {
console.error('Error modifying AppDelegate.h:', error);
}
}
return config;
});
};
// Set custom AppDelegate contents modifications
const setCustomAppDelegateContentsMod = (config, props) => {
return (0, config_plugins_1.withAppDelegate)(config, (cfg) => {
const { modResults } = cfg;
const { contents } = modResults;
const appDelegatePath = cfg.modResults.path;
const smartechInitContent = addSmartechInitCode(contents, props);
const updatedContent = helper_1.Helper.Ios.addDeepLinkHandler(smartechInitContent, props);
fs_2.default.writeFileSync(appDelegatePath, updatedContent, 'utf-8');
modResults.contents = updatedContent;
return cfg;
});
};
// Helper function to modify AppDelegate contents
const addSmartechInitCode = (contents, props) => {
const lines = contents.split("\n");
const didLaunchIndex = lines.findIndex(line => iosConstants_1.smartechBaseSnippet.regexPattern.test(line));
if (didLaunchIndex === -1) {
return contents;
}
// Prepare the lines to insert
const sdkLines = [
iosConstants_1.smartechBaseSnippet.smartechInit,
...(props.ios.isLogEnabled ? [iosConstants_1.smartechBaseSnippet.debugLevel] : []),
iosConstants_1.smartechBaseSnippet.trackAppInstallUpdateBySmartech,
...((props.ios.smartechNudges.smartechNudgesEnabled && props.ios.smartechNudges.isLogEnabled) ? [iosConstants_1.smartechBaseSnippet.hanselLogSnippet] : []),
];
// Add the snippets before and after the `didFinishLaunching` index
const resultLines = [
...lines.slice(0, didLaunchIndex + 2),
...sdkLines,
...lines.slice(didLaunchIndex + 2),
];
return resultLines.join("\n");
};
// Set custom AppDelegate contents modifications
const modifyAppDelegateWithLinker = (config, props) => {
if (!props.ios.smartechNudges.smartechNudgesEnabled || !props.ios.smartechNudges.addTestDevice) {
return config;
}
return (0, config_plugins_1.withAppDelegate)(config, (config) => {
const { modResults } = config;
let { contents } = modResults;
const codeBlock = `BOOL handleBySmartech = [[Smartech sharedInstance] application:application openURL:url options:options];
if(!handleBySmartech) {
return [super application:application openURL:url options:options] || [RCTLinkingManager application:application openURL:url options:options];
}
return YES;`;
const lines = contents.split("\n");
if (contents.includes("openURL:")) {
const modifiedContents = contents.replace("return [super application:application openURL:url options:options] || [RCTLinkingManager application:application openURL:url options:options];", `${codeBlock}`);
modResults.contents = modifiedContents;
}
return config;
});
};
const withNetcoreiOS = (config, props) => {
config = modifyEntitlementsPlist(config, props);
config = modifyInfoPlist(config, props);
config = addHanselURLScheme(config, props);
config = addSmartechHeaderMod(config, props);
config = addSmartechHanseleHeaderMod(config, props);
config = setCustomAppDelegateContentsMod(config, props);
config = modifyAppDelegateWithLinker(config, props);
return config;
};
exports.withNetcoreiOS = withNetcoreiOS;