react-native-codepush-sdk
Version:
A React Native CodePush SDK for over-the-air updates
125 lines (124 loc) • 4.89 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BundleManager = void 0;
const react_native_fs_1 = __importDefault(require("react-native-fs"));
const react_native_1 = require("react-native");
class BundleManager {
/**
* Get the path to the current bundle that should be loaded
*/
static getCurrentBundlePath() {
// In a real implementation, this would check if a custom bundle exists
// and return its path, otherwise return the original bundle path
return BundleManager.CUSTOM_BUNDLE_PATH;
}
/**
* Check if a custom bundle exists
*/
static async hasCustomBundle() {
try {
return await react_native_fs_1.default.exists(BundleManager.CUSTOM_BUNDLE_PATH);
}
catch (error) {
return false;
}
}
/**
* Copy a bundle from source to the current bundle location
*/
static async installBundle(sourcePath) {
try {
const bundlePath = `${sourcePath}/index.bundle`;
if (!(await react_native_fs_1.default.exists(bundlePath))) {
throw new Error('Bundle file not found in update package');
}
// Ensure directory exists
const bundleDir = BundleManager.CUSTOM_BUNDLE_PATH.substring(0, BundleManager.CUSTOM_BUNDLE_PATH.lastIndexOf('/'));
await react_native_fs_1.default.mkdir(bundleDir);
// Copy bundle
await react_native_fs_1.default.copyFile(bundlePath, BundleManager.CUSTOM_BUNDLE_PATH);
// Copy assets if they exist
const assetsSourcePath = `${sourcePath}/assets`;
const assetsDestPath = `${bundleDir}/assets`;
if (await react_native_fs_1.default.exists(assetsSourcePath)) {
// Custom folder copy implementation needed here. For now, this is a placeholder.
// TODO: Implement folder copy logic or use a third-party utility.
}
}
catch (error) {
console.error('Failed to install bundle:', error);
throw error;
}
}
/**
* Remove the current custom bundle and revert to original
*/
static async removeCustomBundle() {
try {
if (await react_native_fs_1.default.exists(BundleManager.CUSTOM_BUNDLE_PATH)) {
await react_native_fs_1.default.unlink(BundleManager.CUSTOM_BUNDLE_PATH);
}
// Also remove assets directory
const bundleDir = BundleManager.CUSTOM_BUNDLE_PATH.substring(0, BundleManager.CUSTOM_BUNDLE_PATH.lastIndexOf('/'));
const assetsPath = `${bundleDir}/assets`;
if (await react_native_fs_1.default.exists(assetsPath)) {
await react_native_fs_1.default.unlink(assetsPath);
}
}
catch (error) {
console.error('Failed to remove custom bundle:', error);
throw error;
}
}
/**
* Validate that a bundle is properly formatted
*/
static async validateBundle(bundlePath) {
try {
// Check if bundle file exists
if (!(await react_native_fs_1.default.exists(bundlePath))) {
return false;
}
// Check file size (should be > 0)
const stats = await react_native_fs_1.default.stat(bundlePath);
if (stats.size === 0) {
return false;
}
// Additional validation could include:
// - Checking bundle format
// - Verifying bundle signature
// - Testing bundle loading
return true;
}
catch (error) {
console.error('Bundle validation failed:', error);
return false;
}
}
/**
* Get bundle metadata
*/
static async getBundleMetadata(bundlePath) {
try {
const metadataPath = `${bundlePath}/metadata.json`;
if (await react_native_fs_1.default.exists(metadataPath)) {
const metadataContent = await react_native_fs_1.default.readFile(metadataPath, 'utf8');
return JSON.parse(metadataContent);
}
return null;
}
catch (error) {
console.error('Failed to read bundle metadata:', error);
return null;
}
}
}
exports.BundleManager = BundleManager;
BundleManager.ORIGINAL_BUNDLE_PATH = react_native_1.Platform.select({
ios: `${react_native_fs_1.default.MainBundlePath}/main.jsbundle`,
android: 'assets://index.android.bundle',
});
BundleManager.CUSTOM_BUNDLE_PATH = `${react_native_fs_1.default.DocumentDirectoryPath}/CustomCodePush/current.bundle`;