"use strict";
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 ora = require("ora");
const evilsniffer_1 = require("../../server/metadata/evilsniffer");
const bundler_1 = require("../../server/publish/bundler");
const publisher_1 = require("../../server/publish/publisher");
const uploader_1 = require("../../server/publish/uploader");
const util_1 = require("../../server/util");
const options_1 = require("../options");
const prereqs_1 = require("../prereqs");
const writer_1 = require("../writer");
function default_1(options) {
    return __awaiter(this, void 0, void 0, function* () {
        const confirmation = `You about to publish your interactive ` +
            `controls. This will make them accessible to everyone on Mixer.`;
        if (!options.force && !(yield writer_1.default.confirm(confirmation))) {
            writer_1.default.write('Aborted.');
            return;
        }
        const spinner = ora('Starting...').start();
        const bundler = new bundler_1.Bundler(options.project);
        const fetcher = new util_1.Fetcher().with(yield options.project.profile.tokens());
        if (!(yield new evilsniffer_1.WriterReporter(options.project).checkEvil(writer_1.default))) {
            return;
        }
        if (!options.skipBundle) {
            yield prereqs_1.ensureWebpackDependencies(options.project);
            let filename = options.tarball;
            if (!filename) {
                const output = yield bundler
                    .bundle(progress => {
                    spinner.text = progress;
                })
                    .catch(options_1.failSpiner(spinner));
                filename = output.filename;
            }
            spinner.text = 'Uploading to Mixer...';
            yield new uploader_1.Uploader(fetcher, options.project).upload(filename).catch(options_1.failSpiner(spinner));
        }
        spinner.text = 'Publishing...';
        yield new publisher_1.Publisher(fetcher).publish(options.project).catch(options_1.failSpiner(spinner));
        const config = yield options.project.packageConfig();
        spinner.succeed(`Published ${config.name}@${config.version}`);
    });
}
exports.default = default_1;
