UNPKG

react-native-update-ota

Version:
115 lines (113 loc) 3.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _reactNative = require("react-native"); var _rnBlobUtil = _interopRequireDefault(require("rn-blob-util")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const LINKING_ERROR = `The package 'react-native-update-ota' doesn't seem to be linked. Make sure: \n\n` + _reactNative.Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n'; // @ts-expect-error const isTurboModuleEnabled = global.__turboModuleProxy != null; const OtaHotUpdateModule = isTurboModuleEnabled ? require('./NativeOtaHotUpdate').default : _reactNative.NativeModules.OtaHotUpdate; const RNhotupdate = OtaHotUpdateModule ? OtaHotUpdateModule : new Proxy({}, { get() { throw new Error(LINKING_ERROR); } }); const downloadBundleFile = async (uri, headers, callback) => { const res = await _rnBlobUtil.default.config({ fileCache: _reactNative.Platform.OS === 'android' }).fetch('GET', uri, { ...headers }).progress((received, total) => { if (callback) { callback(received + '', total + ''); } }); return res.path(); }; function setupBundlePath(path, extension) { return RNhotupdate.setupBundlePath(path, extension); } function setupExactBundlePath(path) { return RNhotupdate.setExactBundlePath(path); } function deleteBundlePath() { return RNhotupdate.deleteBundle(1); } function getCurrentVersion() { return RNhotupdate.getCurrentVersion(0); } async function getVersionAsNumber() { const rawVersion = await getCurrentVersion(); return +rawVersion; } function setCurrentVersion(version) { return RNhotupdate.setCurrentVersion(version + ''); } async function resetApp() { RNhotupdate.restart(); } function removeBundle(restartAfterRemoved) { deleteBundlePath().then(data => { if (data && restartAfterRemoved) { setTimeout(() => { resetApp(); }, 300); if (data) { setCurrentVersion(0); } } }); } const installFail = (option, e) => { option?.updateFail?.(JSON.stringify(e)); console.error('Download bundle fail', JSON.stringify(e)); }; async function downloadBundleUri(uri, version, option) { if (!uri) { return installFail(option, 'Please give a valid URL!'); } if (!version) { return installFail(option, 'Please give a valid version!'); } if (!option?.noCheckVersion) { const currentVersion = await getVersionAsNumber(); if (version <= currentVersion) { return installFail(option, 'Please give a bigger version than the current version, the current version now has setted by: ' + currentVersion); } } try { const path = await downloadBundleFile(uri, option?.headers, option?.progress); if (!path) { return installFail(option); } const success = await setupBundlePath(path, option?.extensionBundle); if (!success) { return installFail(option); } setCurrentVersion(version); option?.updateSuccess?.(); if (option?.restartAfterInstall) { setTimeout(() => { resetApp(); }, 300); } } catch (e) { installFail(option, e); } } var _default = exports.default = { setupBundlePath, setupExactBundlePath, removeUpdate: removeBundle, downloadBundleUri, resetApp, getCurrentVersion: getVersionAsNumber, setCurrentVersion }; //# sourceMappingURL=index.js.map