vromo-ota-hot-update
Version:
Hot update for vromo driver app
175 lines (173 loc) • 5.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
var _index = _interopRequireDefault(require("./gits/index.js"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const LINKING_ERROR = `The package 'react-native-ota-hot-update' 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 (downloadManager, uri, headers, callback) => {
const res = await downloadManager.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(downloadManager, uri, version, option) {
if (!uri) {
installFail(option, 'Please give a valid URL!');
return;
}
if (!version) {
installFail(option, 'Please give a valid version!');
return;
}
const currentVersion = await getVersionAsNumber();
if (version <= currentVersion) {
installFail(option, 'Please give a bigger version than the current version, the current version now has setted by: ' + currentVersion);
return;
}
try {
const path = await downloadBundleFile(downloadManager, uri, option?.headers, option?.progress);
if (path) {
setupBundlePath(path, option?.extensionBundle).then(success => {
if (success) {
setCurrentVersion(version);
option?.updateSuccess?.();
if (option?.restartAfterInstall) {
setTimeout(() => {
resetApp();
}, 300);
}
} else {
installFail(option);
}
});
} else {
installFail(option);
}
} catch (e) {
installFail(option, e);
}
}
const checkForGitUpdate = async options => {
try {
if (!options.url || !options.bundlePath) {
throw new Error(`url or bundlePath should not be null`);
}
const branch = await _index.default.getBranchName();
if (branch) {
const pull = await _index.default.pullUpdate({
branch,
onProgress: options?.onProgress,
folderName: options?.folderName
});
if (pull.success) {
options?.onPullSuccess?.();
if (options?.restartAfterInstall) {
setTimeout(() => {
resetApp();
}, 300);
}
} else {
options?.onPullFailed?.(pull.msg);
}
} else {
const clone = await _index.default.cloneRepo({
onProgress: options?.onProgress,
folderName: options?.folderName,
url: options.url,
branch: options?.branch,
bundlePath: options.bundlePath
});
if (clone.success && clone.bundle) {
await setupExactBundlePath(clone.bundle);
options?.onCloneSuccess?.();
if (options?.restartAfterInstall) {
setTimeout(() => {
resetApp();
}, 300);
}
} else {
options?.onCloneFailed?.(clone.msg);
}
}
} catch (e) {
options?.onCloneFailed?.(e.toString());
} finally {
options?.onFinishProgress?.();
}
};
var _default = exports.default = {
setupBundlePath,
setupExactBundlePath,
removeUpdate: removeBundle,
downloadBundleUri,
resetApp,
getCurrentVersion: getVersionAsNumber,
setCurrentVersion,
git: {
checkForGitUpdate,
..._index.default,
removeGitUpdate: folder => {
RNhotupdate.setExactBundlePath('');
_index.default.removeGitUpdate(folder);
}
}
};
//# sourceMappingURL=index.js.map