appcenter-cli
Version:
Command line tool for Visual Studio App Center
224 lines (223 loc) • 12.4 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const commandline_1 = require("../../../util/commandline");
const apis_1 = require("../../../util/apis");
const interaction_1 = require("../../../util/interaction");
const process_1 = require("process");
const Path = require("path");
const _ = require("lodash");
const MkDirP = require("mkdirp");
const util_1 = require("util");
const Url = require("url");
const Request = require("request");
const Fs = require("fs");
const debug = require("debug")("appcenter-cli:commands:distribute:groups:download");
let DownloadBinaryFromDistributionGroupCommand = class DownloadBinaryFromDistributionGroupCommand extends commandline_1.AppCommand {
run(client) {
return __awaiter(this, void 0, void 0, function* () {
const app = this.app;
// test that optional release id is a positive integer and optional file name is valid
this.validateParameters();
let downloadUrl;
if (!_.isNil(this.releaseId)) {
// distribute.getReleaseForDistributionGroup doesn't support the specific release id now, using two parallel requests instead
const validateReleaseBelongsToReleaseGroup = this.verifyReleaseBelongsToDistributionGroup(client, app, Number(this.releaseId), this.distributionGroup);
const releaseUrl = this.getReleaseUrl(client, app, this.releaseId);
// showing spinner while getting download url and verifying that the specified release was distributed to this distribution group
yield interaction_1.out.progress("Getting release URL...", Promise.all([validateReleaseBelongsToReleaseGroup, releaseUrl]));
downloadUrl = yield releaseUrl;
}
else {
// using distribute.getReleaseForDistributionGroup for getting latest release
downloadUrl = yield interaction_1.out.progress("Getting release URL...", this.getLastReleaseUrl(client, app, this.distributionGroup));
}
const directoryPath = yield this.getDirectoryPath(this.directory);
const filePath = this.getFileFullPath(this.fileName, directoryPath, app.appName, downloadUrl);
yield interaction_1.out.progress("Downloading release...", this.downloadReleasePackageToFile(downloadUrl, filePath));
interaction_1.out.text((obj) => `Release was saved to ${obj.path}`, { path: filePath });
return commandline_1.success();
});
}
validateParameters() {
if (!_.isNil(this.releaseId)) {
const releaseIdNumber = Number(this.releaseId);
if (!Number.isSafeInteger(releaseIdNumber) || !(releaseIdNumber > 0)) {
throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `${this.releaseId} is not a valid release id`);
}
}
if (!_.isNil(this.fileName) && this.fileName !== Path.basename(this.fileName)) {
throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `file name ${this.fileName} is not valid`);
}
}
verifyReleaseBelongsToDistributionGroup(client, app, releaseId, distributionGroup) {
return __awaiter(this, void 0, void 0, function* () {
debug("Verifying that release was distributed to the specified distribution group");
let releasesIds;
try {
const httpRequest = yield apis_1.clientRequest((cb) => client.releases.listByDistributionGroup(distributionGroup, app.ownerName, app.appName, cb));
if (httpRequest.response.statusCode >= 400) {
throw httpRequest.response.statusCode;
}
else {
releasesIds = httpRequest.result.map((details) => details.id);
}
}
catch (error) {
if (error === 404) {
throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `distribution group ${distributionGroup} doesn't exist`);
}
else {
debug(`Failed to get list of the releases for the distribution group - ${util_1.inspect(error)}`);
throw commandline_1.failure(commandline_1.ErrorCodes.Exception, "failed to get the list of the releases for the distribution group");
}
}
if (releasesIds.indexOf(releaseId) === -1) {
throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `release ${releaseId} was not distributed to distribution group ${distributionGroup}`);
}
});
}
getReleaseUrl(client, app, releaseId) {
return __awaiter(this, void 0, void 0, function* () {
debug("Getting download URL for the specified release");
try {
const httpRequest = yield apis_1.clientRequest((cb) => client.releases.getLatestByUser(releaseId, app.ownerName, app.appName, cb));
if (httpRequest.response.statusCode >= 400) {
throw httpRequest.response.statusCode;
}
else {
return httpRequest.result.downloadUrl;
}
}
catch (error) {
if (error === 404) {
throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `release ${releaseId} doesn't exist`);
}
else {
debug(`Failed to get details for release ${releaseId} - ${util_1.inspect(error)}`);
throw commandline_1.failure(commandline_1.ErrorCodes.Exception, "failed to get details of the release");
}
}
});
}
getLastReleaseUrl(client, app, distributionGroup) {
return __awaiter(this, void 0, void 0, function* () {
debug("Getting download URL for the latest release of the specified distribution group");
try {
const httpRequest = yield apis_1.clientRequest((cb) => client.releases.getLatestByDistributionGroup(app.ownerName, app.appName, distributionGroup, "latest", cb));
if (httpRequest.response.statusCode >= 400) {
throw httpRequest.result;
}
else {
return httpRequest.result.downloadUrl;
}
}
catch (error) {
switch (error.code) {
case "no_releases_for_app":
throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `there were no releases for the distribution group ${distributionGroup}`);
case "not_found":
throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `distribution group ${distributionGroup} doesn't exist`);
default:
debug(`Failed to get details of the latest release for distribution group ${distributionGroup} - ${util_1.inspect(error)}`);
throw commandline_1.failure(commandline_1.ErrorCodes.Exception, "failed to get details of the latest release for the distribution group");
}
}
});
}
getDirectoryPath(directoryPath) {
if (!_.isNil(directoryPath)) {
const normalizedPath = Path.normalize(directoryPath);
debug("Checking that specified directories exist and creating them if not");
return new Promise((resolve, reject) => {
MkDirP(normalizedPath, (error) => {
if (!_.isNil(error)) {
if (error.code === "EEXIST") {
reject(commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `file ${directoryPath} already exists - directory path is expected`));
}
else {
debug(`Failed to create/access directory ${directoryPath} - ${util_1.inspect(error)}`);
reject(commandline_1.failure(commandline_1.ErrorCodes.Exception, `failed to create/access directory ${directoryPath}`));
}
}
else {
resolve(normalizedPath);
}
});
});
}
else {
// using current working directory by default
return Promise.resolve(process_1.cwd());
}
}
getFileFullPath(passedFileName, directoryPath, appName, downloadUrl) {
if (_.isNil(passedFileName)) {
// creating default file name from app name and "format" query key value of download url
const ext = "." + Url.parse(downloadUrl, true).query.format;
const name = appName;
return Path.format({ dir: directoryPath, name, ext, base: null, root: null });
}
else {
return Path.join(directoryPath, passedFileName);
}
}
downloadReleasePackageToFile(downloadUrl, filePath) {
debug("Downloading the release package to the path");
return new Promise((resolve, reject) => {
Request.get(downloadUrl)
.on("error", (error) => {
debug(`Failed to download the release from ${downloadUrl} - ${util_1.inspect(error)}`);
reject(commandline_1.failure(commandline_1.ErrorCodes.Exception, `failed to download the release from ${downloadUrl}`));
})
.pipe(Fs.createWriteStream(filePath)
.on("error", (error) => {
debug(`Failed to save the release to ${filePath} - ${util_1.inspect(error)}`);
reject(commandline_1.failure(commandline_1.ErrorCodes.Exception, `failed to save the release to ${filePath}`));
})
.on("finish", () => resolve()));
});
}
};
__decorate([
commandline_1.help("Distribution group name"),
commandline_1.shortName("g"),
commandline_1.longName("group"),
commandline_1.required,
commandline_1.hasArg
], DownloadBinaryFromDistributionGroupCommand.prototype, "distributionGroup", void 0);
__decorate([
commandline_1.help("Release ID"),
commandline_1.shortName("i"),
commandline_1.longName("id"),
commandline_1.hasArg
], DownloadBinaryFromDistributionGroupCommand.prototype, "releaseId", void 0);
__decorate([
commandline_1.help("Name of the destination file"),
commandline_1.shortName("f"),
commandline_1.longName("filename"),
commandline_1.hasArg
], DownloadBinaryFromDistributionGroupCommand.prototype, "fileName", void 0);
__decorate([
commandline_1.help("Directory path for the destination file"),
commandline_1.shortName("d"),
commandline_1.longName("dest"),
commandline_1.hasArg
], DownloadBinaryFromDistributionGroupCommand.prototype, "directory", void 0);
DownloadBinaryFromDistributionGroupCommand = __decorate([
commandline_1.help("Download release package for the distribution group")
], DownloadBinaryFromDistributionGroupCommand);
exports.default = DownloadBinaryFromDistributionGroupCommand;