schoolx-ota-manager
Version:
React Native library for managing OTA updates with GitLab integration
128 lines • 4.8 kB
JavaScript
import AsyncStorage from '@react-native-async-storage/async-storage';
import DeviceInfo from 'react-native-device-info';
import OtaHotUpdate from 'react-native-ota-hot-update';
import { GitLabService } from './services/GitLabService';
import { VersionChecker } from './utils/VersionChecker';
export class OtaManager {
constructor(config) {
this.config = config;
this.gitlabService = new GitLabService(config.gitlabToken, config.gitlabBaseUrl, config.gitlabProjectId, config.repoFolder);
}
/**
* Set progress callback
*/
onProgress(callback) {
this.onProgressCallback = callback;
}
/**
* Check for updates
*/
async checkUpdate() {
try {
this.notifyProgress({ status: 'checking', message: 'Checking for updates...' });
const versionData = await this.gitlabService.getVersion();
const currentAppVersion = DeviceInfo.getVersion();
const platformInfo = versionData[this.config.platform];
if (!platformInfo) {
throw new Error(`No platform info found for ${this.config.platform}`);
}
const installedBuildNumber = parseInt(await AsyncStorage.getItem('INSTALLED_BUILD_NUMBER') || '0');
const isCompatible = VersionChecker.isCompatible(currentAppVersion, platformInfo.version, platformInfo.buildNumber, installedBuildNumber, platformInfo.enabled);
if (!isCompatible) {
return {
hasUpdate: false,
isForced: false
};
}
const hasUpdate = true;
return {
hasUpdate,
bundleInfo: platformInfo,
isForced: platformInfo.isForced,
releaseNotes: platformInfo.releaseNotes
};
}
catch (error) {
console.error('Check update failed:', error);
throw error;
}
}
/**
* Download and install update
*/
async installUpdate() {
console.log("Installing update ===>");
try {
this.notifyProgress({ status: 'downloading', message: 'Downloading update...' });
const bundlePath = await this.gitlabService.downloadPlatformBundle(this.config.platform);
this.notifyProgress({ status: 'installing', message: 'Installing update...' });
const success = await OtaHotUpdate.setupBundlePath(bundlePath);
if (success) {
const versionData = await this.gitlabService.getVersion();
const platformInfo = versionData[this.config.platform];
await AsyncStorage.setItem('INSTALLED_BUILD_NUMBER', platformInfo.buildNumber.toString());
this.notifyProgress({ status: 'completed', message: 'Update installed successfully' });
if (platformInfo.isForced) {
OtaHotUpdate.resetApp();
}
return true;
}
else {
throw new Error('Bundle installation failed');
}
}
catch (error) {
console.error('Install update failed:', error);
this.notifyProgress({ status: 'failed', message: 'Update failed: ' + (error instanceof Error ? error.message : String(error)) });
throw error;
}
}
/**
* Check and install update automatically
*/
async checkAndInstallUpdate() {
try {
const updateInfo = await this.checkUpdate();
if (updateInfo.hasUpdate || updateInfo.isForced) {
return await this.installUpdate();
}
return false;
}
catch (error) {
console.error('Check and install update failed:', error);
throw error;
}
}
/**
* Check for updates when app starts (called once)
*/
async checkOnAppStart() {
try {
console.log('Checking for updates on app start...');
return await this.checkUpdate();
}
catch (error) {
console.error('App start update check failed:', error);
return null;
}
}
/**
* Get current installed build number
*/
async getInstalledBuildNumber() {
const buildNumber = await AsyncStorage.getItem('INSTALLED_BUILD_NUMBER');
return parseInt(buildNumber || '0');
}
/**
* Clear installed build number (for testing)
*/
async clearInstalledBuildNumber() {
await AsyncStorage.removeItem('INSTALLED_BUILD_NUMBER');
}
notifyProgress(progress) {
if (this.onProgressCallback) {
this.onProgressCallback(progress);
}
}
}
//# sourceMappingURL=OtaManager.js.map