mobile-cli-lib
Version:
common lib used by different CLI
126 lines (125 loc) • 5.29 kB
JavaScript
"use strict";
var path = require("path");
var temp = require("temp");
var AndroidDeviceHashService = (function () {
function AndroidDeviceHashService(adb, appIdentifier, $fs, $mobileHelper) {
this.adb = adb;
this.appIdentifier = appIdentifier;
this.$fs = $fs;
this.$mobileHelper = $mobileHelper;
this._hashFileDevicePath = null;
this._hashFileLocalPath = null;
this._tempDir = null;
}
Object.defineProperty(AndroidDeviceHashService.prototype, "hashFileDevicePath", {
get: function () {
if (!this._hashFileDevicePath) {
this._hashFileDevicePath = this.$mobileHelper.buildDevicePath(AndroidDeviceHashService.DEVICE_ROOT_PATH, this.appIdentifier, AndroidDeviceHashService.HASH_FILE_NAME);
}
return this._hashFileDevicePath;
},
enumerable: true,
configurable: true
});
AndroidDeviceHashService.prototype.doesShasumFileExistsOnDevice = function () {
var _this = this;
return (function () {
var lsResult = _this.adb.executeShellCommand(["ls", _this.hashFileDevicePath]).wait();
return !!(lsResult && lsResult.trim() === _this.hashFileDevicePath);
}).future()();
};
AndroidDeviceHashService.prototype.getShasumsFromDevice = function () {
var _this = this;
return (function () {
var hashFileLocalPath = _this.downloadHashFileFromDevice().wait();
if (_this.$fs.exists(hashFileLocalPath).wait()) {
return _this.$fs.readJson(hashFileLocalPath).wait();
}
return null;
}).future()();
};
AndroidDeviceHashService.prototype.uploadHashFileToDevice = function (data) {
var _this = this;
return (function () {
var shasums = {};
if (_.isArray(data)) {
data.forEach(function (localToDevicePathData) {
var localPath = localToDevicePathData.getLocalPath();
var stats = _this.$fs.getFsStats(localPath).wait();
if (stats.isFile()) {
var fileShasum = _this.$fs.getFileShasum(localPath).wait();
shasums[localPath] = fileShasum;
}
});
}
else {
shasums = data;
}
_this.$fs.writeJson(_this.hashFileLocalPath, shasums).wait();
_this.adb.executeCommand(["push", _this.hashFileLocalPath, _this.hashFileDevicePath]).wait();
}).future()();
};
AndroidDeviceHashService.prototype.updateHashes = function (localToDevicePaths) {
var _this = this;
return (function () {
var oldShasums = _this.getShasumsFromDevice().wait();
if (oldShasums) {
_.each(localToDevicePaths, function (ldp) {
var localPath = ldp.getLocalPath();
if (_this.$fs.getFsStats(localPath).wait().isFile()) {
oldShasums[localPath] = _this.$fs.getFileShasum(localPath).wait();
}
});
_this.uploadHashFileToDevice(oldShasums).wait();
return true;
}
return false;
}).future()();
};
AndroidDeviceHashService.prototype.removeHashes = function (localToDevicePaths) {
var _this = this;
return (function () {
var oldShasums = _this.getShasumsFromDevice().wait();
if (oldShasums) {
var fileToShasumDictionary = (_.omit(oldShasums, localToDevicePaths.map(function (ldp) { return ldp.getLocalPath(); })));
_this.uploadHashFileToDevice(fileToShasumDictionary).wait();
return true;
}
return false;
}).future()();
};
Object.defineProperty(AndroidDeviceHashService.prototype, "hashFileLocalPath", {
get: function () {
if (!this._hashFileLocalPath) {
this._hashFileLocalPath = path.join(this.tempDir, AndroidDeviceHashService.HASH_FILE_NAME);
}
return this._hashFileLocalPath;
},
enumerable: true,
configurable: true
});
Object.defineProperty(AndroidDeviceHashService.prototype, "tempDir", {
get: function () {
if (!this._tempDir) {
temp.track();
this._tempDir = temp.mkdirSync("android-device-hash-service-" + this.appIdentifier);
}
return this._tempDir;
},
enumerable: true,
configurable: true
});
AndroidDeviceHashService.prototype.downloadHashFileFromDevice = function () {
var _this = this;
return (function () {
if (!_this.$fs.exists(_this.hashFileLocalPath).wait()) {
_this.adb.executeCommand(["pull", _this.hashFileDevicePath, _this.tempDir]).wait();
}
return _this.hashFileLocalPath;
}).future()();
};
AndroidDeviceHashService.HASH_FILE_NAME = "hashes";
AndroidDeviceHashService.DEVICE_ROOT_PATH = "/data/local/tmp";
return AndroidDeviceHashService;
}());
exports.AndroidDeviceHashService = AndroidDeviceHashService;