mirror of
https://github.com/zhigang1992/react-native-code-push.git
synced 2026-06-10 23:59:42 +08:00
Merge pull request #196 from Microsoft/fix-get-current-package
Fix getCurrentPackage
This commit is contained in:
10
CodePush.js
10
CodePush.js
@@ -27,7 +27,7 @@ async function checkForUpdate(deploymentKey = null) {
|
||||
*/
|
||||
const config = deploymentKey ? { ...nativeConfig, ...{ deploymentKey } } : nativeConfig;
|
||||
const sdk = getPromisifiedSdk(requestFetchAdapter, config);
|
||||
|
||||
|
||||
// Use dynamically overridden getCurrentPackage() during tests.
|
||||
const localPackage = await module.exports.getCurrentPackage();
|
||||
|
||||
@@ -57,7 +57,7 @@ async function checkForUpdate(deploymentKey = null) {
|
||||
* bug in the server, but we're adding this check just to double-check that the
|
||||
* client app is resilient to a potential issue with the update check.
|
||||
*/
|
||||
if (!update || update.updateAppVersion || (update.packageHash === localPackage.packageHash)) {
|
||||
if (!update || update.updateAppVersion || localPackage && (update.packageHash === localPackage.packageHash)) {
|
||||
if (update && update.updateAppVersion) {
|
||||
log("An update is available but it is targeting a newer binary version than you are currently running.");
|
||||
}
|
||||
@@ -87,8 +87,10 @@ const getConfiguration = (() => {
|
||||
|
||||
async function getCurrentPackage() {
|
||||
const localPackage = await NativeCodePush.getCurrentPackage();
|
||||
localPackage.failedInstall = await NativeCodePush.isFailedUpdate(localPackage.packageHash);
|
||||
localPackage.isFirstRun = await NativeCodePush.isFirstRun(localPackage.packageHash);
|
||||
if (localPackage) {
|
||||
localPackage.failedInstall = await NativeCodePush.isFailedUpdate(localPackage.packageHash);
|
||||
localPackage.isFirstRun = await NativeCodePush.isFirstRun(localPackage.packageHash);
|
||||
}
|
||||
return localPackage;
|
||||
}
|
||||
|
||||
|
||||
@@ -462,6 +462,7 @@ RCT_EXPORT_METHOD(getCurrentPackage:(RCTPromiseResolveBlock)resolve
|
||||
|
||||
if (error) {
|
||||
reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
|
||||
return;
|
||||
}
|
||||
|
||||
// Add the "isPending" virtual property to the package at this point, so that
|
||||
|
||||
@@ -142,7 +142,7 @@ NSString * const UnzippedFolderName = @"unzipped";
|
||||
NSString *folderPath = [CodePushPackage getCurrentPackageFolderPath:error];
|
||||
if (!*error) {
|
||||
if (!folderPath) {
|
||||
return [NSDictionary dictionary];
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSString *packagePath = [folderPath stringByAppendingPathComponent:@"app.json"];
|
||||
@@ -159,7 +159,7 @@ NSString * const UnzippedFolderName = @"unzipped";
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
return nil;
|
||||
}
|
||||
|
||||
+ (NSDictionary *)getPackage:(NSString *)packageHash
|
||||
@@ -204,10 +204,15 @@ NSString * const UnzippedFolderName = @"unzipped";
|
||||
failCallback:(void (^)(NSError *err))failCallback
|
||||
{
|
||||
NSString *newPackageFolderPath = [self getPackageFolderPath:updatePackage[@"packageHash"]];
|
||||
NSError *error = nil;
|
||||
NSError *error;
|
||||
|
||||
if (![[NSFileManager defaultManager] fileExistsAtPath:newPackageFolderPath]) {
|
||||
[[NSFileManager defaultManager] createDirectoryAtPath:newPackageFolderPath
|
||||
if ([[NSFileManager defaultManager] fileExistsAtPath:newPackageFolderPath]) {
|
||||
// This removes any stale data in newPackageFolderPath that could have been left
|
||||
// uncleared due to a crash or error during the download or install process.
|
||||
[[NSFileManager defaultManager] removeItemAtPath:newPackageFolderPath
|
||||
error:&error];
|
||||
} else if (![[NSFileManager defaultManager] fileExistsAtPath:[self getCodePushPath]]) {
|
||||
[[NSFileManager defaultManager] createDirectoryAtPath:[self getCodePushPath]
|
||||
withIntermediateDirectories:YES
|
||||
attributes:nil
|
||||
error:&error];
|
||||
@@ -259,9 +264,9 @@ NSString * const UnzippedFolderName = @"unzipped";
|
||||
return;
|
||||
}
|
||||
|
||||
[CodePushPackage copyEntriesInFolder:currentPackageFolderPath
|
||||
destFolder:newPackageFolderPath
|
||||
error:&error];
|
||||
[[NSFileManager defaultManager] copyItemAtPath:currentPackageFolderPath
|
||||
toPath:newPackageFolderPath
|
||||
error:&error];
|
||||
if (error) {
|
||||
failCallback(error);
|
||||
return;
|
||||
|
||||
@@ -411,6 +411,10 @@ public class CodePush {
|
||||
@Override
|
||||
protected Void doInBackground(Object... params) {
|
||||
WritableMap currentPackage = codePushPackage.getCurrentPackage();
|
||||
if (currentPackage == null) {
|
||||
promise.resolve("");
|
||||
return null;
|
||||
}
|
||||
|
||||
Boolean isPendingUpdate = false;
|
||||
|
||||
|
||||
@@ -129,14 +129,14 @@ public class CodePushPackage {
|
||||
public WritableMap getCurrentPackage() {
|
||||
String folderPath = getCurrentPackageFolderPath();
|
||||
if (folderPath == null) {
|
||||
return new WritableNativeMap();
|
||||
return null;
|
||||
}
|
||||
|
||||
String packagePath = CodePushUtils.appendPathComponent(folderPath, PACKAGE_FILE_NAME);
|
||||
try {
|
||||
return CodePushUtils.getWritableMapFromFile(packagePath);
|
||||
} catch (IOException e) {
|
||||
// There is no current package.
|
||||
// Should not happen unless the update metadata was somehow deleted.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -155,6 +155,12 @@ public class CodePushPackage {
|
||||
DownloadProgressCallback progressCallback) throws IOException {
|
||||
|
||||
String newPackageFolderPath = getPackageFolderPath(CodePushUtils.tryGetString(updatePackage, PACKAGE_HASH_KEY));
|
||||
if (FileUtils.fileAtPathExists(newPackageFolderPath)) {
|
||||
// This removes any stale data in newPackageFolderPath that could have been left
|
||||
// uncleared due to a crash or error during the download or install process.
|
||||
FileUtils.deleteDirectoryAtPath(newPackageFolderPath);
|
||||
}
|
||||
|
||||
String downloadUrlString = CodePushUtils.tryGetString(updatePackage, DOWNLOAD_URL_KEY);
|
||||
URL downloadUrl = null;
|
||||
HttpURLConnection connection = null;
|
||||
|
||||
Reference in New Issue
Block a user