forked from MaximBelov/cordova-plugin-code-push
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodePush.js
More file actions
318 lines (315 loc) · 16 KB
/
codePush.js
File metadata and controls
318 lines (315 loc) · 16 KB
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
/********************************************************************************************
THIS FILE HAS BEEN COMPILED FROM TYPESCRIPT SOURCES.
PLEASE DO NOT MODIFY THIS FILE DIRECTLY AS YOU WILL LOSE YOUR CHANGES WHEN RECOMPILING.
INSTEAD, EDIT THE TYPESCRIPT SOURCES UNDER THE WWW FOLDER, AND THEN RUN GULP.
FOR MORE INFORMATION, PLEASE SEE CONTRIBUTING.md.
*********************************************************************************************/
"use strict";
var LocalPackage = require("./localPackage");
var RemotePackage = require("./remotePackage");
var CodePushUtil = require("./codePushUtil");
var NativeAppInfo = require("./nativeAppInfo");
var Sdk = require("./sdk");
var SyncStatus = require("./syncStatus");
var CodePush = (function () {
function CodePush() {
}
CodePush.prototype.notifyApplicationReady = function (notifySucceeded, notifyFailed) {
cordova.exec(notifySucceeded, notifyFailed, "CodePush", "notifyApplicationReady", []);
};
CodePush.prototype.restartApplication = function (installSuccess, errorCallback) {
cordova.exec(installSuccess, errorCallback, "CodePush", "restartApplication", []);
};
CodePush.prototype.reportStatus = function (status, label, appVersion, deploymentKey, previousLabelOrAppVersion, previousDeploymentKey) {
if (((!label && appVersion === previousLabelOrAppVersion) || label === previousLabelOrAppVersion)
&& deploymentKey === previousDeploymentKey) {
return;
}
var createPackageForReporting = function (label, appVersion) {
return {
label: label, appVersion: appVersion, deploymentKey: deploymentKey,
description: null, isMandatory: false,
packageHash: null, packageSize: null,
failedInstall: false
};
};
var reportDone = function (error) {
var reportArgs = {
status: status,
label: label,
appVersion: appVersion,
deploymentKey: deploymentKey,
previousLabelOrAppVersion: previousLabelOrAppVersion,
previousDeploymentKey: 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;
}
};
CodePush.prototype.getCurrentPackage = function (packageSuccess, packageError) {
NativeAppInfo.isPendingUpdate(function (pendingUpdate) {
var packageInfoFile = pendingUpdate ? LocalPackage.OldPackageInfoFile : LocalPackage.PackageInfoFile;
LocalPackage.getPackageInfoOrNull(packageInfoFile, packageSuccess, packageError);
});
};
CodePush.prototype.getPendingPackage = function (packageSuccess, packageError) {
NativeAppInfo.isPendingUpdate(function (pendingUpdate) {
if (pendingUpdate) {
LocalPackage.getPackageInfoOrNull(LocalPackage.PackageInfoFile, packageSuccess, packageError);
}
else {
packageSuccess(null);
}
});
};
CodePush.prototype.checkForUpdate = function (querySuccess, queryError, deploymentKey) {
try {
var callback = function (error, remotePackageOrUpdateNotification) {
if (error) {
CodePushUtil.invokeErrorCallback(error, queryError);
}
else {
var appUpToDate = function () {
CodePushUtil.logMessage("App is up to date.");
querySuccess && querySuccess(null);
};
if (remotePackageOrUpdateNotification) {
if (remotePackageOrUpdateNotification.updateAppVersion) {
CodePushUtil.logMessage("An update is available, but it is targeting a newer binary version than you are currently running.");
appUpToDate();
}
else {
var remotePackage = remotePackageOrUpdateNotification;
NativeAppInfo.isFailedUpdate(remotePackage.packageHash, function (installFailed) {
var result = new RemotePackage();
result.appVersion = remotePackage.appVersion;
result.deploymentKey = deploymentKey;
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 = function () {
Sdk.getAcquisitionManager(function (initError, acquisitionManager) {
if (initError) {
CodePushUtil.invokeErrorCallback(initError, queryError);
}
else {
LocalPackage.getCurrentOrDefaultPackage(function (localPackage) {
NativeAppInfo.getApplicationVersion(function (appVersionError, currentBinaryVersion) {
if (!appVersionError) {
localPackage.appVersion = currentBinaryVersion;
}
CodePushUtil.logMessage("Checking for update.");
acquisitionManager.queryUpdateWithCurrentPackage(localPackage, callback);
});
}, function (error) {
CodePushUtil.invokeErrorCallback(error, queryError);
});
}
}, deploymentKey);
};
if (deploymentKey) {
queryUpdate();
}
else {
NativeAppInfo.getDeploymentKey(function (deploymentKeyError, defaultDeploymentKey) {
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);
}
};
CodePush.prototype.sync = function (syncCallback, syncOptions, downloadProgress, syncErrback) {
if (CodePush.SyncInProgress) {
CodePushUtil.logMessage("Sync already in progress.");
syncCallback && syncCallback(SyncStatus.IN_PROGRESS);
}
else {
var syncCallbackAndUpdateSyncInProgress = function (err, result) {
switch (result) {
case SyncStatus.ERROR:
case SyncStatus.IN_PROGRESS:
case SyncStatus.UP_TO_DATE:
case SyncStatus.UPDATE_IGNORED:
case SyncStatus.UPDATE_INSTALLED:
CodePush.SyncInProgress = false;
default:
break;
}
if (err) {
syncErrback && syncErrback(err);
}
syncCallback && syncCallback(result);
};
CodePush.SyncInProgress = true;
this.syncInternal(syncCallbackAndUpdateSyncInProgress, syncOptions, downloadProgress);
}
};
CodePush.prototype.syncInternal = function (syncCallback, syncOptions, downloadProgress) {
if (!syncOptions) {
syncOptions = this.getDefaultSyncOptions();
}
else {
var defaultDialogOptions = this.getDefaultUpdateDialogOptions();
if (syncOptions.updateDialog) {
if (typeof syncOptions.updateDialog !== typeof ({})) {
syncOptions.updateDialog = defaultDialogOptions;
}
else {
CodePushUtil.copyUnassignedMembers(defaultDialogOptions, syncOptions.updateDialog);
}
}
var defaultOptions = this.getDefaultSyncOptions();
CodePushUtil.copyUnassignedMembers(defaultOptions, syncOptions);
}
window.codePush.notifyApplicationReady();
var onError = function (error) {
CodePushUtil.logError("An error occurred during sync.", error);
syncCallback && syncCallback(error, SyncStatus.ERROR);
};
var onInstallSuccess = function (appliedWhen) {
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 = function (localPackage) {
syncCallback && syncCallback(null, SyncStatus.INSTALLING_UPDATE);
localPackage.install(onInstallSuccess, onError, syncOptions);
};
var downloadAndInstallUpdate = function (remotePackage) {
syncCallback && syncCallback(null, SyncStatus.DOWNLOADING_PACKAGE);
remotePackage.download(onDownloadSuccess, onError, downloadProgress);
};
var onUpdate = function (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 = syncOptions.updateDialog;
if (dlgOpts) {
CodePushUtil.logMessage("Awaiting user action.");
syncCallback && syncCallback(null, SyncStatus.AWAITING_USER_ACTION);
}
if (remotePackage.isMandatory && syncOptions.updateDialog) {
var message = dlgOpts.appendReleaseDescription ?
dlgOpts.mandatoryUpdateMessage + dlgOpts.descriptionPrefix + remotePackage.description
: dlgOpts.mandatoryUpdateMessage;
navigator.notification.alert(message, function () { downloadAndInstallUpdate(remotePackage); }, dlgOpts.updateTitle, dlgOpts.mandatoryContinueButtonLabel);
}
else if (!remotePackage.isMandatory && syncOptions.updateDialog) {
var optionalUpdateCallback = function (buttonIndex) {
switch (buttonIndex) {
case 1:
downloadAndInstallUpdate(remotePackage);
break;
case 2:
default:
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 {
downloadAndInstallUpdate(remotePackage);
}
}
};
syncCallback && syncCallback(null, SyncStatus.CHECKING_FOR_UPDATE);
window.codePush.checkForUpdate(onUpdate, onError, syncOptions.deploymentKey);
};
CodePush.prototype.getDefaultSyncOptions = function () {
if (!CodePush.DefaultSyncOptions) {
CodePush.DefaultSyncOptions = {
ignoreFailedUpdates: true,
installMode: InstallMode.ON_NEXT_RESTART,
minimumBackgroundDuration: 0,
mandatoryInstallMode: InstallMode.IMMEDIATE,
updateDialog: false,
deploymentKey: undefined
};
}
return CodePush.DefaultSyncOptions;
};
CodePush.prototype.getDefaultUpdateDialogOptions = function () {
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;
};
return CodePush;
}());
var ReportStatus;
(function (ReportStatus) {
ReportStatus[ReportStatus["STORE_VERSION"] = 0] = "STORE_VERSION";
ReportStatus[ReportStatus["UPDATE_CONFIRMED"] = 1] = "UPDATE_CONFIRMED";
ReportStatus[ReportStatus["UPDATE_ROLLED_BACK"] = 2] = "UPDATE_ROLLED_BACK";
})(ReportStatus || (ReportStatus = {}));
var instance = new CodePush();
module.exports = instance;