From 453573bf30a37b34d53d66e660122e90aa8455ca Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Sun, 8 Dec 2019 21:31:10 -0800 Subject: [PATCH 1/2] chore: update nativescript-zip to latest and use {N} scoped --- .gitignore | 3 +- demo-ng/package.json | 10 +++--- demo/package.json | 14 ++++---- src/TNSAcquisitionManager.ts | 4 +-- src/TNSLocalPackage.ts | 63 ++++++++++++++++++------------------ src/TNSRemotePackage.ts | 9 +++--- src/TNSRequester.ts | 4 +-- src/app-sync.ts | 48 +++++++++++++-------------- src/package.json | 12 +++---- src/tsconfig.json | 1 - 10 files changed, 83 insertions(+), 85 deletions(-) diff --git a/.gitignore b/.gitignore index 14e2a2e..f93800f 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,5 @@ demo/platforms publish/src publish/package -node_modules \ No newline at end of file +node_modules +package-lock.json \ No newline at end of file diff --git a/demo-ng/package.json b/demo-ng/package.json index ef3c0dd..7571b59 100644 --- a/demo-ng/package.json +++ b/demo-ng/package.json @@ -2,10 +2,10 @@ "nativescript": { "id": "org.nativescript.demong.AppSync", "tns-android": { - "version": "6.1.2" + "version": "6.2.0" }, "tns-ios": { - "version": "6.1.0" + "version": "6.2.0" } }, "description": "NativeScript Application", @@ -47,13 +47,13 @@ "nativescript-theme-core": "~1.0.6", "reflect-metadata": "~0.1.13", "rxjs": "~6.5.2", - "tns-core-modules": "~6.1.0", + "tns-core-modules": "~6.2.0", "zone.js": "~0.10.0" }, "devDependencies": { "@angular/compiler-cli": "~8.2.3", "@ngtools/webpack": "~8.2.2", - "nativescript-dev-webpack": "~1.2.0", - "typescript": "3.4.5" + "nativescript-dev-webpack": "~1.3.0", + "typescript": "~3.5.3" } } diff --git a/demo/package.json b/demo/package.json index 2a8bfc2..5e933a7 100644 --- a/demo/package.json +++ b/demo/package.json @@ -2,10 +2,10 @@ "nativescript": { "id": "org.nativescript.plugindemo.AppSync", "tns-ios": { - "version": "6.0.2" + "version": "6.2.0" }, "tns-android": { - "version": "6.0.2" + "version": "6.2.0" } }, "scripts": { @@ -34,7 +34,7 @@ "nativescript-app-sync": "file:../publish/package/nativescript-app-sync-1.0.7.tgz", "nativescript-theme-core": "~1.0.4", "nativescript-unit-test-runner": "0.7.0", - "tns-core-modules": "~6.0.0" + "tns-core-modules": "~6.2.0" }, "devDependencies": { "babel-traverse": "6.26.0", @@ -46,10 +46,10 @@ "karma-jasmine": "2.0.1", "karma-nativescript-launcher": "^0.4.0", "lazy": "1.0.11", - "nativescript-dev-webpack": "1.0.1", - "tns-platform-declarations": "~6.0.0", - "tslint": "~5.4.3", - "typescript": "3.4.5", + "nativescript-dev-webpack": "1.3.0", + "tns-platform-declarations": "~6.2.0", + "tslint": "~5.20.0", + "typescript": "~3.5.3", "karma-webpack": "3.0.5" } } diff --git a/src/TNSAcquisitionManager.ts b/src/TNSAcquisitionManager.ts index bbb8263..cec2588 100644 --- a/src/TNSAcquisitionManager.ts +++ b/src/TNSAcquisitionManager.ts @@ -1,6 +1,6 @@ import * as AppVersion from "nativescript-appversion"; import { AcquisitionManager as AppSyncSDK } from "nativescript-app-sync-sdk/script/acquisition-sdk"; -import { device } from "tns-core-modules/platform"; +import { Device } from "@nativescript/core"; import { TNSRequester } from "./TNSRequester"; export class TNSAcquisitionManager { @@ -11,7 +11,7 @@ export class TNSAcquisitionManager { const config: Configuration = { serverUrl, appVersion: AppVersion.getVersionNameSync(), - clientUniqueId: device.uuid, + clientUniqueId: Device.uuid, deploymentKey }; this.appSyncSDK = new AppSyncSDK(new TNSRequester(), config); diff --git a/src/TNSLocalPackage.ts b/src/TNSLocalPackage.ts index b63620e..85f1bbd 100644 --- a/src/TNSLocalPackage.ts +++ b/src/TNSLocalPackage.ts @@ -1,9 +1,6 @@ import { Zip } from "nativescript-zip"; -import * as appSettings from "tns-core-modules/application-settings"; -import * as fs from "tns-core-modules/file-system"; -import * as fsa from "tns-core-modules/file-system/file-system-access"; -import { isIOS } from "tns-core-modules/platform"; -import * as utils from "tns-core-modules/utils/utils"; +import { isIOS, ApplicationSettings, knownFolders, File, Folder, Utils } from "@nativescript/core"; +import { FileSystemAccess } from "@nativescript/core/file-system/file-system-access"; import { AppSync } from "./app-sync"; import { TNSAcquisitionManager } from "./TNSAcquisitionManager"; @@ -30,15 +27,15 @@ export class TNSLocalPackage implements ILocalPackage { serverUrl: string; install(installSuccess: Function, errorCallback?: ErrorCallback, installOptions?: InstallOptions): void { - let appFolderPath = fs.knownFolders.documents().path + "/app"; - let unzipFolderPath = fs.knownFolders.documents().path + "/AppSync-Unzipped/" + this.packageHash; - let appSyncFolder = fs.knownFolders.documents().path + "/AppSync"; + let appFolderPath = knownFolders.documents().path + "/app"; + let unzipFolderPath = knownFolders.documents().path + "/AppSync-Unzipped/" + this.packageHash; + let appSyncFolder = knownFolders.documents().path + "/AppSync"; // make sure the AppSync folder exists - fs.Folder.fromPath(appSyncFolder); - let newPackageFolderPath = fs.knownFolders.documents().path + "/AppSync/" + this.packageHash; + Folder.fromPath(appSyncFolder); + let newPackageFolderPath = knownFolders.documents().path + "/AppSync/" + this.packageHash; // in case of a rollback make 'newPackageFolderPath' could already exist, so check and remove - if (fs.Folder.exists(newPackageFolderPath)) { - fs.Folder.fromPath(newPackageFolderPath).removeSync(); + if (Folder.exists(newPackageFolderPath)) { + Folder.fromPath(newPackageFolderPath).removeSync(); } const onUnzipComplete = (success: boolean, error?: string) => { @@ -48,10 +45,10 @@ export class TNSLocalPackage implements ILocalPackage { return; } - const previousHash = appSettings.getString(AppSync.CURRENT_HASH_KEY, null); - const isDiffPackage = fs.File.exists(unzipFolderPath + "/hotappsync.json"); + const previousHash = ApplicationSettings.getString(AppSync.CURRENT_HASH_KEY, null); + const isDiffPackage = File.exists(unzipFolderPath + "/hotappsync.json"); if (isDiffPackage) { - const copySourceFolder = previousHash === null ? appFolderPath : fs.knownFolders.documents().path + "/AppSync/" + previousHash; + const copySourceFolder = previousHash === null ? appFolderPath : knownFolders.documents().path + "/AppSync/" + previousHash; if (!TNSLocalPackage.copyFolder(copySourceFolder, newPackageFolderPath)) { errorCallback && errorCallback(new Error(`Failed to copy ${copySourceFolder} to ${newPackageFolderPath}`)); return; @@ -61,16 +58,16 @@ export class TNSLocalPackage implements ILocalPackage { return; } } else { - new fsa.FileSystemAccess().rename(unzipFolderPath, newPackageFolderPath, (error) => { + new FileSystemAccess().rename(unzipFolderPath, newPackageFolderPath, (error) => { errorCallback && errorCallback(new Error(error)); return; }); } if (!isIOS) { - let pendingFolderPath = fs.knownFolders.documents().path + "/AppSync/pending"; - if (fs.Folder.exists(pendingFolderPath)) { - fs.Folder.fromPath(pendingFolderPath).removeSync(); + let pendingFolderPath = knownFolders.documents().path + "/AppSync/pending"; + if (Folder.exists(pendingFolderPath)) { + Folder.fromPath(pendingFolderPath).removeSync(); } if (!TNSLocalPackage.copyFolder(newPackageFolderPath, pendingFolderPath)) { errorCallback && errorCallback(new Error(`Failed to copy ${newPackageFolderPath} to ${pendingFolderPath}`)); @@ -78,22 +75,22 @@ export class TNSLocalPackage implements ILocalPackage { } } - appSettings.setString(TNSLocalPackage.APPSYNC_CURRENT_APPVERSION, this.appVersion); + ApplicationSettings.setString(TNSLocalPackage.APPSYNC_CURRENT_APPVERSION, this.appVersion); TNSLocalPackage.saveCurrentPackage(this); let buildTime: string; // Note that this 'if' hardly justifies subclassing so we're not if (isIOS) { const plist = NSBundle.mainBundle.pathForResourceOfType(null, "plist"); - const fileDate = new fsa.FileSystemAccess().getLastModified(plist); + const fileDate = new FileSystemAccess().getLastModified(plist); buildTime = "" + fileDate.getTime(); } else { - const appSyncApkBuildTimeStringId = utils.ad.resources.getStringId(TNSLocalPackage.APPSYNC_APK_BUILD_TIME); - buildTime = utils.ad.getApplicationContext().getResources().getString(appSyncApkBuildTimeStringId); + const appSyncApkBuildTimeStringId = Utils.android.resources.getStringId(TNSLocalPackage.APPSYNC_APK_BUILD_TIME); + buildTime = Utils.android.getApplicationContext().getResources().getString(appSyncApkBuildTimeStringId); } - appSettings.setString(TNSLocalPackage.APPSYNC_CURRENT_APPBUILDTIME, buildTime); + ApplicationSettings.setString(TNSLocalPackage.APPSYNC_CURRENT_APPBUILDTIME, buildTime); //noinspection JSIgnoredPromiseFromCall (removal is async, don't really care if it fails) - fs.File.fromPath(this.localPath).remove(); + File.fromPath(this.localPath).remove(); installSuccess(); }; @@ -121,7 +118,11 @@ export class TNSLocalPackage implements ILocalPackage { } ); } else { - Zip.unzipWithProgress(archive, destination, progressCallback).then( + Zip.unzip({ + archive, + directory: destination, + onProgress: progressCallback + }).then( () => { completionCallback(true); }, @@ -138,20 +139,20 @@ export class TNSLocalPackage implements ILocalPackage { return; } - appSettings.remove(TNSLocalPackage.APPSYNC_CURRENT_APPVERSION); - appSettings.remove(TNSLocalPackage.APPSYNC_CURRENT_APPBUILDTIME); + ApplicationSettings.remove(TNSLocalPackage.APPSYNC_CURRENT_APPVERSION); + ApplicationSettings.remove(TNSLocalPackage.APPSYNC_CURRENT_APPBUILDTIME); - const appSyncFolder = fs.Folder.fromPath(fs.knownFolders.documents().path + "/AppSync"); + const appSyncFolder = Folder.fromPath(knownFolders.documents().path + "/AppSync"); //noinspection JSIgnoredPromiseFromCall appSyncFolder.clear(); } private static saveCurrentPackage(pack: IPackage): void { - appSettings.setString(TNSLocalPackage.APPSYNC_CURRENT_PACKAGE, JSON.stringify(pack)); + ApplicationSettings.setString(TNSLocalPackage.APPSYNC_CURRENT_PACKAGE, JSON.stringify(pack)); } static getCurrentPackage(): IPackage { - const packageStr: string = appSettings.getString(TNSLocalPackage.APPSYNC_CURRENT_PACKAGE, null); + const packageStr: string = ApplicationSettings.getString(TNSLocalPackage.APPSYNC_CURRENT_PACKAGE, null); return packageStr === null ? null : JSON.parse(packageStr); } diff --git a/src/TNSRemotePackage.ts b/src/TNSRemotePackage.ts index 5697d08..f1504a4 100644 --- a/src/TNSRemotePackage.ts +++ b/src/TNSRemotePackage.ts @@ -1,5 +1,4 @@ -import { File } from "tns-core-modules/file-system"; -import { getFile } from "tns-core-modules/http"; +import { File, Http } from "@nativescript/core"; import { TNSAcquisitionManager } from "./TNSAcquisitionManager"; import { TNSLocalPackage } from "./TNSLocalPackage"; @@ -36,15 +35,15 @@ export class TNSRemotePackage implements IRemotePackage { }; // download, with a silly but effective retry mechanism - getFile(this.downloadUrl) + Http.getFile(this.downloadUrl) .then(onDownloadSuccess) .catch(() => { setTimeout(() => { - getFile(this.downloadUrl) + Http.getFile(this.downloadUrl) .then(onDownloadSuccess) .catch(() => { setTimeout(() => { - getFile(this.downloadUrl) + Http.getFile(this.downloadUrl) .then(onDownloadSuccess) .catch((e: any) => downloadError(new Error("Could not download remote package. " + e))); }, 3000); diff --git a/src/TNSRequester.ts b/src/TNSRequester.ts index 670e605..338eb56 100644 --- a/src/TNSRequester.ts +++ b/src/TNSRequester.ts @@ -1,4 +1,4 @@ -import { HttpResponse, request } from "tns-core-modules/http"; +import { HttpResponse, Http as nsHttp } from "@nativescript/core"; import Requester = Http.Requester; const packageJson = require("./package.json"); @@ -16,7 +16,7 @@ export class TNSRequester implements Requester { requestBody = JSON.stringify(requestBody); } - request({ + nsHttp.request({ method: TNSRequester.getHttpMethodName(verb), url, content: requestBody, diff --git a/src/app-sync.ts b/src/app-sync.ts index b7bf20f..41808e7 100644 --- a/src/app-sync.ts +++ b/src/app-sync.ts @@ -1,10 +1,8 @@ /// -import * as appSettings from "application-settings"; import * as AppVersion from "nativescript-appversion"; -import * as application from "tns-core-modules/application"; -import { device } from "tns-core-modules/platform"; -import { confirm } from "tns-core-modules/ui/dialogs"; +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"; @@ -143,11 +141,11 @@ export class AppSync { }; const onInstallSuccess = () => { - appSettings.setString(AppSync.PENDING_HASH_KEY, remotePackage.packageHash); - appSettings.setString(AppSync.CURRENT_HASH_KEY, remotePackage.packageHash); + ApplicationSettings.setString(AppSync.PENDING_HASH_KEY, remotePackage.packageHash); + ApplicationSettings.setString(AppSync.CURRENT_HASH_KEY, remotePackage.packageHash); const onSuspend = () => { - application.off("suspend", onSuspend); + Application.off("suspend", onSuspend); this.killApp(false); }; @@ -163,7 +161,7 @@ export class AppSync { case InstallMode.ON_NEXT_RESUME: console.log("Update is installed and will be run when the app next resumes."); - application.on("suspend", onSuspend); + Application.on("suspend", onSuspend); break; case InstallMode.IMMEDIATE: @@ -179,7 +177,7 @@ export class AppSync { setTimeout(() => this.killApp(true), 300); } else { // fall back to next suspend/resume instead - application.on("suspend", onSuspend); + Application.on("suspend", onSuspend); } }); break; @@ -219,7 +217,7 @@ export class AppSync { const config: Configuration = { serverUrl, appVersion: AppVersion.getVersionNameSync(), - clientUniqueId: device.uuid, + clientUniqueId: Device.uuid, deploymentKey }; @@ -265,7 +263,7 @@ export class AppSync { resolve({ appVersion: config.appVersion, deploymentKey: config.deploymentKey, - packageHash: appSettings.getString(AppSync.CURRENT_HASH_KEY), + packageHash: ApplicationSettings.getString(AppSync.CURRENT_HASH_KEY), isMandatory: false, failedInstall: false, description: undefined, @@ -283,7 +281,7 @@ export class AppSync { new TNSAcquisitionManager(deploymentKey, serverUrl).reportStatusDeploy(null, "DeploymentSucceeded"); } else if (!AppSync.hasPendingHash()) { - const currentPackageHash = appSettings.getString(AppSync.CURRENT_HASH_KEY, null); + 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); @@ -297,37 +295,37 @@ export class AppSync { } private static killApp(restartOnAndroid: boolean): void { - if (application.android) { + if (Application.android) { if (restartOnAndroid) { //noinspection JSUnresolvedFunction,JSUnresolvedVariable - const mStartActivity = new android.content.Intent(application.android.context, application.android.startActivity.getClass()); + const mStartActivity = new android.content.Intent(Application.android.context, Application.android.startActivity.getClass()); const mPendingIntentId = parseInt("" + (Math.random() * 100000), 10); //noinspection JSUnresolvedFunction,JSUnresolvedVariable - const mPendingIntent = android.app.PendingIntent.getActivity(application.android.context, mPendingIntentId, mStartActivity, android.app.PendingIntent.FLAG_CANCEL_CURRENT); + const mPendingIntent = android.app.PendingIntent.getActivity(Application.android.context, mPendingIntentId, mStartActivity, android.app.PendingIntent.FLAG_CANCEL_CURRENT); //noinspection JSUnresolvedFunction,JSUnresolvedVariable - const mgr = application.android.context.getSystemService(android.content.Context.ALARM_SERVICE); + const mgr = Application.android.context.getSystemService(android.content.Context.ALARM_SERVICE); //noinspection JSUnresolvedFunction,JSUnresolvedVariable mgr.set(android.app.AlarmManager.RTC, java.lang.System.currentTimeMillis() + 100, mPendingIntent); //noinspection JSUnresolvedFunction,JSUnresolvedVariable } android.os.Process.killProcess(android.os.Process.myPid()); - } else if (application.ios) { + } else if (Application.ios) { exit(0); } } private static cleanPackagesIfNeeded(): void { - const shouldClean = appSettings.getBoolean(AppSync.CLEAN_KEY, false); + const shouldClean = ApplicationSettings.getBoolean(AppSync.CLEAN_KEY, false); if (!shouldClean) { return; } - appSettings.remove(AppSync.CLEAN_KEY); - appSettings.remove(AppSync.BINARY_FIRST_RUN_KEY); + ApplicationSettings.remove(AppSync.CLEAN_KEY); + ApplicationSettings.remove(AppSync.BINARY_FIRST_RUN_KEY); TNSLocalPackage.clean(); } private static isBinaryFirstRun(): boolean { - const firstRunFlagSet = appSettings.getBoolean(AppSync.BINARY_FIRST_RUN_KEY, false); + const firstRunFlagSet = ApplicationSettings.getBoolean(AppSync.BINARY_FIRST_RUN_KEY, false); return !firstRunFlagSet; } @@ -336,18 +334,18 @@ export class AppSync { * @returns {boolean} */ private static hasPendingHash(): boolean { - return appSettings.hasKey(AppSync.PENDING_HASH_KEY); + return ApplicationSettings.hasKey(AppSync.PENDING_HASH_KEY); } private static markBinaryAsFirstRun(): void { - appSettings.setBoolean(AppSync.BINARY_FIRST_RUN_KEY, true); + ApplicationSettings.setBoolean(AppSync.BINARY_FIRST_RUN_KEY, true); } private static firstLaunchValue(): string { - return appSettings.getString(AppSync.UNCONFIRMED_INSTALL_KEY, null); + return ApplicationSettings.getString(AppSync.UNCONFIRMED_INSTALL_KEY, null); } private static markPackageAsFirstRun(pack: string): void { - appSettings.setString(AppSync.UNCONFIRMED_INSTALL_KEY, pack); + ApplicationSettings.setString(AppSync.UNCONFIRMED_INSTALL_KEY, pack); } } diff --git a/src/package.json b/src/package.json index 663e9da..f9705a2 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "nativescript-app-sync", - "version": "1.0.7", + "version": "2.0.0", "description": "Use AppSync to hot deploy updates to your app.", "main": "app-sync", "typings": "index.d.ts", @@ -69,15 +69,15 @@ "readmeFilename": "README.md", "dependencies": { "nativescript-app-sync-sdk": "~1.0.1", - "nativescript-appversion": "~1.4.2", + "nativescript-appversion": "~1.4.4", "nativescript-hook": "~0.2.5", - "nativescript-zip": "git+https://github.com/EddyVerbruggen/nativescript-zip.git#330fa2d" + "nativescript-zip": "~4.0.2" }, "devDependencies": { "rimraf": "~2.6.3", - "tns-core-modules": "~5.4.2", - "tns-platform-declarations": "~5.4.2", + "tns-core-modules": "~6.2.0", + "tns-platform-declarations": "~6.2.0", "tslint": "~5.12.0", - "typescript": "~3.3.0" + "typescript": "~3.5.3" } } diff --git a/src/tsconfig.json b/src/tsconfig.json index e43389f..fd9fcb1 100644 --- a/src/tsconfig.json +++ b/src/tsconfig.json @@ -14,7 +14,6 @@ "baseUrl": ".", "paths": { "*": [ - "./node_modules/tns-core-modules/*", "./node_modules/*" ], "~/*": [ From 5455e5e7024955dddeab25336da80983f98bd791 Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Mon, 9 Dec 2019 23:35:40 -0800 Subject: [PATCH 2/2] chore: expose TNSLocalPackage --- src/app-sync.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/app-sync.ts b/src/app-sync.ts index 41808e7..3982e80 100644 --- a/src/app-sync.ts +++ b/src/app-sync.ts @@ -7,6 +7,8 @@ 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.