-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathapp-sync.ts
361 lines (302 loc) · 14.2 KB
/
app-sync.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
/// <reference path="./code-push-lib.d.ts"/>
import * as AppVersion from "nativescript-appversion";
import { Application, ApplicationSettings, Device } from "@nativescript/core";
import { confirm } from "@nativescript/core/ui/dialogs";
import { TNSAcquisitionManager } from "./TNSAcquisitionManager";
import { TNSLocalPackage } from "./TNSLocalPackage";
import { TNSRemotePackage } from "./TNSRemotePackage";
export { TNSLocalPackage } from './TNSLocalPackage';
export enum InstallMode {
/**
* The update will be applied to the running application immediately. The application will be reloaded with the new content immediately.
*/
IMMEDIATE = <any>"IMMEDIATE",
/**
* The update is downloaded but not installed immediately. The new content will be available the next time the application is started.
*/
ON_NEXT_RESTART = <any>"ON_NEXT_RESTART",
/**
* The update is downloaded but not installed immediately. The new content will be available the next time the application is resumed or restarted, whichever event happends first.
*/
ON_NEXT_RESUME = <any>"ON_NEXT_RESUME",
}
export enum SyncStatus {
/**
* The application is up to date.
*/
UP_TO_DATE = <any>"UP_TO_DATE",
/**
* An update is available, it has been downloaded, unzipped and copied to the deployment folder.
* After the completion of the callback invoked with SyncStatus.UPDATE_INSTALLED, the application will be reloaded with the updated code and resources.
*/
UPDATE_INSTALLED = <any>"UPDATE_INSTALLED",
/**
* An optional update is available, but the user declined to install it. The update was not downloaded.
*/
UPDATE_IGNORED = <any>"UPDATE_IGNORED",
/**
* An error happened during the sync operation. This might be an error while communicating with the server, downloading or unziping the update.
* The console logs should contain more information about what happened. No update has been applied in this case.
*/
ERROR = <any>"ERROR",
/**
* Returned if HMR is enabled and not overridden by the user.
*/
SKIPPING_BECAUSE_HMR_ENABLED = <any>"SKIPPING_BECAUSE_HMR_ENABLED",
/**
* There is an ongoing sync in progress, so this attempt to sync has been aborted.
*/
IN_PROGRESS = <any>"IN_PROGRESS",
/**
* Intermediate status - the plugin is about to check for updates.
*/
CHECKING_FOR_UPDATE = <any>"CHECKING_FOR_UPDATE",
/**
* Intermediate status - a user dialog is about to be displayed. This status will be reported only if user interaction is enabled.
*/
AWAITING_USER_ACTION = <any>"AWAITING_USER_ACTION",
/**
* Intermediate status - the update package is about to be downloaded.
*/
DOWNLOADING_PACKAGE = <any>"DOWNLOADING_PACKAGE",
/**
* Intermediate status - the update package is about to be installed.
*/
INSTALLING_UPDATE = <any>"INSTALLING_UPDATE"
}
export interface DownloadProgress {
totalBytes: number;
receivedBytes: number;
}
export class AppSync {
public static CURRENT_HASH_KEY: string = "APPSYNC_CURRENT_HASH"; // same as native
private static PENDING_HASH_KEY: string = "APPSYNC_PENDING_HASH"; // same as native
private static CLEAN_KEY: string = "APPSYNC_CLEAN"; // same as native (Android)
private static BINARY_FIRST_RUN_KEY: string = "BINARY_FIRST_RUN";
private static UNCONFIRMED_INSTALL_KEY: string = "UNCONFIRMED_INSTALL";
private static syncInProgress = false;
static sync(options: SyncOptions, syncCallback?: SuccessCallback<SyncStatus>, downloadProgress?: SuccessCallback<DownloadProgress>): void {
if (!options || !options.deploymentKey) {
throw new Error("Missing deploymentKey, pass it as part of the first parameter of the 'sync' function: { deploymentKey: 'your-key' }");
}
// skip AppSync when HMR is detected, unless it's explicitly allowed
// @ts-ignore
if (Boolean(module.hot) && !options.enabledWhenUsingHmr) {
syncCallback && syncCallback(SyncStatus.SKIPPING_BECAUSE_HMR_ENABLED);
return;
}
AppSync.syncInProgress = true;
// by default, use our Cloud server
options.serverUrl = options.serverUrl || "https://appsync-server.nativescript.org/";
AppSync.cleanPackagesIfNeeded();
AppSync.notifyApplicationReady(options.deploymentKey, options.serverUrl);
syncCallback && syncCallback(SyncStatus.CHECKING_FOR_UPDATE);
AppSync.checkForUpdate(options.deploymentKey, options.serverUrl).then(
(remotePackage?: IRemotePackage) => {
if (!remotePackage) {
syncCallback && syncCallback(SyncStatus.UP_TO_DATE);
AppSync.syncInProgress = false;
return;
}
if (options.ignoreFailedUpdates === undefined) {
options.ignoreFailedUpdates = true;
}
const updateShouldBeIgnored = remotePackage.failedInstall && options.ignoreFailedUpdates;
if (updateShouldBeIgnored) {
console.log("An update is available, but it is being ignored due to having been previously rolled back.");
syncCallback && syncCallback(SyncStatus.UP_TO_DATE);
AppSync.syncInProgress = false;
return;
}
const versionIsCurrent = remotePackage.packageHash === ApplicationSettings.getString(AppSync.CURRENT_HASH_KEY);
if (versionIsCurrent) {
console.log("Update already installed.");
syncCallback && syncCallback(SyncStatus.UP_TO_DATE);
AppSync.syncInProgress = false;
return;
}
const onError = (error: Error) => {
console.log("Download error: " + error);
syncCallback && syncCallback(SyncStatus.ERROR);
AppSync.syncInProgress = false;
};
const onInstallSuccess = () => {
ApplicationSettings.setString(AppSync.PENDING_HASH_KEY, remotePackage.packageHash);
ApplicationSettings.setString(AppSync.CURRENT_HASH_KEY, remotePackage.packageHash);
const onSuspend = () => {
Application.off("suspend", onSuspend);
this.killApp(false);
};
syncCallback && syncCallback(SyncStatus.UPDATE_INSTALLED, remotePackage.label);
const installMode = options.installMode || InstallMode.ON_NEXT_RESTART;
const mandatoryInstallMode = options.mandatoryInstallMode || InstallMode.ON_NEXT_RESUME;
switch (remotePackage.isMandatory ? mandatoryInstallMode : installMode) {
case InstallMode.ON_NEXT_RESTART:
console.log("Update is installed and will be run on the next app restart.");
break;
case InstallMode.ON_NEXT_RESUME:
console.log("Update is installed and will be run when the app next resumes.");
Application.on("suspend", onSuspend);
break;
case InstallMode.IMMEDIATE:
const updateDialogOptions = <UpdateDialogOptions>(options.updateDialog || {});
confirm({
title: updateDialogOptions.updateTitle,
message: (remotePackage.isMandatory ? updateDialogOptions.mandatoryUpdateMessage : updateDialogOptions.optionalUpdateMessage) + (updateDialogOptions.appendReleaseDescription ? "\n" + remotePackage.description : ""),
okButtonText: updateDialogOptions.mandatoryContinueButtonLabel || "Restart",
cancelButtonText: updateDialogOptions.optionalIgnoreButtonLabel || "Cancel",
cancelable: true
}).then(confirmed => {
if (confirmed) {
setTimeout(() => this.killApp(true), 300);
} else {
// fall back to next suspend/resume instead
Application.on("suspend", onSuspend);
}
});
break;
}
AppSync.syncInProgress = false;
};
const onDownloadSuccess = (localPackage: ILocalPackage) => {
syncCallback && syncCallback(SyncStatus.INSTALLING_UPDATE, remotePackage.label);
localPackage.install(onInstallSuccess, onError);
};
syncCallback && syncCallback(SyncStatus.DOWNLOADING_PACKAGE, remotePackage.label);
remotePackage.download(
onDownloadSuccess,
onError,
downloadProgress
);
},
(error: string) => {
console.log(error);
AppSync.syncInProgress = false;
if (syncCallback) {
syncCallback(SyncStatus.ERROR);
}
}
);
}
static checkForUpdate(deploymentKey: string, serverUrl?: string): Promise<IRemotePackage | undefined> {
return new Promise((resolve, reject) => {
// by default, use our Cloud server
serverUrl = serverUrl || "https://appsync-server.nativescript.org/";
const config: Configuration = {
serverUrl,
appVersion: AppVersion.getVersionNameSync(),
clientUniqueId: Device.uuid,
deploymentKey
};
AppSync.getCurrentPackage(config)
.then((queryPackage?: IPackage) => {
new TNSAcquisitionManager(deploymentKey, serverUrl).queryUpdateWithCurrentPackage(queryPackage, (error: Error, result: IRemotePackage | NativeUpdateNotification) => {
if (error) {
reject(error.message || error.toString());
}
if (!result || (<NativeUpdateNotification>result).updateAppVersion) {
resolve();
return;
}
// At this point we know there's an update available for the current version
const remotePackage: IRemotePackage = <IRemotePackage>result;
let tnsRemotePackage: IRemotePackage = new TNSRemotePackage();
tnsRemotePackage.description = remotePackage.description;
tnsRemotePackage.label = remotePackage.label;
tnsRemotePackage.appVersion = remotePackage.appVersion;
tnsRemotePackage.isMandatory = remotePackage.isMandatory;
tnsRemotePackage.packageHash = remotePackage.packageHash;
tnsRemotePackage.packageSize = remotePackage.packageSize;
tnsRemotePackage.downloadUrl = remotePackage.downloadUrl;
// the server doesn't send back the deploymentKey
tnsRemotePackage.deploymentKey = config.deploymentKey;
// TODO (low prio) see https://github.com/Microsoft/cordova-plugin-code-push/blob/055d9e625d47d56e707d9624c9a14a37736516bb/www/codePush.ts#L182
// .. or https://github.com/Microsoft/react-native-code-push/blob/2cd2ef0ca2e27a95f84579603c2d222188bb9ce5/CodePush.js#L84
tnsRemotePackage.failedInstall = false;
tnsRemotePackage.serverUrl = serverUrl;
resolve(tnsRemotePackage);
});
})
.catch(e => reject(e));
});
}
private static getCurrentPackage(config: Configuration): Promise<IPackage> {
return new Promise((resolve, reject) => {
resolve({
appVersion: config.appVersion,
deploymentKey: config.deploymentKey,
packageHash: ApplicationSettings.getString(AppSync.CURRENT_HASH_KEY),
isMandatory: false,
failedInstall: false,
description: undefined,
label: undefined,
packageSize: undefined,
serverUrl: config.serverUrl
});
});
}
private static notifyApplicationReady(deploymentKey: string, serverUrl?: string): void {
if (AppSync.isBinaryFirstRun()) {
// first run of a binary from the AppStore
AppSync.markBinaryAsFirstRun();
new TNSAcquisitionManager(deploymentKey, serverUrl).reportStatusDeploy(null, "DeploymentSucceeded");
} else if (!AppSync.hasPendingHash()) {
const currentPackageHash = ApplicationSettings.getString(AppSync.CURRENT_HASH_KEY, null);
if (currentPackageHash !== null && currentPackageHash !== AppSync.firstLaunchValue()) {
// first run of an update from AppSync
AppSync.markPackageAsFirstRun(currentPackageHash);
const currentPackage: ILocalPackage = <ILocalPackage>TNSLocalPackage.getCurrentPackage();
if (currentPackage !== null) {
currentPackage.isFirstRun = true;
new TNSAcquisitionManager(deploymentKey, serverUrl).reportStatusDeploy(currentPackage, "DeploymentSucceeded");
}
}
}
}
private static killApp(restartOnAndroid: boolean): void {
if (Application.android) {
if (restartOnAndroid) {
const packageManager = Application.android.context.getPackageManager();
const intent = packageManager.getLaunchIntentForPackage(Application.android.context.getPackageName());
const componentName = intent.getComponent();
//noinspection JSUnresolvedFunction,JSUnresolvedVariable
const mainIntent = new android.content.Intent.makeRestartActivityTask(componentName);
Application.android.context.startActivity(mainIntent);
//noinspection JSUnresolvedFunction,JSUnresolvedVariable
}
//noinspection JSUnresolvedFunction,JSUnresolvedVariable
android.os.Process.killProcess(android.os.Process.myPid());
} else if (Application.ios) {
exit(0);
}
}
private static cleanPackagesIfNeeded(): void {
const shouldClean = ApplicationSettings.getBoolean(AppSync.CLEAN_KEY, false);
if (!shouldClean) {
return;
}
ApplicationSettings.remove(AppSync.CLEAN_KEY);
ApplicationSettings.remove(AppSync.BINARY_FIRST_RUN_KEY);
TNSLocalPackage.clean();
}
private static isBinaryFirstRun(): boolean {
const firstRunFlagSet = ApplicationSettings.getBoolean(AppSync.BINARY_FIRST_RUN_KEY, false);
return !firstRunFlagSet;
}
/**
* This key exists until a restart is done (removed by native upon start).
* @returns {boolean}
*/
private static hasPendingHash(): boolean {
return ApplicationSettings.hasKey(AppSync.PENDING_HASH_KEY);
}
private static markBinaryAsFirstRun(): void {
ApplicationSettings.setBoolean(AppSync.BINARY_FIRST_RUN_KEY, true);
}
private static firstLaunchValue(): string {
return ApplicationSettings.getString(AppSync.UNCONFIRMED_INSTALL_KEY, null);
}
private static markPackageAsFirstRun(pack: string): void {
ApplicationSettings.setString(AppSync.UNCONFIRMED_INSTALL_KEY, pack);
}
}