"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.LinkPackageAction = 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 LinkPackageAction extends BaseHotlinkPackageAction_1.BaseHotlinkPackageAction {
    constructor(parser) {
        super({
            actionName: actionNameConstants_1.LINK_PACKAGE_ACTION_NAME,
            summary: '(EXPERIMENTAL) Use hotlinks to simulate installation of a locally built project folder as a dependency' +
                ' of specific projects.',
            documentation: 'This command enables you to test a locally built project by creating a symlink under the specified' +
                ' projects\' node_modules folders.  The implementation is similar to "pnpm link" and "npm link", but' +
                ' better integrated with Rush features.  Like those commands, the symlink ("hotlink") is not reflected' +
                ' in pnpm-lock.yaml, affects the consuming project only, and has the same limitations as "workspace:*".' +
                '  The hotlinks will be cleared when you next run "rush install" or "rush update".' +
                `  Compare with the "rush ${actionNameConstants_1.BRIDGE_PACKAGE_ACTION_NAME}" command, which affects the entire lockfile` +
                ' including indirect dependencies.',
            parser
        });
        this._projectListParameter = this.defineStringListParameter({
            parameterLongName: '--project',
            argumentName: 'PROJECT_NAME',
            required: false,
            description: 'A list of Rush project names that will be hotlinked to the "--path" folder. ' +
                'If not specified, the default is the project of the current working directory.'
        });
    }
    async _getProjectsToLinkAsync() {
        const projectsToLink = new Set();
        const projectNames = this._projectListParameter.values;
        if (projectNames.length > 0) {
            for (const projectName of projectNames) {
                const project = this.rushConfiguration.getProjectByName(projectName);
                if (!project) {
                    throw new Error(`The project "${projectName}" was not found in "${RushConstants_1.RushConstants.rushPackageName}"`);
                }
                projectsToLink.add(project);
            }
        }
        else {
            const currentProject = this.rushConfiguration.tryGetProjectForPath(process.cwd());
            if (!currentProject) {
                throw new Error(`No Rush project was found in the current working directory`);
            }
            projectsToLink.add(currentProject);
        }
        return projectsToLink;
    }
    async hotlinkPackageAsync(linkedPackagePath, hotlinkManager) {
        const projectsToLink = await this._getProjectsToLinkAsync();
        await node_core_library_1.Async.forEachAsync(projectsToLink, async (project) => {
            await hotlinkManager.linkPackageAsync(this.terminal, project, linkedPackagePath);
        }, { concurrency: 5 });
    }
}
exports.LinkPackageAction = LinkPackageAction;
//# sourceMappingURL=LinkPackageAction.js.map