UNPKG

@appzung/expo-config-code-push

Version:

Config plugin to auto configure AppZung CodePush for React Native on prebuild

115 lines (114 loc) 5.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const config_plugins_1 = require("@expo/config-plugins"); const codeMod_1 = require("@expo/config-plugins/build/android/codeMod"); const generateCode_1 = require("@expo/config-plugins/build/utils/generateCode"); const withIos = (config, { ios }) => { if (!ios) { return config; } config = (0, config_plugins_1.withInfoPlist)(config, (config) => { if (!ios.CodePushReleaseChannelPublicId) { throw new Error('Missing ios CodePushReleaseChannelPublicId'); } config.modResults.CodePushReleaseChannelPublicId = ios.CodePushReleaseChannelPublicId; if (ios.CodePushSigningPublicKey) { config.modResults.CodePushSigningPublicKey = ios.CodePushSigningPublicKey; } return config; }); config = (0, config_plugins_1.withAppDelegate)(config, (config) => { const { modResults } = config; const { language } = modResults; if (language !== 'objc' && language !== 'objcpp') { throw new Error(`Cannot modify the project AppDelegate as it's not in a supported language: ${language}`); } config.modResults.contents = (0, generateCode_1.mergeContents)({ src: modResults.contents, comment: '//', tag: '@appzung/react-native-code-push-header', offset: 1, anchor: /#import "AppDelegate\.h"/, newSrc: '#import <CodePush/CodePush.h>', }).contents; config.modResults.contents = config.modResults.contents.replace(/return \[\[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];/, `// @appzung/react-native-code-push-bundle\n return [CodePush bundleURL];`); return config; }); return config; }; const withAndroid = (config, { android }) => { if (!android) { return config; } config = (0, config_plugins_1.withStringsXml)(config, (config) => { if (!android.CodePushReleaseChannelPublicId) { throw new Error('Missing android CodePushReleaseChannelPublicId'); } config_plugins_1.AndroidConfig.Strings.setStringItem([ { $: { name: 'CodePushReleaseChannelPublicId', translatable: 'false', }, _: android.CodePushReleaseChannelPublicId, }, ], config.modResults); if (android.CodePushSigningPublicKey) { config_plugins_1.AndroidConfig.Strings.setStringItem([ { $: { name: 'CodePushSigningPublicKey', translatable: 'false', }, _: android.CodePushSigningPublicKey, }, ], config.modResults); } return config; }); config = (0, config_plugins_1.withAppBuildGradle)(config, (config) => { if (config.modResults.language !== 'groovy') { throw new Error(`Cannot modify build.gradle if it's not groovy, received: ${config.modResults.language}`); } if (!config.modResults.contents.includes('@appzung/react-native-code-push-gradle')) { config.modResults.contents = config.modResults.contents + '\n' + '// @appzung/react-native-code-push-gradle' + '\n' + 'apply from: "../../node_modules/@appzung/react-native-code-push/android/codepush.gradle"' + '\n'; } return config; }); config = (0, config_plugins_1.withMainApplication)(config, (config) => { const { modResults } = config; const { language } = modResults; config.modResults.contents = (0, codeMod_1.addImports)(modResults.contents, ['com.appzung.codepush.react.CodePush'], language === 'java'); if (language === 'kt') { config.modResults.contents = (0, generateCode_1.mergeContents)({ src: modResults.contents, comment: '//', tag: '@appzung/react-native-code-push-main-application-kt', offset: 1, anchor: /override fun getUseDeveloperSupport\(\): Boolean = BuildConfig\.DEBUG/, newSrc: ` override fun getJSBundleFile(): String { return CodePush.getJSBundleFile() }`, }).contents; return config; } throw new Error(`Cannot modify MainApplication because the language "${language}" is not supported`); }); return config; }; const withAppZungCodePushPlugin = (config, props) => { config = withIos(config, props); config = withAndroid(config, props); return config; }; const pkg = { name: '@appzung/react-native-code-push', version: 'UNVERSIONED', }; exports.default = (0, config_plugins_1.createRunOncePlugin)(withAppZungCodePushPlugin, pkg.name, pkg.version);