-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTNSLocalPackage.js
More file actions
143 lines (143 loc) · 7.6 KB
/
TNSLocalPackage.js
File metadata and controls
143 lines (143 loc) · 7.6 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var nativescript_zip_1 = require("@nativescript/zip");
var core_1 = require("@nativescript/core");
var file_system_access_1 = require("@nativescript/core/file-system/file-system-access");
var app_sync_1 = require("./app-sync");
var TNSAcquisitionManager_1 = require("./TNSAcquisitionManager");
var TNSLocalPackage = /** @class */ (function () {
function TNSLocalPackage() {
}
TNSLocalPackage.prototype.install = function (installSuccess, errorCallback, installOptions) {
var _this = this;
var appFolderPath = core_1.knownFolders.documents().path + "/app";
var unzipFolderPath = core_1.knownFolders.documents().path + "/AppSync-Unzipped/" + this.packageHash;
var appSyncFolder = core_1.knownFolders.documents().path + "/AppSync";
// make sure the AppSync folder exists
core_1.Folder.fromPath(appSyncFolder);
var newPackageFolderPath = core_1.knownFolders.documents().path + "/AppSync/" + this.packageHash;
// in case of a rollback make 'newPackageFolderPath' could already exist, so check and remove
if (core_1.Folder.exists(newPackageFolderPath)) {
core_1.Folder.fromPath(newPackageFolderPath).removeSync();
}
var onUnzipComplete = function (success, error) {
if (!success) {
new TNSAcquisitionManager_1.TNSAcquisitionManager(_this.deploymentKey, _this.serverUrl).reportStatusDeploy(_this, "DeploymentFailed");
errorCallback && errorCallback(new Error(error));
return;
}
var previousHash = core_1.ApplicationSettings.getString(app_sync_1.AppSync.CURRENT_HASH_KEY, null);
var isDiffPackage = core_1.File.exists(unzipFolderPath + "/hotappsync.json");
if (isDiffPackage) {
var copySourceFolder = previousHash === null ? appFolderPath : core_1.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 file_system_access_1.FileSystemAccess().rename(unzipFolderPath, newPackageFolderPath, function (error) {
errorCallback && errorCallback(new Error(error));
return;
});
}
if (!core_1.isIOS) {
var pendingFolderPath = core_1.knownFolders.documents().path + "/AppSync/pending";
if (core_1.Folder.exists(pendingFolderPath)) {
core_1.Folder.fromPath(pendingFolderPath).removeSync();
}
if (!TNSLocalPackage.copyFolder(newPackageFolderPath, pendingFolderPath)) {
errorCallback && errorCallback(new Error("Failed to copy " + newPackageFolderPath + " to " + pendingFolderPath));
return;
}
}
core_1.ApplicationSettings.setString(TNSLocalPackage.APPSYNC_CURRENT_APPVERSION, _this.appVersion);
TNSLocalPackage.saveCurrentPackage(_this);
var buildTime;
// Note that this 'if' hardly justifies subclassing so we're not
if (core_1.isIOS) {
var plist = NSBundle.mainBundle.pathForResourceOfType(null, "plist");
var fileDate = new file_system_access_1.FileSystemAccess().getLastModified(plist);
buildTime = "" + fileDate.getTime();
}
else {
var appSyncApkBuildTimeStringId = core_1.Utils.android.resources.getStringId(TNSLocalPackage.APPSYNC_APK_BUILD_TIME);
buildTime = core_1.Utils.android.getApplicationContext().getResources().getString(appSyncApkBuildTimeStringId);
}
core_1.ApplicationSettings.setString(TNSLocalPackage.APPSYNC_CURRENT_APPBUILDTIME, buildTime);
//noinspection JSIgnoredPromiseFromCall (removal is async, don't really care if it fails)
core_1.File.fromPath(_this.localPath).remove();
installSuccess();
};
TNSLocalPackage.unzip(this.localPath, unzipFolderPath,
// TODO expose through plugin API (not that it's super useful)
function (percent) {
// console.log("AppSync package unzip progress: " + percent);
}, onUnzipComplete);
};
TNSLocalPackage.unzip = function (archive, destination, progressCallback, completionCallback) {
if (core_1.isIOS) {
TNSAppSync.unzipFileAtPathToDestinationOnProgressOnComplete(archive, destination, function (itemNr, totalNr) {
progressCallback(Math.floor((itemNr / totalNr) * 100));
}, function (path, success, error) {
completionCallback(success, error ? error.localizedDescription : null);
});
}
else {
nativescript_zip_1.Zip.unzip({
archive: archive,
directory: destination,
onProgress: progressCallback
}).then(function () {
completionCallback(true);
}, function (error) {
completionCallback(false, error);
});
}
};
TNSLocalPackage.clean = function () {
// note that we mustn't call this on Android, but it can't hurt to guard that
if (!core_1.isIOS) {
return;
}
core_1.ApplicationSettings.remove(TNSLocalPackage.APPSYNC_CURRENT_APPVERSION);
core_1.ApplicationSettings.remove(TNSLocalPackage.APPSYNC_CURRENT_APPBUILDTIME);
var appSyncFolder = core_1.Folder.fromPath(core_1.knownFolders.documents().path + "/AppSync");
//noinspection JSIgnoredPromiseFromCall
appSyncFolder.clear();
};
TNSLocalPackage.saveCurrentPackage = function (pack) {
core_1.ApplicationSettings.setString(TNSLocalPackage.APPSYNC_CURRENT_PACKAGE, JSON.stringify(pack));
};
TNSLocalPackage.getCurrentPackage = function () {
var packageStr = core_1.ApplicationSettings.getString(TNSLocalPackage.APPSYNC_CURRENT_PACKAGE, null);
return packageStr === null ? null : JSON.parse(packageStr);
};
TNSLocalPackage.copyFolder = function (fromPath, toPath) {
if (core_1.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;
}
}
};
// this is the app version at the moment the AppSync package was installed
TNSLocalPackage.APPSYNC_CURRENT_APPVERSION = "APPSYNC_CURRENT_APPVERSION"; // same as native
TNSLocalPackage.APPSYNC_CURRENT_PACKAGE = "APPSYNC_CURRENT_PACKAGE";
// this is the build timestamp of the app at the moment the AppSync package was installed
TNSLocalPackage.APPSYNC_CURRENT_APPBUILDTIME = "APPSYNC_CURRENT_APPBUILDTIME"; // same as native
TNSLocalPackage.APPSYNC_APK_BUILD_TIME = "APPSYNC_APK_BUILD_TIME"; // same as include.gradle
return TNSLocalPackage;
}());
exports.TNSLocalPackage = TNSLocalPackage;