"use strict";
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());
    });
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander = require("commander");
const publish_1 = require("../Api/publish");
const ewizardService_types_1 = require("../interfaces/ewizardService.types");
const utils_1 = require("../utils");
const codeArtifactUtils_1 = require("../utils/codeArtifactUtils");
const I18n_1 = require("./../modules/I18n");
const ensureAuth_1 = require("./utils/ensureAuth");
const logger_1 = require("./utils/logger");
const validatePackageName_1 = require("./utils/validatePackageName");
commander
    .name('wiz publish')
    .description('Publish component to a private repository. Use `-r global` to publish to the global repository')
    .addOption(new commander.Option(`-r, --repository <repositoryType>`, 'Publish component to the global repository').choices(['global']))
    .parse();
publish(commander.opts())
    .catch(logger_1.handleError)
    .finally(logger_1.closeLogger);
function publish(options) {
    return __awaiter(this, void 0, void 0, function* () {
        const ewizard = yield ensureAuth_1.default();
        const cwd = process.cwd();
        const npmRegistryParams = yield codeArtifactUtils_1.getNpmRegistryParams(options, cwd, ewizardService_types_1.ActionType.Publish);
        const _a = yield ewizard.getNpmRegistry(npmRegistryParams), { packageNamePatterns } = _a, npmRegistryConfig = __rest(_a, ["packageNamePatterns"]);
        if (packageNamePatterns.length) {
            const packageName = yield utils_1.getPackageName(cwd);
            const isValidPackageName = validatePackageName_1.validatePackageName(packageName, packageNamePatterns);
            if (!isValidPackageName) {
                throw new Error(I18n_1.default.error('PUBLISH_GLOBAL_NAME'));
            }
        }
        const publishOptions = Object.assign({ cwd }, npmRegistryConfig);
        const publish = yield publish_1.createAction(cwd);
        return publish(publishOptions);
    });
}
//# sourceMappingURL=wiz-publish.js.map