This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathcodePush.ts
491 lines (443 loc) · 24.2 KB
/
codePush.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/// <reference path="../typings/codePush.d.ts" />
/// <reference types="cordova-plugin-file" />
/// <reference types="cordova-plugin-file-transfer" />
/// <reference types="cordova" />
/// <reference types="cordova-plugin-dialogs" />
"use strict";
declare var zip: any;
declare var cordova: Cordova;
import LocalPackage = require("./localPackage");
import RemotePackage = require("./remotePackage");
import CodePushUtil = require("./codePushUtil");
import NativeAppInfo = require("./nativeAppInfo");
import Sdk = require("./sdk");
import SyncStatus = require("./syncStatus");
/**
* This is the entry point to Cordova CodePush SDK.
* It provides the following features to the app developer:
* - polling the server for new versions of the app
* - notifying the plugin that the application loaded successfully after an update
* - getting information about the currently deployed package
*/
class CodePush implements CodePushCordovaPlugin {
/**
* The default options for the sync command.
*/
private static DefaultSyncOptions: SyncOptions;
/**
* The default UI for the update dialog, in case it is enabled.
* Please note that the update dialog is disabled by default.
*/
private static DefaultUpdateDialogOptions: UpdateDialogOptions;
/**
* Whether or not a sync is currently in progress.
*/
private static SyncInProgress: boolean;
/**
* Notifies the plugin that the update operation succeeded and that the application is ready.
* Calling this function is required on the first run after an update. On every subsequent application run, calling this function is a noop.
* If using sync API, calling this function is not required since sync calls it internally.
*
* @param notifySucceeded Optional callback invoked if the plugin was successfully notified.
* @param notifyFailed Optional callback invoked in case of an error during notifying the plugin.
*/
public notifyApplicationReady(notifySucceeded?: SuccessCallback<void>, notifyFailed?: ErrorCallback): void {
cordova.exec(notifySucceeded, notifyFailed, "CodePush", "notifyApplicationReady", []);
}
/**
* Do full update or diff update
* 0 = diff update
* 1 = full update
*/
private UpdateType: Number = 0;
/**
*
* @param type
* Implement for full update prefence
*/
public setUpdateType(type: Number): void {
this.UpdateType = type;
}
/**
* Reloads the application. If there is a pending update package installed using ON_NEXT_RESTART or ON_NEXT_RESUME modes, the update
* will be immediately visible to the user. Otherwise, calling this function will simply reload the current version of the application.
*/
public restartApplication(installSuccess: SuccessCallback<void>, errorCallback?: ErrorCallback): void {
cordova.exec(installSuccess, errorCallback, "CodePush", "restartApplication", []);
}
/**
* Reports an application status back to the server.
* !!! This function is called from the native side, please make changes accordingly. !!!
*/
public reportStatus(status: number, label: string, appVersion: string, deploymentKey: string, previousLabelOrAppVersion?: string, previousDeploymentKey?: string) {
if (((!label && appVersion === previousLabelOrAppVersion) || label === previousLabelOrAppVersion)
&& deploymentKey === previousDeploymentKey) {
// No-op since the new appVersion and label is exactly the same as the previous
// (the app might have been updated via a direct or HockeyApp deployment).
return;
}
var createPackageForReporting = (label: string, appVersion: string): IPackage => {
return {
/* The SDK only reports the label and appVersion.
The rest of the properties are added for type safety. */
label, appVersion, deploymentKey,
description: null, isMandatory: false,
packageHash: null, packageSize: null,
failedInstall: false
};
};
var reportDone = (error: Error) => {
var reportArgs = {
status,
label,
appVersion,
deploymentKey,
previousLabelOrAppVersion,
previousDeploymentKey
};
if (error) {
CodePushUtil.logError(`An error occurred while reporting status: ${JSON.stringify(reportArgs)}`, error);
cordova.exec(null, null, "CodePush", "reportFailed", [reportArgs]);
} else {
CodePushUtil.logMessage(`Reported status: ${JSON.stringify(reportArgs)}`);
cordova.exec(null, null, "CodePush", "reportSucceeded", [reportArgs]);
}
};
switch (status) {
case ReportStatus.STORE_VERSION:
Sdk.reportStatusDeploy(null, AcquisitionStatus.DeploymentSucceeded, deploymentKey, previousLabelOrAppVersion, previousDeploymentKey, reportDone);
break;
case ReportStatus.UPDATE_CONFIRMED:
Sdk.reportStatusDeploy(createPackageForReporting(label, appVersion), AcquisitionStatus.DeploymentSucceeded, deploymentKey, previousLabelOrAppVersion, previousDeploymentKey, reportDone);
break;
case ReportStatus.UPDATE_ROLLED_BACK:
Sdk.reportStatusDeploy(createPackageForReporting(label, appVersion), AcquisitionStatus.DeploymentFailed, deploymentKey, previousLabelOrAppVersion, previousDeploymentKey, reportDone);
break;
}
}
/**
* Get the current package information.
*
* @param packageSuccess Callback invoked with the currently deployed package information.
* @param packageError Optional callback invoked in case of an error.
*/
public getCurrentPackage(packageSuccess: SuccessCallback<LocalPackage>, packageError?: ErrorCallback): void {
NativeAppInfo.isPendingUpdate((pendingUpdate: boolean) => {
var packageInfoFile = pendingUpdate ? LocalPackage.OldPackageInfoFile : LocalPackage.PackageInfoFile;
LocalPackage.getPackageInfoOrNull(packageInfoFile, packageSuccess, packageError);
});
}
/**
* Gets the pending package information, if any. A pending package is one that has been installed but the application still runs the old code.
* This happends only after a package has been installed using ON_NEXT_RESTART or ON_NEXT_RESUME mode, but the application was not restarted/resumed yet.
*/
public getPendingPackage(packageSuccess: SuccessCallback<ILocalPackage>, packageError?: ErrorCallback): void {
NativeAppInfo.isPendingUpdate((pendingUpdate: boolean) => {
if (pendingUpdate) {
LocalPackage.getPackageInfoOrNull(LocalPackage.PackageInfoFile, packageSuccess, packageError);
} else {
packageSuccess(null);
}
});
}
/**
* Checks with the CodePush server if an update package is available for download.
*
* @param querySuccess Callback invoked in case of a successful response from the server.
* The callback takes one RemotePackage parameter. A non-null package is a valid update.
* A null package means the application is up to date for the current native application version.
* @param queryError Optional callback invoked in case of an error.
* @param deploymentKey Optional deployment key that overrides the config.xml setting.
*/
public checkForUpdate(querySuccess: SuccessCallback<RemotePackage>, queryError?: ErrorCallback, deploymentKey?: string): void {
try {
var callback: Callback<RemotePackage | NativeUpdateNotification> = (error: Error, remotePackageOrUpdateNotification: IRemotePackage | NativeUpdateNotification) => {
if (error) {
CodePushUtil.invokeErrorCallback(error, queryError);
}
else {
var appUpToDate = () => {
CodePushUtil.logMessage("App is up to date.");
querySuccess && querySuccess(null);
};
if (remotePackageOrUpdateNotification) {
if ((<NativeUpdateNotification>remotePackageOrUpdateNotification).updateAppVersion) {
/* There is an update available for a different version. In the current version of the plugin, we treat that as no update. */
CodePushUtil.logMessage("An update is available, but it is targeting a newer binary version than you are currently running.");
appUpToDate();
} else {
/* There is an update available for the current version. */
var remotePackage: RemotePackage = <RemotePackage>remotePackageOrUpdateNotification;
NativeAppInfo.isFailedUpdate(remotePackage.packageHash, (installFailed: boolean) => {
var result: RemotePackage = new RemotePackage();
result.appVersion = remotePackage.appVersion;
result.deploymentKey = deploymentKey; // server does not send back the deployment key
result.description = remotePackage.description;
result.downloadUrl = remotePackage.downloadUrl;
result.isMandatory = remotePackage.isMandatory;
result.label = remotePackage.label;
result.packageHash = remotePackage.packageHash;
result.packageSize = remotePackage.packageSize;
result.failedInstall = installFailed;
CodePushUtil.logMessage("An update is available. " + JSON.stringify(result));
querySuccess && querySuccess(result);
});
}
}
else {
appUpToDate();
}
}
};
var queryUpdate = () => {
Sdk.getAcquisitionManager((initError: Error, acquisitionManager: AcquisitionManager) => {
if (initError) {
CodePushUtil.invokeErrorCallback(initError, queryError);
} else {
LocalPackage.getCurrentOrDefaultPackage((localPackage: LocalPackage) => {
NativeAppInfo.getApplicationVersion((appVersionError: Error, currentBinaryVersion: string) => {
if (!appVersionError) {
localPackage.appVersion = currentBinaryVersion;
}
if (this.UpdateType === 1) {
delete localPackage.label;
}
CodePushUtil.logMessage("Checking for update.");
acquisitionManager.queryUpdateWithCurrentPackage(localPackage, callback);
});
}, (error: Error) => {
CodePushUtil.invokeErrorCallback(error, queryError);
});
}
}, deploymentKey);
};
if (deploymentKey) {
queryUpdate();
} else {
NativeAppInfo.getDeploymentKey((deploymentKeyError: Error, defaultDeploymentKey: string) => {
if (deploymentKeyError) {
CodePushUtil.invokeErrorCallback(deploymentKeyError, queryError);
} else {
deploymentKey = defaultDeploymentKey;
queryUpdate();
}
});
}
} catch (e) {
CodePushUtil.invokeErrorCallback(new Error("An error occurred while querying for updates." + CodePushUtil.getErrorMessage(e)), queryError);
}
}
/**
* Convenience method for installing updates in one method call.
* This method is provided for simplicity, and its behavior can be replicated by using window.codePush.checkForUpdate(), RemotePackage's download() and LocalPackage's install() methods.
* If another sync is already running, it yields SyncStatus.IN_PROGRESS.
*
* The algorithm of this method is the following:
* - Checks for an update on the CodePush server.
* - If an update is available
* - If the update is mandatory and the alertMessage is set in options, the user will be informed that the application will be updated to the latest version.
* The update package will then be downloaded and applied.
* - If the update is not mandatory and the confirmMessage is set in options, the user will be asked if they want to update to the latest version.
* If they decline, the syncCallback will be invoked with SyncStatus.UPDATE_IGNORED.
* - Otherwise, the update package will be downloaded and applied with no user interaction.
* - If no update is available on the server, the syncCallback will be invoked with the SyncStatus.UP_TO_DATE.
* - If an error occurs during checking for update, downloading or installing it, the syncCallback will be invoked with the SyncStatus.ERROR.
*
* @param syncCallback Optional callback to be called with the status of the sync operation.
* The callback will be called only once, and the possible statuses are defined by the SyncStatus enum.
* @param syncOptions Optional SyncOptions parameter configuring the behavior of the sync operation.
* @param downloadProgress Optional callback invoked during the download process. It is called several times with one DownloadProgress parameter.
* @param syncErrback Optional errback invoked if an error occurs. The callback will be called only once
*
*/
public sync(syncCallback?: SuccessCallback<any>, syncOptions?: SyncOptions, downloadProgress?: SuccessCallback<DownloadProgress>, syncErrback?: ErrorCallback): void {
/* Check if a sync is already in progress */
if (CodePush.SyncInProgress) {
/* A sync is already in progress */
CodePushUtil.logMessage("Sync already in progress.");
syncCallback && syncCallback(SyncStatus.IN_PROGRESS);
} else {
/* Create a callback that resets the SyncInProgress flag when the sync is complete
* If the sync status is a result status, then the sync must be complete and the flag must be updated
* Otherwise, do not change the flag and trigger the syncCallback as usual
*/
var syncCallbackAndUpdateSyncInProgress: Callback<any> = (err: Error, result: any): void => {
switch (result) {
case SyncStatus.ERROR:
case SyncStatus.IN_PROGRESS:
case SyncStatus.UP_TO_DATE:
case SyncStatus.UPDATE_IGNORED:
case SyncStatus.UPDATE_INSTALLED:
/* The sync has completed */
CodePush.SyncInProgress = false;
default:
/* The sync is not yet complete, so do nothing */
break;
}
if (err) {
syncErrback && syncErrback(err);
}
syncCallback && syncCallback(result);
};
/* Begin the sync */
CodePush.SyncInProgress = true;
this.syncInternal(syncCallbackAndUpdateSyncInProgress, syncOptions, downloadProgress);
}
}
/**
* Convenience method for installing updates in one method call.
* This method is provided for simplicity, and its behavior can be replicated by using window.codePush.checkForUpdate(), RemotePackage's download() and LocalPackage's install() methods.
*
* A helper function for the sync function. It does not check if another sync is ongoing.
*
* @param syncCallback Optional callback to be called with the status of the sync operation.
* The callback will be called only once, and the possible statuses are defined by the SyncStatus enum.
* @param syncOptions Optional SyncOptions parameter configuring the behavior of the sync operation.
* @param downloadProgress Optional callback invoked during the download process. It is called several times with one DownloadProgress parameter.
*
*/
private syncInternal(syncCallback?: Callback<any>, syncOptions?: SyncOptions, downloadProgress?: SuccessCallback<DownloadProgress>): void {
/* No options were specified, use default */
if (!syncOptions) {
syncOptions = this.getDefaultSyncOptions();
} else {
/* Some options were specified */
/* Handle dialog options */
var defaultDialogOptions = this.getDefaultUpdateDialogOptions();
if (syncOptions.updateDialog) {
if (typeof syncOptions.updateDialog !== typeof ({})) {
/* updateDialog set to truey condition, use default options */
syncOptions.updateDialog = defaultDialogOptions;
} else {
/* some options were specified, merge with default */
CodePushUtil.copyUnassignedMembers(defaultDialogOptions, syncOptions.updateDialog);
}
}
/* Handle other options. Dialog options will not be overwritten. */
var defaultOptions = this.getDefaultSyncOptions();
CodePushUtil.copyUnassignedMembers(defaultOptions, syncOptions);
}
window.codePush.notifyApplicationReady();
var onError = (error: Error) => {
CodePushUtil.logError("An error occurred during sync.", error);
syncCallback && syncCallback(error, SyncStatus.ERROR);
};
var onInstallSuccess = (appliedWhen: InstallMode) => {
switch (appliedWhen) {
case InstallMode.ON_NEXT_RESTART:
CodePushUtil.logMessage("Update is installed and will be run on the next app restart.");
break;
case InstallMode.ON_NEXT_RESUME:
if (syncOptions.minimumBackgroundDuration > 0) {
CodePushUtil.logMessage(`Update is installed and will be run after the app has been in the background for at least ${syncOptions.minimumBackgroundDuration} seconds.`);
} else {
CodePushUtil.logMessage("Update is installed and will be run when the app next resumes.");
}
break;
}
syncCallback && syncCallback(null, SyncStatus.UPDATE_INSTALLED);
};
var onDownloadSuccess = (localPackage: ILocalPackage) => {
syncCallback && syncCallback(null, SyncStatus.INSTALLING_UPDATE);
localPackage.install(onInstallSuccess, onError, syncOptions);
};
var downloadAndInstallUpdate = (remotePackage: RemotePackage) => {
syncCallback && syncCallback(null, SyncStatus.DOWNLOADING_PACKAGE);
remotePackage.download(onDownloadSuccess, onError, downloadProgress);
};
var onUpdate = (remotePackage: RemotePackage) => {
var updateShouldBeIgnored = remotePackage && (remotePackage.failedInstall && syncOptions.ignoreFailedUpdates);
if (!remotePackage || updateShouldBeIgnored) {
if (updateShouldBeIgnored) {
CodePushUtil.logMessage("An update is available, but it is being ignored due to have been previously rolled back.");
}
syncCallback && syncCallback(null, SyncStatus.UP_TO_DATE);
} else {
var dlgOpts: UpdateDialogOptions = <UpdateDialogOptions>syncOptions.updateDialog;
if (dlgOpts) {
CodePushUtil.logMessage("Awaiting user action.");
syncCallback && syncCallback(null, SyncStatus.AWAITING_USER_ACTION);
}
if (remotePackage.isMandatory && syncOptions.updateDialog) {
/* Alert user */
var message = dlgOpts.appendReleaseDescription ?
dlgOpts.mandatoryUpdateMessage + dlgOpts.descriptionPrefix + remotePackage.description
: dlgOpts.mandatoryUpdateMessage;
navigator.notification.alert(message, () => { downloadAndInstallUpdate(remotePackage); }, dlgOpts.updateTitle, dlgOpts.mandatoryContinueButtonLabel);
} else if (!remotePackage.isMandatory && syncOptions.updateDialog) {
/* Confirm update with user */
var optionalUpdateCallback = (buttonIndex: number) => {
switch (buttonIndex) {
case 1:
/* Install */
downloadAndInstallUpdate(remotePackage);
break;
case 2:
default:
/* Cancel */
CodePushUtil.logMessage("User cancelled the update.");
syncCallback && syncCallback(null, SyncStatus.UPDATE_IGNORED);
break;
}
};
var message = dlgOpts.appendReleaseDescription ?
dlgOpts.optionalUpdateMessage + dlgOpts.descriptionPrefix + remotePackage.description
: dlgOpts.optionalUpdateMessage;
navigator.notification.confirm(message, optionalUpdateCallback, dlgOpts.updateTitle, [dlgOpts.optionalInstallButtonLabel, dlgOpts.optionalIgnoreButtonLabel]);
} else {
/* No user interaction */
downloadAndInstallUpdate(remotePackage);
}
}
};
syncCallback && syncCallback(null, SyncStatus.CHECKING_FOR_UPDATE);
window.codePush.checkForUpdate(onUpdate, onError, syncOptions.deploymentKey);
}
/**
* Returns the default options for the CodePush sync operation.
* If the options are not defined yet, the static DefaultSyncOptions member will be instantiated.
*/
private getDefaultSyncOptions(): SyncOptions {
if (!CodePush.DefaultSyncOptions) {
CodePush.DefaultSyncOptions = {
ignoreFailedUpdates: true,
installMode: InstallMode.ON_NEXT_RESTART,
minimumBackgroundDuration: 0,
mandatoryInstallMode: InstallMode.IMMEDIATE,
updateDialog: false,
deploymentKey: undefined
};
}
return CodePush.DefaultSyncOptions;
}
/**
* Returns the default options for the update dialog.
* Please note that the dialog is disabled by default.
*/
private getDefaultUpdateDialogOptions(): UpdateDialogOptions {
if (!CodePush.DefaultUpdateDialogOptions) {
CodePush.DefaultUpdateDialogOptions = {
updateTitle: "Update available",
mandatoryUpdateMessage: "An update is available that must be installed.",
mandatoryContinueButtonLabel: "Continue",
optionalUpdateMessage: "An update is available. Would you like to install it?",
optionalInstallButtonLabel: "Install",
optionalIgnoreButtonLabel: "Ignore",
appendReleaseDescription: false,
descriptionPrefix: " Description: "
};
}
return CodePush.DefaultUpdateDialogOptions;
}
}
/**
* Defines the application statuses reported from the native layer.
* !!! This enum is defined in native code as well, please make changes accordingly. !!!
*/
enum ReportStatus {
STORE_VERSION = 0,
UPDATE_CONFIRMED = 1,
UPDATE_ROLLED_BACK = 2
}
var instance = new CodePush();
export = instance;