// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { BaseRushAction } from './BaseRushAction';
import { RushConstants } from '../../logic/RushConstants';
import { VARIANT_PARAMETER } from '../../api/Variants';
export const PACKAGE_PARAMETER_NAME = '--package';
/**
 * This is the common base class for AddAction and RemoveAction.
 */
export class BaseAddAndRemoveAction extends BaseRushAction {
    get specifiedPackageNameList() {
        return this._packageNameListParameter.values;
    }
    constructor(options) {
        super(options);
        const { packageNameListParameterDescription, allFlagDescription } = options;
        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._packageNameListParameter = this.defineStringListParameter({
            parameterLongName: PACKAGE_PARAMETER_NAME,
            parameterShortName: '-p',
            required: true,
            argumentName: 'PACKAGE',
            description: packageNameListParameterDescription
        });
        this._allFlag = this.defineFlagParameter({
            parameterLongName: '--all',
            description: allFlagDescription
        });
        this._variantParameter = this.defineStringParameter(VARIANT_PARAMETER);
    }
    getProjects() {
        if (this._allFlag.value) {
            return this.rushConfiguration.projects;
        }
        else {
            const currentProject = this.rushConfiguration.tryGetProjectForPath(process.cwd());
            if (!currentProject) {
                throw new Error(`The rush "${this.actionName}" command must be invoked under a project` +
                    ` folder that is registered in ${RushConstants.rushJsonFilename} unless the ${this._allFlag.longName} is used.`);
            }
            return [currentProject];
        }
    }
    async runAsync() {
        const packageJsonUpdater = await import(
        /* webpackChunkName: 'PackageJsonUpdater' */ '../../logic/PackageJsonUpdater');
        const updater = new packageJsonUpdater.PackageJsonUpdater(this.terminal, this.rushConfiguration, this.rushGlobalFolder);
        const updateOptions = await this.getUpdateOptionsAsync();
        await updater.doRushUpdateAsync(updateOptions);
    }
}
//# sourceMappingURL=BaseAddAndRemoveAction.js.map