"use strict";
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) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(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 util_1 = require("util");
const fs = require("fs");
const pfs = require("../../../util/misc/promisfied-fs");
const update_contents_tasks_1 = require("./update-contents-tasks");
const file_utils_1 = require("./file-utils");
const validation_utils_1 = require("./validation-utils");
const appcenter_file_upload_client_1 = require("appcenter-file-upload-client");
const chalk = require("chalk");
const debug = require("debug")("appcenter-cli:commands:codepush:release-base");
class CodePushReleaseCommandBase extends commandline_1.AppCommand {
    constructor(args) {
        super(args);
        this.fileUploadClient = new appcenter_file_upload_client_1.default();
    }
    run(client) {
        return __awaiter(this, void 0, void 0, function* () {
            throw new Error("For dev purposes only!");
        });
    }
    release(client) {
        return __awaiter(this, void 0, void 0, function* () {
            this.rollout = Number(this.specifiedRollout);
            const validationResult = yield this.validate(client);
            if (!validationResult.succeeded) {
                return validationResult;
            }
            this.deploymentName = this.specifiedDeploymentName;
            if (this.privateKeyPath) {
                const appInfo = (yield interaction_1.out.progress("Getting app info...", apis_1.clientRequest((cb) => client.appsOperations.get(this.app.ownerName, this.app.appName, cb)))).result;
                const platform = appInfo.platform.toLowerCase();
                // In React-Native case we should add "CodePush" name folder as root for relase files for keeping sync with React Native client SDK.
                // Also single file also should be in "CodePush" folder.
                if (platform === "react-native" &&
                    (file_utils_1.getLastFolderInPath(this.updateContentsPath) !== "CodePush" || !file_utils_1.isDirectory(this.updateContentsPath))) {
                    yield file_utils_1.moveReleaseFilesInTmpFolder(this.updateContentsPath).then((tmpPath) => {
                        this.updateContentsPath = tmpPath;
                    });
                }
                yield update_contents_tasks_1.sign(this.privateKeyPath, this.updateContentsPath);
            }
            const updateContentsZipPath = yield update_contents_tasks_1.zip(this.updateContentsPath);
            try {
                const app = this.app;
                this.checkTargetBinaryVersion(this.targetBinaryVersion);
                const releaseUpload = this.upload(client, app, this.deploymentName, updateContentsZipPath);
                yield interaction_1.out.progress("Uploading bundle...", releaseUpload);
                yield interaction_1.out.progress("Creating CodePush release...", this.createRelease(client, app, this.deploymentName, {
                    releaseUpload: yield releaseUpload,
                    targetBinaryVersion: this.targetBinaryVersion,
                    description: this.description,
                    disabled: this.disabled,
                    mandatory: this.mandatory,
                    rollout: this.rollout,
                }));
                interaction_1.out.text(`Successfully released an update containing the "${this.updateContentsPath}" ` +
                    `${fs.lstatSync(this.updateContentsPath).isDirectory() ? "directory" : "file"}` +
                    ` to the "${this.deploymentName}" deployment of the "${this.app.appName}" app.`);
                return commandline_1.success();
            }
            catch (error) {
                if (error.response && error.response.statusCode === 409 && this.disableDuplicateReleaseError) {
                    // 409 (Conflict) status code means that uploaded package is identical
                    // to the contents of the specified deployment's current release
                    console.warn(chalk.yellow("[Warning] " + error.response.body));
                    return commandline_1.success();
                }
                else {
                    debug(`Failed to release a CodePush update - ${util_1.inspect(error)}`);
                    return commandline_1.failure(commandline_1.ErrorCodes.Exception, error.response ? error.response.body : error);
                }
            }
            finally {
                yield pfs.rmDir(updateContentsZipPath);
            }
        });
    }
    upload(client, app, deploymentName, updateContentsZipPath) {
        return __awaiter(this, void 0, void 0, function* () {
            debug(`Starting release upload on deployment: ${deploymentName} with zip file: ${updateContentsZipPath}`);
            const releaseUpload = (yield apis_1.clientRequest((cb) => client.codePushDeploymentUpload.create(deploymentName, app.ownerName, app.appName, cb))).result;
            yield this.uploadBundle(releaseUpload, updateContentsZipPath);
            return releaseUpload;
        });
    }
    createRelease(client, app, deploymentName, uploadedRelease) {
        return __awaiter(this, void 0, void 0, function* () {
            debug(`Starting release process on deployment: ${deploymentName} with uploaded release metadata: ${util_1.inspect(uploadedRelease)}`);
            yield apis_1.clientRequest((cb) => client.codePushDeploymentReleases.create(deploymentName, uploadedRelease, app.ownerName, app.appName, cb));
        });
    }
    uploadBundle(releaseUpload, bundleZipPath) {
        return __awaiter(this, void 0, void 0, function* () {
            debug(`Starting to upload the release bundle: ${bundleZipPath} with upload data: ${util_1.inspect(releaseUpload)}`);
            yield this.fileUploadClient.upload({
                assetId: releaseUpload.id,
                assetDomain: releaseUpload.uploadDomain,
                assetToken: releaseUpload.token,
                file: bundleZipPath,
                onMessage: (message, level) => {
                    debug(`Upload client message: ${message}`);
                },
            });
        });
    }
    checkTargetBinaryVersion(version) {
        const warningVersion = validation_utils_1.validateVersion(version);
        if (warningVersion) {
            interaction_1.out.text(`\nYour target-binary-version "${version}" will be treated as "${warningVersion}".\n`);
        }
    }
    validate(client) {
        return __awaiter(this, void 0, void 0, function* () {
            if (file_utils_1.isBinaryOrZip(this.updateContentsPath)) {
                return commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, "It is unnecessary to package releases in a .zip or binary file. Please specify the direct path to the update content's directory (e.g. /platforms/ios/www) or file (e.g. main.jsbundle).");
            }
            if (!validation_utils_1.isValidRange(this.targetBinaryVersion)) {
                return commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, "Invalid binary version(s) for a release.");
            }
            if (!Number.isSafeInteger(this.rollout) || !validation_utils_1.isValidRollout(this.rollout)) {
                return commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `Rollout value should be integer value between ${chalk.bold("1")} and ${chalk.bold("100")}.`);
            }
            if (!this.deploymentName && !(yield validation_utils_1.isValidDeployment(client, this.app, this.specifiedDeploymentName))) {
                return commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `Deployment "${this.specifiedDeploymentName}" does not exist.`);
            }
            return commandline_1.success();
        });
    }
}
__decorate([
    commandline_1.help("Deployment to release the update to"),
    commandline_1.shortName("d"),
    commandline_1.longName("deployment-name"),
    commandline_1.defaultValue("Staging"),
    commandline_1.hasArg
], CodePushReleaseCommandBase.prototype, "specifiedDeploymentName", void 0);
__decorate([
    commandline_1.help("Description of the changes made to the app in this release"),
    commandline_1.longName("description"),
    commandline_1.hasArg
], CodePushReleaseCommandBase.prototype, "description", void 0);
__decorate([
    commandline_1.help("Specifies whether this release should be immediately downloadable"),
    commandline_1.shortName("x"),
    commandline_1.longName("disabled")
], CodePushReleaseCommandBase.prototype, "disabled", void 0);
__decorate([
    commandline_1.help("Specifies whether this release should be considered mandatory"),
    commandline_1.shortName("m"),
    commandline_1.longName("mandatory")
], CodePushReleaseCommandBase.prototype, "mandatory", void 0);
__decorate([
    commandline_1.help("Specifies the location of a RSA private key to sign the release with." +
        chalk.yellow("NOTICE:") +
        " use it for react native applications only, client SDK on other platforms will be ignoring signature verification for now!"),
    commandline_1.shortName("k"),
    commandline_1.longName("private-key-path"),
    commandline_1.hasArg
], CodePushReleaseCommandBase.prototype, "privateKeyPath", void 0);
__decorate([
    commandline_1.help("When this flag is set, releasing a package that is identical to the latest release will produce a warning instead of an error"),
    commandline_1.longName("disable-duplicate-release-error")
], CodePushReleaseCommandBase.prototype, "disableDuplicateReleaseError", void 0);
__decorate([
    commandline_1.help("Percentage of users this release should be available to"),
    commandline_1.shortName("r"),
    commandline_1.longName("rollout"),
    commandline_1.defaultValue("100"),
    commandline_1.hasArg
], CodePushReleaseCommandBase.prototype, "specifiedRollout", void 0);
exports.default = CodePushReleaseCommandBase;
