"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.UpgradeInteractiveAction = void 0;
const BaseRushAction_1 = require("./BaseRushAction");
const Variants_1 = require("../../api/Variants");
class UpgradeInteractiveAction extends BaseRushAction_1.BaseRushAction {
    constructor(parser) {
        const documentation = [
            'Provide an interactive way to upgrade your dependencies. Running the command will open an interactive prompt' +
                ' that will ask you which projects and which dependencies you would like to upgrade.' +
                ' It will then update your package.json files, and run "rush update" for you.' +
                ' If you are using ensureConsistentVersions policy, upgrade-interactive will update all packages which use the' +
                ' dependencies that you are upgrading and match their SemVer range if provided. If ensureConsistentVersions' +
                ' is not enabled, upgrade-interactive will only update the dependency in the package you specify.' +
                ' This can be overriden by using the --make-consistent flag.'
        ];
        super({
            actionName: 'upgrade-interactive',
            summary: 'Provides interactive prompt for upgrading package dependencies per project',
            safeForSimultaneousRushProcesses: false,
            documentation: documentation.join(''),
            parser
        });
        this._makeConsistentFlag = this.defineFlagParameter({
            parameterLongName: '--make-consistent',
            description: 'When upgrading dependencies from a single project, also upgrade dependencies from other projects.'
        });
        this._skipUpdateFlag = this.defineFlagParameter({
            parameterLongName: '--skip-update',
            parameterShortName: '-s',
            description: 'If specified, the "rush update" command will not be run after updating the package.json files.'
        });
        this._variantParameter = this.defineStringParameter(Variants_1.VARIANT_PARAMETER);
    }
    async runAsync() {
        const [{ PackageJsonUpdater }, { InteractiveUpgrader }] = await Promise.all([
            Promise.resolve().then(() => __importStar(require(/* webpackChunkName: 'PackageJsonUpdater' */ '../../logic/PackageJsonUpdater'))),
            Promise.resolve().then(() => __importStar(require(/* webpackChunkName: 'InteractiveUpgrader' */ '../../logic/InteractiveUpgrader')))
        ]);
        const packageJsonUpdater = new PackageJsonUpdater(this.terminal, this.rushConfiguration, this.rushGlobalFolder);
        const interactiveUpgrader = new InteractiveUpgrader(this.rushConfiguration);
        const variant = await (0, Variants_1.getVariantAsync)(this._variantParameter, this.rushConfiguration, true);
        const shouldMakeConsistent = this.rushConfiguration.defaultSubspace.shouldEnsureConsistentVersions(variant) ||
            this._makeConsistentFlag.value;
        const { projects, depsToUpgrade } = await interactiveUpgrader.upgradeAsync();
        await packageJsonUpdater.doRushUpgradeAsync({
            projects,
            packagesToAdd: depsToUpgrade.packages,
            updateOtherPackages: shouldMakeConsistent,
            skipUpdate: this._skipUpdateFlag.value,
            debugInstall: this.parser.isDebug,
            variant
        });
    }
}
exports.UpgradeInteractiveAction = UpgradeInteractiveAction;
//# sourceMappingURL=UpgradeInteractiveAction.js.map