"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.BridgePackageAction = void 0;
const node_core_library_1 = require("@rushstack/node-core-library");
const BaseHotlinkPackageAction_1 = require("./BaseHotlinkPackageAction");
const actionNameConstants_1 = require("../../utilities/actionNameConstants");
const RushConstants_1 = require("../../logic/RushConstants");
class BridgePackageAction extends BaseHotlinkPackageAction_1.BaseHotlinkPackageAction {
    constructor(parser) {
        super({
            actionName: actionNameConstants_1.BRIDGE_PACKAGE_ACTION_NAME,
            summary: '(EXPERIMENTAL) Use hotlinks to simulate upgrade of a dependency for all consumers across a lockfile.',
            documentation: 'This command enables you to test a locally built project by simulating its upgrade by updating' +
                ' node_modules folders using hotlinks.  Unlike "pnpm link" and "npm link", the hotlinks created by this' +
                ' command affect all Rush projects across the lockfile, as well as their indirect dependencies.  The' +
                ' simulated installation is not reflected in pnpm-lock.yaml, does not install new package.json dependencies,' +
                ' and simply updates the contents of existing node_modules folders of "rush install".' +
                '  The hotlinks will be cleared when you next run "rush install" or "rush update".' +
                `  Compare with the "rush ${actionNameConstants_1.LINK_PACKAGE_ACTION_NAME}" command, which affects only the consuming project.`,
            parser
        });
        this._versionParameter = this.defineStringParameter({
            parameterLongName: '--version',
            argumentName: 'SEMVER_RANGE',
            defaultValue: '*',
            description: 'Specify which installed versions should be hotlinked.'
        });
        this._subspaceNamesParameter = this.defineStringListParameter({
            parameterLongName: '--subspace',
            argumentName: 'SUBSPACE_NAME',
            description: 'The name of the subspace to use for the hotlinked package.'
        });
    }
    _getSubspacesToBridgeAsync() {
        const subspaceToBridge = new Set();
        const subspaceNames = this._subspaceNamesParameter.values;
        if (subspaceNames.length > 0) {
            for (const subspaceName of subspaceNames) {
                const subspace = this.rushConfiguration.tryGetSubspace(subspaceName);
                if (!subspace) {
                    throw new Error(`The subspace "${subspaceName}" was not found in "${RushConstants_1.RushConstants.rushPackageName}"`);
                }
                subspaceToBridge.add(subspace);
            }
        }
        else {
            const currentProject = this.rushConfiguration.tryGetProjectForPath(process.cwd());
            if (!currentProject) {
                throw new Error(`No Rush project was found in the current working directory`);
            }
            subspaceToBridge.add(currentProject.subspace);
        }
        return subspaceToBridge;
    }
    async hotlinkPackageAsync(linkedPackagePath, hotlinkManager) {
        const version = this._versionParameter.value;
        const subspaces = await this._getSubspacesToBridgeAsync();
        await node_core_library_1.Async.forEachAsync(subspaces, async (subspace) => {
            await hotlinkManager.bridgePackageAsync(this.terminal, subspace, linkedPackagePath, version);
        }, { concurrency: 5 });
    }
}
exports.BridgePackageAction = BridgePackageAction;
//# sourceMappingURL=BridgePackageAction.js.map