nativescript-code-push
Version:
Use CodePush to hot deploy updates to your app.
143 lines (142 loc) • 7.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var nativescript_zip_1 = require("nativescript-zip");
var appSettings = require("tns-core-modules/application-settings");
var fs = require("tns-core-modules/file-system");
var fsa = require("tns-core-modules/file-system/file-system-access");
var platform_1 = require("tns-core-modules/platform");
var utils = require("tns-core-modules/utils/utils");
var code_push_1 = require("./code-push");
var TNSAcquisitionManager_1 = require("./TNSAcquisitionManager");
var TNSLocalPackage = /** @class */ (function () {
function TNSLocalPackage() {
}
TNSLocalPackage.prototype.install = function (installSuccess, errorCallback, installOptions) {
var _this = this;
var appFolderPath = fs.knownFolders.documents().path + "/app";
var unzipFolderPath = fs.knownFolders.documents().path + "/CodePush-Unzipped/" + this.packageHash;
var codePushFolder = fs.knownFolders.documents().path + "/CodePush";
// make sure the CodePush folder exists
fs.Folder.fromPath(codePushFolder);
var newPackageFolderPath = fs.knownFolders.documents().path + "/CodePush/" + 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();
}
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 = appSettings.getString(code_push_1.CodePush.CURRENT_HASH_KEY, null);
var isDiffPackage = fs.File.exists(unzipFolderPath + "/hotcodepush.json");
if (isDiffPackage) {
var copySourceFolder = previousHash === null ? appFolderPath : fs.knownFolders.documents().path + "/CodePush/" + 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 fsa.FileSystemAccess().rename(unzipFolderPath, newPackageFolderPath, function (error) {
errorCallback && errorCallback(new Error(error));
return;
});
}
if (!platform_1.isIOS) {
var pendingFolderPath = fs.knownFolders.documents().path + "/CodePush/pending";
if (fs.Folder.exists(pendingFolderPath)) {
fs.Folder.fromPath(pendingFolderPath).removeSync();
}
if (!TNSLocalPackage.copyFolder(newPackageFolderPath, pendingFolderPath)) {
errorCallback && errorCallback(new Error("Failed to copy " + newPackageFolderPath + " to " + pendingFolderPath));
return;
}
}
appSettings.setString(TNSLocalPackage.CODEPUSH_CURRENT_APPVERSION, _this.appVersion);
TNSLocalPackage.saveCurrentPackage(_this);
var buildTime;
// Note that this 'if' hardly justifies subclassing so we're not
if (platform_1.isIOS) {
var plist = utils.ios.getter(NSBundle, NSBundle.mainBundle).pathForResourceOfType(null, "plist");
var fileDate = new fsa.FileSystemAccess().getLastModified(plist);
buildTime = "" + fileDate.getTime();
}
else {
var codePushApkBuildTimeStringId = utils.ad.resources.getStringId(TNSLocalPackage.CODEPUSH_APK_BUILD_TIME);
buildTime = utils.ad.getApplicationContext().getResources().getString(codePushApkBuildTimeStringId);
}
appSettings.setString(TNSLocalPackage.CODEPUSH_CURRENT_APPBUILDTIME, buildTime);
//noinspection JSIgnoredPromiseFromCall (removal is async, don't really care if it fails)
fs.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("CodePush package unzip progress: " + percent);
}, onUnzipComplete);
};
TNSLocalPackage.unzip = function (archive, destination, progressCallback, completionCallback) {
if (platform_1.isIOS) {
TNSCodePush.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.unzipWithProgress(archive, destination, 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 (!platform_1.isIOS) {
return;
}
appSettings.remove(TNSLocalPackage.CODEPUSH_CURRENT_APPVERSION);
appSettings.remove(TNSLocalPackage.CODEPUSH_CURRENT_APPBUILDTIME);
var codePushFolder = fs.Folder.fromPath(fs.knownFolders.documents().path + "/CodePush");
//noinspection JSIgnoredPromiseFromCall
codePushFolder.clear();
};
TNSLocalPackage.saveCurrentPackage = function (pack) {
appSettings.setString(TNSLocalPackage.CODEPUSH_CURRENT_PACKAGE, JSON.stringify(pack));
};
TNSLocalPackage.getCurrentPackage = function () {
var packageStr = appSettings.getString(TNSLocalPackage.CODEPUSH_CURRENT_PACKAGE, null);
return packageStr === null ? null : JSON.parse(packageStr);
};
TNSLocalPackage.copyFolder = function (fromPath, toPath) {
if (platform_1.isIOS) {
return TNSCodePush.copyEntriesInFolderDestFolderError(fromPath, toPath);
}
else {
try {
com.tns.TNSCodePush.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 CodePush package was installed
TNSLocalPackage.CODEPUSH_CURRENT_APPVERSION = "CODEPUSH_CURRENT_APPVERSION"; // same as native
TNSLocalPackage.CODEPUSH_CURRENT_PACKAGE = "CODEPUSH_CURRENT_PACKAGE";
// this is the build timestamp of the app at the moment the CodePush package was installed
TNSLocalPackage.CODEPUSH_CURRENT_APPBUILDTIME = "CODEPUSH_CURRENT_APPBUILDTIME"; // same as native
TNSLocalPackage.CODEPUSH_APK_BUILD_TIME = "CODE_PUSH_APK_BUILD_TIME"; // same as include.gradle
return TNSLocalPackage;
}());
exports.TNSLocalPackage = TNSLocalPackage;