Skip to content

Commit 90e2691

Browse files
Allow an arbitrary server URL #5
1 parent bdaccef commit 90e2691

File tree

6 files changed

+32
-16
lines changed

6 files changed

+32
-16
lines changed

demo/demoapp/main-view-model.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as application from "tns-core-modules/application";
33
import { Observable } from "tns-core-modules/data/observable";
44
import { isIOS } from "tns-core-modules/platform";
55

6+
// TODO add a demo which asks the user to restart the app to update it (with a 'confirm' and nativescript-exit)
67
export class HelloWorldModel extends Observable {
78
private codePush: CodePush;
89

@@ -34,7 +35,8 @@ export class HelloWorldModel extends Observable {
3435
CodePush.sync({
3536
deploymentKey: isIOS ? HelloWorldModel.CODEPUSH_IOS_STAGING_KEY : HelloWorldModel.CODEPUSH_ANDROID_STAGING_KEY,
3637
installMode: InstallMode.ON_NEXT_RESTART, // has not effect currently, always using ON_NEXT_RESTART
37-
mandatoryInstallMode: InstallMode.IMMEDIATE // has not effect currently, always using ON_NEXT_RESTART
38+
mandatoryInstallMode: InstallMode.IMMEDIATE, // has not effect currently, always using ON_NEXT_RESTART
39+
// serverUrl: "https://www.nu.nl"
3840
}, (syncStatus: SyncStatus): void => {
3941
console.log("syncStatus: " + syncStatus);
4042
if (syncStatus === SyncStatus.UP_TO_DATE) {

src/TNSAcquisitionManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ export class TNSAcquisitionManager {
77

88
private codePushSDK: AcquisitionManager;
99

10-
constructor(deploymentKey: string) {
10+
constructor(deploymentKey: string, serverUrl: string) {
1111
const config: Configuration = {
12-
serverUrl: "https://nativescript-codepush-server.herokuapp.com/",
12+
serverUrl,
1313
appVersion: AppVersion.getVersionNameSync(),
1414
clientUniqueId: device.uuid,
1515
deploymentKey

src/TNSLocalPackage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class TNSLocalPackage implements ILocalPackage {
2727
packageHash: string;
2828
packageSize: number;
2929
failedInstall: boolean;
30+
serverUrl: string;
3031

3132
install(installSuccess: SuccessCallback<InstallMode>, errorCallback?: ErrorCallback, installOptions?: InstallOptions): void {
3233
let appFolderPath = fs.knownFolders.documents().path + "/app";
@@ -42,7 +43,7 @@ export class TNSLocalPackage implements ILocalPackage {
4243

4344
const onUnzipComplete = (success: boolean, error?: string) => {
4445
if (!success) {
45-
new TNSAcquisitionManager(this.deploymentKey).reportStatusDeploy(this, "DeploymentFailed");
46+
new TNSAcquisitionManager(this.deploymentKey, this.serverUrl).reportStatusDeploy(this, "DeploymentFailed");
4647
errorCallback && errorCallback(new Error(error));
4748
return;
4849
}

src/TNSRemotePackage.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class TNSRemotePackage implements IRemotePackage {
1313
packageHash: string;
1414
packageSize: number;
1515
failedInstall: boolean;
16+
serverUrl: string;
1617

1718
download(downloadSuccess: SuccessCallback<ILocalPackage>, downloadError?: ErrorCallback, downloadProgress?: SuccessCallback<DownloadProgress>): void {
1819
getFile(this.downloadUrl).then(
@@ -28,10 +29,11 @@ export class TNSRemotePackage implements IRemotePackage {
2829
tnsLocalPackage.isFirstRun = false;
2930
// TODO (low prio) see https://github.com/Microsoft/cordova-plugin-code-push/blob/055d9e625d47d56e707d9624c9a14a37736516bb/www/remotePackage.ts#L55 (but prolly not too relevant)
3031
tnsLocalPackage.failedInstall = false;
32+
tnsLocalPackage.serverUrl = this.serverUrl;
3133

3234
downloadSuccess(tnsLocalPackage);
3335

34-
new TNSAcquisitionManager(this.deploymentKey).reportStatusDownload(tnsLocalPackage);
36+
new TNSAcquisitionManager(this.deploymentKey, this.serverUrl).reportStatusDownload(tnsLocalPackage);
3537
},
3638
(e: any) => {
3739
downloadError(new Error("Could not access local package. " + e));

src/code-push-lib.d.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ interface IPackage {
3333
packageHash: string;
3434
packageSize: number;
3535
failedInstall: boolean;
36+
serverUrl: string;
3637
}
3738

3839
/**
@@ -324,9 +325,14 @@ interface SyncOptions extends InstallOptions {
324325
updateDialog?: boolean | UpdateDialogOptions;
325326

326327
/**
327-
* Overrides the config.xml deployment key when checking for updates.
328+
* The deployment key when checking for updates.
329+
*/
330+
deploymentKey: string;
331+
332+
/**
333+
* Overrides the default server URL (https://nativescript-codepush-server.herokuapp.com/).
328334
*/
329-
deploymentKey?: string;
335+
serverUrl?: string;
330336
}
331337

332338
/**

src/code-push.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ export class CodePush {
101101

102102
CodePush.cleanPackagesIfNeeded();
103103

104-
CodePush.notifyApplicationReady(options.deploymentKey);
104+
CodePush.notifyApplicationReady(options.deploymentKey, options.serverUrl);
105105

106106
syncCallback && syncCallback(SyncStatus.CHECKING_FOR_UPDATE);
107-
CodePush.checkForUpdate(options.deploymentKey).then(
107+
CodePush.checkForUpdate(options.deploymentKey, options.serverUrl).then(
108108
(remotePackage?: IRemotePackage) => {
109109
if (!remotePackage) {
110110
syncCallback && syncCallback(SyncStatus.UP_TO_DATE);
@@ -173,17 +173,20 @@ export class CodePush {
173173
);
174174
}
175175

176-
static checkForUpdate(deploymentKey: string): Promise<IRemotePackage> {
176+
static checkForUpdate(deploymentKey: string, serverUrl?: string): Promise<IRemotePackage> {
177177
return new Promise((resolve, reject) => {
178+
// by default, use our Cloud server
179+
serverUrl = serverUrl || "https://nativescript-codepush-server.herokuapp.com/";
180+
178181
const config: Configuration = {
179-
serverUrl: "https://nativescript-codepush-server.herokuapp.com/",
182+
serverUrl,
180183
appVersion: AppVersion.getVersionNameSync(),
181184
clientUniqueId: device.uuid,
182185
deploymentKey
183186
};
184187

185188
CodePush.getCurrentPackage(config).then((queryPackage?: IPackage) => {
186-
new TNSAcquisitionManager(deploymentKey).queryUpdateWithCurrentPackage(queryPackage, (error: Error, result: IRemotePackage | NativeUpdateNotification) => {
189+
new TNSAcquisitionManager(deploymentKey, serverUrl).queryUpdateWithCurrentPackage(queryPackage, (error: Error, result: IRemotePackage | NativeUpdateNotification) => {
187190
if (error) {
188191
reject(error.message || error.toString());
189192
}
@@ -209,6 +212,7 @@ export class CodePush {
209212
// TODO (low prio) see https://github.com/Microsoft/cordova-plugin-code-push/blob/055d9e625d47d56e707d9624c9a14a37736516bb/www/codePush.ts#L182
210213
// .. or https://github.com/Microsoft/react-native-code-push/blob/2cd2ef0ca2e27a95f84579603c2d222188bb9ce5/CodePush.js#L84
211214
tnsRemotePackage.failedInstall = false;
215+
tnsRemotePackage.serverUrl = serverUrl;
212216

213217
resolve(tnsRemotePackage);
214218
});
@@ -226,7 +230,8 @@ export class CodePush {
226230
failedInstall: false,
227231
description: undefined,
228232
label: undefined,
229-
packageSize: undefined
233+
packageSize: undefined,
234+
serverUrl: config.serverUrl
230235
});
231236
});
232237
}
@@ -241,11 +246,11 @@ export class CodePush {
241246
TNSLocalPackage.clean();
242247
}
243248

244-
static notifyApplicationReady(deploymentKey: string): void {
249+
static notifyApplicationReady(deploymentKey: string, serverUrl?: string): void {
245250
if (CodePush.isBinaryFirstRun()) {
246251
// first run of a binary from the AppStore
247252
CodePush.markBinaryAsFirstRun();
248-
new TNSAcquisitionManager(deploymentKey).reportStatusDeploy(null, "DeploymentSucceeded");
253+
new TNSAcquisitionManager(deploymentKey, serverUrl).reportStatusDeploy(null, "DeploymentSucceeded");
249254

250255
} else if (!CodePush.hasPendingHash()) {
251256
const currentPackageHash = appSettings.getString(CodePush.CURRENT_HASH_KEY, null);
@@ -255,7 +260,7 @@ export class CodePush {
255260
const currentPackage: ILocalPackage = <ILocalPackage>TNSLocalPackage.getCurrentPackage();
256261
if (currentPackage !== null) {
257262
currentPackage.isFirstRun = true;
258-
new TNSAcquisitionManager(deploymentKey).reportStatusDeploy(currentPackage, "DeploymentSucceeded");
263+
new TNSAcquisitionManager(deploymentKey, serverUrl).reportStatusDeploy(currentPackage, "DeploymentSucceeded");
259264
}
260265
}
261266
}

0 commit comments

Comments
 (0)