react-native-code-push-ota-updates
Version:
A custom CodePush-like SDK for React Native
42 lines (39 loc) • 1.99 kB
JavaScript
import RNFS from "react-native-fs";
import { Platform, NativeModules, Image } from "react-native";
import { unzip } from "react-native-zip-archive";
import DeviceInfo from "react-native-device-info";
const CodePushSDK = {
downloadUpdate: async (orgId, deploymentKey) => {
const versionCode = DeviceInfo.getVersion();
const build = DeviceInfo.getBuildNumber();
const version = `${versionCode}.${build}`;
const CODE_PUSH_BUNDLE_LOCATION = "CodePush/xyteqitrd/";
const CODEPUSH_BUNDLE_NAME = `${CODE_PUSH_BUNDLE_LOCATION}update.zip`;
const bundleName =
Platform.OS === "android" ? "index.android.bundle" : "main.jsbundle";
const CODEPUSH_DIR = `${RNFS.DocumentDirectoryPath}`;
const CODEPUSH_BUNDLE_PATH = `${CODEPUSH_DIR}/${CODEPUSH_BUNDLE_NAME}`;
const downloadUrl = `http://192.168.3.102:5001/v1/bundle/orgs/${orgId}/deployments/${deploymentKey}/bundles/${version}/${Platform.OS}/download`;
const destExists = await RNFS.exists(CODEPUSH_BUNDLE_PATH);
if (!destExists) {
console.warn("⚠️ Des assets folder does not exist:");
}
await RNFS.mkdir(`${RNFS.DocumentDirectoryPath}/CodePush/xyteqitrd`);
const downloadRes = await RNFS.downloadFile({
fromUrl: 'https://storage.googleapis.com/dope-doctors/codepush/bundle.zip',
toFile: `${RNFS.DocumentDirectoryPath}/CodePush/xyteqitrd/update.zip`
}).promise;
if (downloadRes.statusCode !== 200) {
console.log("Failed to download update.,downloadRes", downloadRes);
}
const ACTUAL_BUNDLE_LOCATION = `${CODEPUSH_DIR}/${CODE_PUSH_BUNDLE_LOCATION}`;
await unzip(CODEPUSH_BUNDLE_PATH, ACTUAL_BUNDLE_LOCATION).catch((error) => {
console.log("error>>", error);
});
const bundlePath = `${CODEPUSH_DIR}/${ACTUAL_BUNDLE_LOCATION}${bundleName}`;
// const ASSETS_DIR = `${CODEPUSH_DIR}/${CODE_PUSH_BUNDLE_LOCATION}assets`;
// await CodePushSDK.handleAssets(ASSETS_DIR);
return bundlePath;
}
};
export default CodePushSDK;