Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 7bb4e10

Browse files
committed
fix getCurrentPackage
1 parent 009ae20 commit 7bb4e10

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
lines changed

CodePush.js

Lines changed: 6 additions & 4 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 15 additions & 8 deletions
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,15 +204,22 @@ + (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:[self getCodePushPath]]) {
210+
[[NSFileManager defaultManager] createDirectoryAtPath:[self getCodePushPath]
211211
withIntermediateDirectories:YES
212212
attributes:nil
213213
error:&error];
214214
}
215215

216+
if ([[NSFileManager defaultManager] fileExistsAtPath:newPackageFolderPath]) {
217+
// This removes any stale data in newPackageFolderPath that could have been left
218+
// uncleared due to a crash or error during the download or install process.
219+
[[NSFileManager defaultManager] removeItemAtPath:newPackageFolderPath
220+
error:&error];
221+
}
222+
216223
if (error) {
217224
return failCallback(error);
218225
}
@@ -259,9 +266,9 @@ + (void)downloadPackage:(NSDictionary *)updatePackage
259266
return;
260267
}
261268

262-
[CodePushPackage copyEntriesInFolder:currentPackageFolderPath
263-
destFolder:newPackageFolderPath
264-
error:&error];
269+
[[NSFileManager defaultManager] copyItemAtPath:currentPackageFolderPath
270+
toPath:newPackageFolderPath
271+
error:&error];
265272
if (error) {
266273
failCallback(error);
267274
return;

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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 8 additions & 2 deletions
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)