"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
require("reflect-metadata");
const core_1 = require("@oclif/core");
const command_base_1 = require("../cli-common/command-base");
const codiac_relay_contracts_1 = require("@codiac.io/codiac-relay-contracts");
const command_base_tenantless_1 = require("../command-base-tenantless");
const ops_1 = require("../ops");
const ops_2 = require("../ops");
class Publish extends command_base_tenantless_1.CommandBaseTenantless {
    run() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve, reject) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                try {
                    this.bootstrap();
                    const { args, flags } = yield this.parse(Publish);
                    let targetRemote = true;
                    if (flags.remote)
                        targetRemote = true;
                    if (flags.local)
                        targetRemote = false;
                    if (flags.silent === true) {
                        this.ioc.rebind(ops_2.INTERFACES_CLI.IUserInputProvider).to(ops_1.ThrowingUserInputProvider);
                    }
                    // TODO:  Consider prompting the user to create an image (or package)
                    //        if none exist yet.
                    // STEP 1) Publish Packages
                    // await this._ops.npmPublish(targetRemote);
                    // STEP 2) Publish containers
                    let depTarget = yield this.acquireDependencyToTarget(this.ctor.name, flags.only, args.name);
                    yield this._ops.projectPublishImages(targetRemote, depTarget, flags.image, flags.username, flags.password);
                    // STEP 3) Remember target mode
                    if (flags.local || flags.remote) {
                        this._projUserCfg.publishingMode = (flags.local) ? codiac_relay_contracts_1.PublishingMode.local : codiac_relay_contracts_1.PublishingMode.remote;
                        this.projCfgRepo.writeProjectUserConfig(this._projUserCfg);
                    }
                    resolve();
                }
                catch (err) {
                    this.handleError(err);
                    reject(err);
                }
            }));
        });
    }
}
exports.default = Publish;
Publish.description = `Publishes the exports (packages and images) declared in the ${codiac_relay_contracts_1.ProjectConfig.name}.`;
Publish.flags = Object.assign({ help: core_1.Flags.help({ char: 'h' }), remote: core_1.Flags.boolean({ char: 'r', required: false, description: 'Publishes to the remote image registry.  NOTE: this setting gets remembered and is used for subsequent calls unless otherwise specified.' }), local: core_1.Flags.boolean({ char: 'l', required: false, description: 'Publishes to the local built-in image registry.  NOTE: this setting gets remembered and is used for subsequent calls unless otherwise specified.' }), image: core_1.Flags.string({ char: 'i', multiple: true, required: false, description: '(optional, defaults to all) Limits the list of images to publish.  Value must match the name property of an image export defined in the codiac project file (codiac.json)' }), username: core_1.Flags.string({ char: 'u', required: false, description: '(optional, prompts as needed) Username at the target registry.' }), password: core_1.Flags.string({ char: 'p', required: false, description: '(optional, prompts as needed) Password or token at the target registry.' }), silent: core_1.Flags.boolean({ char: 'y', description: 'Suppresses user dialogue.', dependsOn: ["image", "username", "password"] }) }, command_base_1.commandBase.dependencyTargetingFlags);
Publish.args = Object.assign({}, command_base_1.commandBase.dependencyTargetingArgs);
Publish.ANONYMOUS_USERNAME = "anonymous";
//# sourceMappingURL=publish.js.map