forked from NativeScript/nativescript-app-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTNSLocalPackage.ts
172 lines (154 loc) · 6.95 KB
/
TNSLocalPackage.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
import { Zip } from "nativescript-zip";
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";
declare const com: any;
export class TNSLocalPackage implements ILocalPackage {
// this is the app version at the moment the AppSync package was installed
private static APPSYNC_CURRENT_APPVERSION: string = "APPSYNC_CURRENT_APPVERSION"; // same as native
private static APPSYNC_CURRENT_PACKAGE: string = "APPSYNC_CURRENT_PACKAGE";
// this is the build timestamp of the app at the moment the AppSync package was installed
private static APPSYNC_CURRENT_APPBUILDTIME: string = "APPSYNC_CURRENT_APPBUILDTIME"; // same as native
private static APPSYNC_APK_BUILD_TIME: string = "APPSYNC_APK_BUILD_TIME"; // same as include.gradle
localPath: string;
isFirstRun: boolean;
deploymentKey: string;
description: string;
label: string;
appVersion: string;
isMandatory: boolean;
packageHash: string;
packageSize: number;
failedInstall: boolean;
serverUrl: string;
install(installSuccess: Function, errorCallback?: ErrorCallback, installOptions?: InstallOptions): void {
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
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 (Folder.exists(newPackageFolderPath)) {
Folder.fromPath(newPackageFolderPath).removeSync();
}
const onUnzipComplete = (success: boolean, error?: string) => {
if (!success) {
new TNSAcquisitionManager(this.deploymentKey, this.serverUrl).reportStatusDeploy(this, "DeploymentFailed");
errorCallback && errorCallback(new Error(error));
return;
}
const previousHash = ApplicationSettings.getString(AppSync.CURRENT_HASH_KEY, null);
const isDiffPackage = File.exists(unzipFolderPath + "/hotappsync.json");
if (isDiffPackage) {
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;
}
if (!TNSLocalPackage.copyFolder(unzipFolderPath, newPackageFolderPath)) {
errorCallback && errorCallback(new Error(`Failed to copy ${unzipFolderPath} to ${newPackageFolderPath}`));
return;
}
} else {
new FileSystemAccess().rename(unzipFolderPath, newPackageFolderPath, (error) => {
errorCallback && errorCallback(new Error(error));
return;
});
}
if (!isIOS) {
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}`));
return;
}
}
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 FileSystemAccess().getLastModified(plist);
buildTime = "" + fileDate.getTime();
} else {
const appSyncApkBuildTimeStringId = Utils.android.resources.getStringId(TNSLocalPackage.APPSYNC_APK_BUILD_TIME);
buildTime = Utils.android.getApplicationContext().getResources().getString(appSyncApkBuildTimeStringId);
}
ApplicationSettings.setString(TNSLocalPackage.APPSYNC_CURRENT_APPBUILDTIME, buildTime);
//noinspection JSIgnoredPromiseFromCall (removal is async, don't really care if it fails)
File.fromPath(this.localPath).remove();
installSuccess();
};
TNSLocalPackage.unzip(
this.localPath,
unzipFolderPath,
// TODO expose through plugin API (not that it's super useful)
(percent: number) => {
// console.log("AppSync package unzip progress: " + percent);
},
onUnzipComplete);
}
static unzip(archive: string, destination: string, progressCallback: (progressPercent) => void, completionCallback: (success: boolean, error?: string) => void): void {
if (isIOS) {
TNSAppSync.unzipFileAtPathToDestinationOnProgressOnComplete(
archive,
destination,
(itemNr: number, totalNr: number) => {
progressCallback(Math.floor((itemNr / totalNr) * 100));
},
(path: string, success: boolean, error: NSError) => {
completionCallback(success, error ? error.localizedDescription : null);
}
);
} else {
Zip.unzip({
archive,
directory: destination,
onProgress: progressCallback
}).then(
() => {
completionCallback(true);
},
(error: string) => {
completionCallback(false, error);
}
);
}
}
static clean(): void {
// note that we mustn't call this on Android, but it can't hurt to guard that
if (!isIOS) {
return;
}
ApplicationSettings.remove(TNSLocalPackage.APPSYNC_CURRENT_APPVERSION);
ApplicationSettings.remove(TNSLocalPackage.APPSYNC_CURRENT_APPBUILDTIME);
const appSyncFolder = Folder.fromPath(knownFolders.documents().path + "/AppSync");
//noinspection JSIgnoredPromiseFromCall
appSyncFolder.clear();
}
private static saveCurrentPackage(pack: IPackage): void {
ApplicationSettings.setString(TNSLocalPackage.APPSYNC_CURRENT_PACKAGE, JSON.stringify(pack));
}
static getCurrentPackage(): IPackage {
const packageStr: string = ApplicationSettings.getString(TNSLocalPackage.APPSYNC_CURRENT_PACKAGE, null);
return packageStr === null ? null : JSON.parse(packageStr);
}
private static copyFolder(fromPath: string, toPath: string): boolean {
if (isIOS) {
return TNSAppSync.copyEntriesInFolderDestFolderError(fromPath, toPath);
} else {
try {
com.tns.TNSAppSync.copyDirectoryContents(fromPath, toPath);
return true;
} catch (error) {
console.log(`Copy error on Android: ${error}`);
return false;
}
}
}
}