Skip to content

Commit a61d0c4

Browse files
committed
Merge pull request #196 from Microsoft/fix-get-current-package
Fix getCurrentPackage
2 parents 009ae20 + 446dc97 commit a61d0c4

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

CodePush.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async function checkForUpdate(deploymentKey = null) {
2727
*/
2828
const config = deploymentKey ? { ...nativeConfig, ...{ deploymentKey } } : nativeConfig;
2929
const sdk = getPromisifiedSdk(requestFetchAdapter, config);
30-
30+
3131
// Use dynamically overridden getCurrentPackage() during tests.
3232
const localPackage = await module.exports.getCurrentPackage();
3333

@@ -57,7 +57,7 @@ async function checkForUpdate(deploymentKey = null) {
5757
* bug in the server, but we're adding this check just to double-check that the
5858
* client app is resilient to a potential issue with the update check.
5959
*/
60-
if (!update || update.updateAppVersion || (update.packageHash === localPackage.packageHash)) {
60+
if (!update || update.updateAppVersion || localPackage && (update.packageHash === localPackage.packageHash)) {
6161
if (update && update.updateAppVersion) {
6262
log("An update is available but it is targeting a newer binary version than you are currently running.");
6363
}
@@ -87,8 +87,10 @@ const getConfiguration = (() => {
8787

8888
async function getCurrentPackage() {
8989
const localPackage = await NativeCodePush.getCurrentPackage();
90-
localPackage.failedInstall = await NativeCodePush.isFailedUpdate(localPackage.packageHash);
91-
localPackage.isFirstRun = await NativeCodePush.isFirstRun(localPackage.packageHash);
90+
if (localPackage) {
91+
localPackage.failedInstall = await NativeCodePush.isFailedUpdate(localPackage.packageHash);
92+
localPackage.isFirstRun = await NativeCodePush.isFirstRun(localPackage.packageHash);
93+
}
9294
return localPackage;
9395
}
9496

CodePush.m

+1
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
462462

463463
if (error) {
464464
reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
465+
return;
465466
}
466467

467468
// Add the "isPending" virtual property to the package at this point, so that

CodePushPackage.m

+13-8
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ + (NSDictionary *)getCurrentPackage:(NSError **)error
142142
NSString *folderPath = [CodePushPackage getCurrentPackageFolderPath:error];
143143
if (!*error) {
144144
if (!folderPath) {
145-
return [NSDictionary dictionary];
145+
return nil;
146146
}
147147

148148
NSString *packagePath = [folderPath stringByAppendingPathComponent:@"app.json"];
@@ -159,7 +159,7 @@ + (NSDictionary *)getCurrentPackage:(NSError **)error
159159
}
160160
}
161161

162-
return NULL;
162+
return nil;
163163
}
164164

165165
+ (NSDictionary *)getPackage:(NSString *)packageHash
@@ -204,10 +204,15 @@ + (void)downloadPackage:(NSDictionary *)updatePackage
204204
failCallback:(void (^)(NSError *err))failCallback
205205
{
206206
NSString *newPackageFolderPath = [self getPackageFolderPath:updatePackage[@"packageHash"]];
207-
NSError *error = nil;
207+
NSError *error;
208208

209-
if (![[NSFileManager defaultManager] fileExistsAtPath:newPackageFolderPath]) {
210-
[[NSFileManager defaultManager] createDirectoryAtPath:newPackageFolderPath
209+
if ([[NSFileManager defaultManager] fileExistsAtPath:newPackageFolderPath]) {
210+
// This removes any stale data in newPackageFolderPath that could have been left
211+
// uncleared due to a crash or error during the download or install process.
212+
[[NSFileManager defaultManager] removeItemAtPath:newPackageFolderPath
213+
error:&error];
214+
} else if (![[NSFileManager defaultManager] fileExistsAtPath:[self getCodePushPath]]) {
215+
[[NSFileManager defaultManager] createDirectoryAtPath:[self getCodePushPath]
211216
withIntermediateDirectories:YES
212217
attributes:nil
213218
error:&error];
@@ -259,9 +264,9 @@ + (void)downloadPackage:(NSDictionary *)updatePackage
259264
return;
260265
}
261266

262-
[CodePushPackage copyEntriesInFolder:currentPackageFolderPath
263-
destFolder:newPackageFolderPath
264-
error:&error];
267+
[[NSFileManager defaultManager] copyItemAtPath:currentPackageFolderPath
268+
toPath:newPackageFolderPath
269+
error:&error];
265270
if (error) {
266271
failCallback(error);
267272
return;

android/app/src/main/java/com/microsoft/codepush/react/CodePush.java

+4
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,10 @@ public void getCurrentPackage(final Promise promise) {
411411
@Override
412412
protected Void doInBackground(Object... params) {
413413
WritableMap currentPackage = codePushPackage.getCurrentPackage();
414+
if (currentPackage == null) {
415+
promise.resolve("");
416+
return null;
417+
}
414418

415419
Boolean isPendingUpdate = false;
416420

android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,14 @@ public String getPreviousPackageHash() {
129129
public WritableMap getCurrentPackage() {
130130
String folderPath = getCurrentPackageFolderPath();
131131
if (folderPath == null) {
132-
return new WritableNativeMap();
132+
return null;
133133
}
134134

135135
String packagePath = CodePushUtils.appendPathComponent(folderPath, PACKAGE_FILE_NAME);
136136
try {
137137
return CodePushUtils.getWritableMapFromFile(packagePath);
138138
} catch (IOException e) {
139-
// There is no current package.
139+
// Should not happen unless the update metadata was somehow deleted.
140140
return null;
141141
}
142142
}
@@ -155,6 +155,12 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
155155
DownloadProgressCallback progressCallback) throws IOException {
156156

157157
String newPackageFolderPath = getPackageFolderPath(CodePushUtils.tryGetString(updatePackage, PACKAGE_HASH_KEY));
158+
if (FileUtils.fileAtPathExists(newPackageFolderPath)) {
159+
// This removes any stale data in newPackageFolderPath that could have been left
160+
// uncleared due to a crash or error during the download or install process.
161+
FileUtils.deleteDirectoryAtPath(newPackageFolderPath);
162+
}
163+
158164
String downloadUrlString = CodePushUtils.tryGetString(updatePackage, DOWNLOAD_URL_KEY);
159165
URL downloadUrl = null;
160166
HttpURLConnection connection = null;

0 commit comments

Comments
 (0)